新建一个maven project ,在这里我就不在讲述怎么创建maven 工程了,首先配置pom.xml pom文件的代码如下:SSM框架整合
org.apache.commons
commons-lang3
3.3.2
org.apache.commons
commons-io
1.3.2
commons-net
commons-net
3.3
com.fasterxml.jackson.core
jackson-databind
2.4.2
org.apache.httpcomponents
httpclient
4.3.5
commons-fileupload
commons-fileupload
1.3.1
junit
junit
4.12
test
org.freemarker
freemarker-gae
2.3.23
org.slf4j
slf4j-log4j12
1.7.7
org.slf4j
slf4j-api
1.7.7
ch.qos.logback
logback-core
1.1.7
ch.qos.logback
logback-classic
1.1.7
mysql
mysql-connector-java
5.1.32
runtime
com.alibaba
druid
1.0.9
org.mybatis
mybatis
3.4.1
pagehelper
com.github.pagehelper
4.1.6
org.mybatis
mybatis-spring
1.3.0
taglibs
standard
1.1.2
jstl
jstl
1.2
com.fasterxml.jackson.core
jackson-databind
2.8.2
com.alibaba
fastjson
1.2.17
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-core
4.3.2.RELEASE
org.springframework
spring-beans
4.3.2.RELEASE
org.springframework
spring-context
4.3.2.RELEASE
org.springframework
spring-jdbc
4.3.2.RELEASE
org.springframework
spring-tx
4.3.2.RELEASE
org.springframework
spring-web
4.3.2.RELEASE
org.springframework
spring-webmvc
4.3.2.RELEASE
org.springframework
spring-test
4.3.2.RELEASE
redis.clients
jedis
2.9.0
com.dyuproject.protostuff
protostuff-core
1.0.10
com.dyuproject.protostuff
protostuff-runtime
1.0.10
commons-collections
commons-collections
3.2.1
com.alibaba
page
1.0
0xi-lifehell
org.apache.maven.plugins
maven-resources-plugin
2.7
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
/
8080
org.mybatis.generator
mybatis-generator-maven-plugin
true
false
mysql
mysql-connector-java
5.1.32
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
配置mybatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置全局属性-->
<settings>
<!-- 使用jdbc的getGeneratedKeys 获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列别名替换列名 默认:true
select name as title from table
-->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换 Table(create_time) -> Entity(createTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定使用的数据库 -->
<property name="dialect" value="mysql" />
</plugin>
</plugins>
</configuration>
配置jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/webapp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=PRC&useSSL=false&zeroDateTimeBehavior=convertToNull
jdbc.user=root
jdbc.password=root
showsql=true
配置spring-dao.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合Mybatis过程 -->
<!-- 1.配置数据库相关参数 properties的属性: ${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!-- 约定大于配置 -->
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描entity包 使用别名 (value间使用分号分隔)-->
<!--<property name="typeAliasesPackage" value="com.oxi.entity"/>-->
<!-- 扫描SQL配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置扫描DAO接口包,动态实现DAO接口,注入到Spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出扫描DAO接口包 -->
<property name="basePackage" value="com.test.dao"/>
</bean>
</beans>
spring-service.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描service 包下所有注解的类型 -->
<context:component-scan base-package="com.test.service"/>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置基于注解的声明式事务
默认使用注解来管理事务行为
使用注解控制事务的优点:
1.开发团队达成一致约定,明确标注事务方法的编程风格.
2.保证事务方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求或者剥离到事务方法外部.保证实务操作干净清晰.
3.不是所有的方法都需要事务.如只有一条修改操作的,只读操作不需要事务控制。(mysql行级锁)
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解模式 -->
<!-- 简化配置:
(1).自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
(2).提供一系列:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,
xml,json默认读写支持
-->
<mvc:annotation-driven/>
<!-- servlet-mapping 映射路径:"/" -->
<!-- 2.静态资源默认servlet配置
(1).加入对静态资源的处理:js/gif/png
(2).允许使用"/"做整体映射
-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.oxi.web"/>
</beans>