1.集成spring

1.写spring配置文件

配置文件放在main下的resources文件夹下
新建spring配置文件spring-context,在这个文件配置扫描业务层

1
2
<!--扫描业务层组件-->
<context:component-scan base-package="com.zxy.sample.server"/>

2.配置spring容器的监听器

在web.xml文件里面配置spring容器的监听器来启动spring容器,如:

1
2
3
4
5
6
7
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-context.xml</param-value>
</context-param>

2.集成c3p0数据源

1.添加依赖c3p0和jdbc-mysql

打开maven依赖库网站搜索c3p0、mysql
搜索 com.mchange 包下的c3p0,选择用的比较多的框架复制到pom.xml文件下的dependencies下
搜索 mysql » mysql-connector-java 包下的mysql,选择用的比较多的框架复制到pom.xml文件下的dependencies下

2.在新建的spring配置文件spring-context中建立数据源集成c3p0,如:

1
2
3
4
5
6
7
8
9
10
11
12
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!--数据库名字sample-->
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/sample"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!--数据池最小值-->
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="20"/>
<property name="initialPoolSize" value="10"/>
<property name="acquireIncrement" value="1"/>
</bean>

3.配置数据库事务管理

1.添加依赖spring-tx和spring-jdbc,复制到pom.xml文件下的dependencies下

2.配置数据源事务管理

1
2
3
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

3.使用注解控制事务

1
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>

4.集成mybatis

1.添加依赖mybatis,mybatis-spring,分页pagehelper,动态代理cglib

2.写mybatis配置文件

在main下的resources文件夹下新建mybatis文件夹,在文件夹下新建mybatis-config文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="defaultStatementTimeout" value="3000"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="proxyFactory" value="CGLIB"/>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<!-- 分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="true"/>
<property name="rowBoundsWithCount" value="true"/>
<property name="pageSizeZero" value="true"/>
</plugin>
</plugins>

3.在spring-context.xml文件中配置SqlSessionFactoryBean来指定数据源、mybatis配置文件、mapper文件路径

1
2
3
4
5
6
7
8
9
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xml</value>
</list>
</property>
</bean>

4.在spring-context.xml文件中配置扫描mapper生成的dao文件

1
2
3
4
5
<!--扫描生成所有的dao层对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zxy.sample.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>