首页 > 代码库 > 用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能

用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能

一、搭建开始前的准备

1、我用的MyEclipse2014版,大家也可以用IDEA。

2、下载Tomcat(免安装解压包)、MySQL(zip包下载地址 免安装解压包,好处就是双击启动,最后我会把bat的启动发给大家)、用的Navicat for MySQL的MySQL的图形界面工具。

3、配置环境变量CATALINA_HOME(path中添加%CATALINA_HOME%\lib;%CATALINA_HOME%\bin;)、MySQL_HOME(path中添加%MySQL_HOME%\bin;)

4、启动mysql和Tomcat试试能否正常运行。

5、打开MyEclipse新建Maven项目。File>New>Other(或Ctrl+N)>Maven Project;然后我们用default Workspace就行了(注意Location的路径,区分目录名和项目名);然后再Filter中输入webapp,我们选择org.apache.maven.archetypes 下的maven-archetype-webapp;点击下一步,填入Group Id(公司名,一般为com.xxx),Artifact Id(项目名),包名(一般根据公司名和项目名生成),单机Finish就创建完一个Maven的Web项目了。注:创建出来后有时候会有错(具体是index.jsp)这是因为我们没有在pom.xml中添加servlet的原因,自己加上就OK了。

6、一般创建出来的项目,只有src/main/resources,然后我们加上其他的三项(必须是新建Source Folder!src/main/java; src/test/resources; src/test/java),有时候会提示你该文件夹已经存在,我们稍改一下名,等创建完了后再用F2改回正确的就OK了(例如src/main/java创建失败,我们把java改为javas,最后再改为java即可。)

最终创建出来的目录如图:


7、万事具备只欠搭建,好吧下面才开始进入正文。

二、搭建

1、我习惯先建立一些package包名(src/main/java),例如:xxx.xxx.dao;xxx.xxx.dao.impl;xxx.xxx.model;xxx.xxx.service;xxx.xxx.service.impl;xxx.xxx.controller;具体的包应该放什么,起什么作用,大家可以百度。

2、配置资源文件,也就是所谓的src/main/resources下的文件。

(1.)config.properties

#\u5C5E\u6027\u6587\u4EF6

#Oracle \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.OracleDialect
#driverClassName=oracle.jdbc.driver.OracleDriver
#validationQuery=SELECT 1 FROM DUAL
#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl
#jdbc_username=
#jdbc_password=

#SQLServer \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#driverClassName=net.sourceforge.jtds.jdbc.Driver
#validationQuery=SELECT 1
#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/sy
#jdbc_username=
#jdbc_password=

#Derby \u7684\u914D\u7F6E
#hibernate.dialect=org.hibernate.dialect.DerbyDialect
#driverClassName=org.apache.derby.jdbc.EmbeddedDriver
#validationQuery=SELECT 1
#jdbc_url=jdbc:derby:sy;create=true
#jdbc_username=
#jdbc_password=

#MySQL \u7684\u914D\u7F6E
hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/sshf?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=

hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false

sessionInfoName=sessionInfo

uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached

注意:?useUnicode=true&characterEncoding=utf-8这句话,是UTF8的编码。注释的部分是其他数据库的编写形式。

(2.)log4j.properties

log4j.rootLogger=INFO,A1,R

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%c]%m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=sshf.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.Threshold=ALL
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

注意:sshf.log就是输出日志文件的文件名

(3.)spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config.properties" />

	<!-- 自动扫描dao和service包(自动注入) -->
	<context:component-scan base-package="com.jesus.sshframework.dao,com.jesus.sshframework.service" />

</beans>
没什么可说的,要说的也在注释里了。

(4.)spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context-3.1.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.jesus.sshframework.controller" />
	<!-- 开启mvc注解 -->
	<mvc:annotation-driven />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value=http://www.mamicode.com/"/">>
(5.)spring-hibernate.xml(我喜欢这样写,这样能知道具体用的什么持久层框架,如果用myBatis,则spring-mybatis.xml就可以了)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

	<!-- JNDI(通过Tomcat)方式配置数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value=http://www.mamicode.com/"${jndiName}">  -->>
注:我用的是阿里巴巴温少写的Druid的,具体介绍
3、贴一下pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion><!--maven2.0必须是这样写,现在是maven2唯一支持的版本 -->
	<!-- The Basics -->
	<groupId>com.jesus</groupId><!--指定组名,例如:org.apache.maven -->
	<artifactId>SSHframework</artifactId><!--指定工程名例如:app -->
	<packaging>war</packaging><!--The current core packaging values are: pom, 
		jar, maven-plugin, ejb, war, ear, rar, par -->
	<version>0.0.1-SNAPSHOT</version><!--指定版本号 -->
	<name>SSHframework</name><!--一些无关太重要的东西,用户描述你的项目的名字,可选的 -->
	<url>http://maven.apache.org</url><!--暂时不知何物,貌似无关重要,只是写明开发团队的网站,可选的 -->
	<description></description> <!-- project的描述 -->
	<inceptionYear></inceptionYear><!-- 工程的初始时间 -->
	<licenses></licenses><!--列出本工程直接的licenses,而不要列出dependencies的licenses -->


	<!-- 指定maven仓库 -->
	<repositories><!-- 发现依赖和扩展的远程资源库 -->
		<!-- maven自己官方仓库 -->
		<repository>
			<id>maven</id>
			<name>Maven Repository Switchboard</name>
			<layout>default</layout>
			<url>http://repo1.maven.org/maven2</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
		<!-- 阿里巴巴发布版本仓库 -->
		<repository>
			<id>alibaba-opensource</id>
			<name>alibaba-opensource</name>
			<url>http://code.alibabatech.com/mvn/releases/</url>
			<layout>default</layout>
		</repository>
		<!-- 阿里巴巴快照版本仓库 -->
		<repository>
			<id>alibaba-opensource-snapshot</id>
			<name>alibaba-opensource-snapshot</name>
			<url>http://code.alibabatech.com/mvn/snapshots/</url>
			<layout>default</layout>
		</repository>
	</repositories>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<!-- maven打成war包放到tomcat目录下,即可运行 -->
	<build>
		<finalName>SSHframework</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies><!-- 这个元素描述了所有与当前项目相关的依赖 -->
		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
			<scope>provided</scope>
		</dependency>
		
		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2.1-b03</version>
			<scope>provided</scope>
		</dependency>
		
		<!-- 文件上传 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>
		
		<!-- fastJson json数组有关的 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.26</version>
		</dependency>
		
		<!-- 使用 Jackson 把 Java 对象转换成 JSON 字串 -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.11</version>
		</dependency>
		
		<!-- mysql数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>

		<!-- druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>0.2.9</version>
		</dependency>
		
		<!-- aspectjweaver.jar这是Spring AOP所要用到的包 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.1</version>
		</dependency>
		
		<!-- hibernate4 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.2.5.Final</version>
		</dependency>		
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>4.2.5.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>4.2.5.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate.javax.persistence</groupId>
			<artifactId>hibernate-jpa-2.0-api</artifactId>
			<version>1.0.1.Final</version>
		</dependency>
		
		<!-- spring mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		
		<!-- spring3 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		
	</dependencies>
	
</project>
注:会使用的注意下版本更新就是了,有的可能会有jar包交叉,注意就行了。


好了,这篇有些长了,再起一篇文章吧。


用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能