mybatis-spring 整合

Mybatis-Spring整合

     参考文档:http://www.mybatis.org/spring/

     下载地址:https://github.com/mybatis/spring/releases

第一步准备JAR包 (共25个)

Spring 核心 6个 jar
spring-aop-5.0.6.RELEASE.jar
spring-beans-5.0.6.RELEASE.jar
spring-context-5.0.6.RELEASE.jar
spring-expression-5.0.6.RELEASE.jar
spring-core-5.0.6.RELEASE.jar
commons-logging-1.2.jar

Spring Aop 需要的 jar
aopalliance-1.0.jar
aspectjweaver-1.9.1.jar
spring-aspects-5.0.6.RELEASE.jar

Spring 事物处理 jar
spring-tx-5.0.6.RELEASE.jar
spring-jdbc-5.0.6.RELEASE.jar

mybatis 核心
mybatis-3.4.6.jar

mybatis 缓存
ehcache-core-2.6.8.jar
mybatis-ehcache-1.0.3.jar

mybatis 逆向工程
mybatis-generator-core-1.3.7.jar

mybatis 分页插件
jsqlparser-0.9.5.jar
pagehelper-5.1.4.jar

日志文件
log4j-1.2.17.jar
slf4j-api-1.7.25.jar(这个有许多jar包与之关联)
slf4j-log4j12-1.7.25.jar

mysql jar
mysql-connector-java-5.1.8-bin.jar

c3p0 连接池
c3p0-0.9.5.2.jar
mchange-commons-java-0.2.11.jar

mybatis与spring整合 核心 jar
mybatis-spring-1.3.2.jar
spring-orm-5.0.6.RELEASE.jar

整合方式有两种:

第一种 放弃mybatis的 mybatis-config.xml配置文件,使用spring的applicationContext.xml统一配置

1 driverClass=com.mysql.jdbc.Driver
2 jdbcUrl=jdbc:mysql://127.0.0.1:3306/javaweb_emp?useUnicode=true&characterEncoding=utf-8
3 user=root
4 password=123456
db.properties
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:util="http://www.springframework.org/schema/util"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans
10         http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context.xsd
13         http://www.springframework.org/schema/util 
14         http://www.springframework.org/schema/util/spring-util.xsd
15         http://www.springframework.org/schema/aop
16         http://www.springframework.org/schema/aop/spring-aop.xsd
17         http://www.springframework.org/schema/tx
18         http://www.springframework.org/schema/tx/spring-tx.xsd">
19     <!-- 注解 检测 -->
20     <context:component-scan base-package="com.sm"></context:component-scan>
21     <!-- 开启 aop 注解驱动 -->
22     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
23     <!-- 配置c3p0 文件 使用db.properties配置 -->
24     <!-- 1.导入db.properties -->
25     <context:property-placeholder location="db.properties" />
26     <!-- 2.配置属性 -->
27     <bean id="dataSource"
28         class="com.mchange.v2.c3p0.ComboPooledDataSource">
29         <property name="driverClass" value="${driverClass}"></property>
30         <property name="jdbcUrl" value="${jdbcUrl}"></property>
31         <property name="user" value="${user}"></property>
32         <property name="password" value="${password}"></property>
33     </bean>
34     <!-- 配置Mybatis -->
35     <bean id="sqlSessionFactory"
36         class="org.mybatis.spring.SqlSessionFactoryBean">
37         <!-- 连接数据源 -->
38         <property name="dataSource" ref="dataSource"></property>
39         <!-- 设置别名 -->
40         <property name="typeAliasesPackage" value="com.sm.entity"></property>
41         <!-- mapper映射处理 -->
42         <property name="mapperLocations" value="com/sm/mapper/mapper/*.xml"></property>
43         <!-- 分页插件 配置 -->
44         <property name="plugins">
45             <array>
46                 <bean class="com.github.pagehelper.PageInterceptor">
47                     <property name="properties">
48                         <!--使用下面的方式配置参数,一行配置一个 -->
49                         <value>
50                             helperDialect=mysql
51                             reasonable=true
52                         </value>
53                     </property>
54                 </bean>
55             </array>
56         </property>
57     </bean>
58     <!-- 配置mapper映射 一次只能配置一个,如果有许多的mapper则非常繁琐,不推荐使用 -->
59     <!--<bean id="empMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
60         配置mapper接口路径 <property name="mapperInterface" value="com.sm.mapper.EmpMapper"></property> 
61         引入sqlSessionFactory <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 
62         </bean> -->
63 
64     <!-- 配置mapper映射 配置mapper扫描, 可以扫描多个mapper 推荐使用 -->
65     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
66         <property name="basePackage" value="com.sm.mapper" />
67         <property name="sqlSessionFactoryBeanName"
68             value="sqlSessionFactory" />
69     </bean>
70 
71     <!-- 配置管理器 -->
72     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
73         <property name="dataSource" ref="dataSource" />
74     </bean>
75 
76     <!-- 配置事务驱动,开启事务注解 -->
77     <tx:annotation-driven
78         transaction-manager="txManager" />
79 
80     <!-- 配置事务的通知属性 -->
81     <tx:advice id="txAdvice" transaction-manager="txManager">
82         <tx:attributes>
83             <!-- 查询时只读 -->
84             <tx:method name="find*" read-only="true" />
85             <!-- 所有的方法都有 isolation="DEFAULT" propagation="REQUIRED" 默认事务 -->
86             <tx:method name="*" />
87         </tx:attributes>
88     </tx:advice>
89     <!-- 配置aop -->
90     <aop:config>
91         <aop:pointcut
92             expression="execution(* com.sm.service.impl.*.*(..) )" id="pt" />
93         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
94     </aop:config>
95 </beans>
applicationContext.xml

第二种 在applicationContext.xml 中引入 mybatis-config.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:util="http://www.springframework.org/schema/util"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:context="http://www.springframework.org/schema/context"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans
10         http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context.xsd
13         http://www.springframework.org/schema/util 
14         http://www.springframework.org/schema/util/spring-util.xsd
15         http://www.springframework.org/schema/aop
16         http://www.springframework.org/schema/aop/spring-aop.xsd
17         http://www.springframework.org/schema/tx
18         http://www.springframework.org/schema/tx/spring-tx.xsd">
19     <!-- 注解 检测 -->
20     <context:component-scan base-package="com.sm"></context:component-scan>
21     <!-- 开启 aop 注解驱动 -->
22     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
23     <!-- 配置c3p0 文件 使用db.properties配置 -->
24     <!-- 1.导入db.properties -->
25     <context:property-placeholder location="db.properties" />
26     <!-- 2.配置属性 -->
27     <bean id="dataSource"
28         class="com.mchange.v2.c3p0.ComboPooledDataSource">
29         <property name="driverClass" value="${driverClass}"></property>
30         <property name="jdbcUrl" value="${jdbcUrl}"></property>
31         <property name="user" value="${user}"></property>
32         <property name="password" value="${password}"></property>
33     </bean>
34     <!-- 配置Mybatis -->
35     <bean id="sqlSessionFactory"
36         class="org.mybatis.spring.SqlSessionFactoryBean">
37         <!-- 连接数据源 -->
38         <property name="dataSource" ref="dataSource"></property>
39         <!-- 导入mybatis-config.xml文件 -->
40         <property name="configLocation" value="mybatis-config.xml"></property>
41     </bean>
42     <!-- 配置mapper映射 一次只能配置一个,如果有许多的mapper则非常繁琐,不推荐使用 -->
43     <!--<bean id="empMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
44         配置mapper接口路径 <property name="mapperInterface" value="com.sm.mapper.EmpMapper"></property> 
45         引入sqlSessionFactory <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 
46         </bean> -->
47 
48     <!-- 配置mapper映射 配置mapper扫描, 可以扫描多个mapper 推荐使用 -->
49     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
50         <property name="basePackage" value="com.sm.mapper" />
51         <property name="sqlSessionFactoryBeanName"
52             value="sqlSessionFactory" />
53     </bean>
54 
55     <!-- 配置管理器 -->
56     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
57         <property name="dataSource" ref="dataSource" />
58     </bean>
59 
60     <!-- 配置事务驱动,开启事务注解 -->
61     <tx:annotation-driven
62         transaction-manager="txManager" />
63 
64     <!-- 配置事务的通知属性 -->
65     <tx:advice id="txAdvice" transaction-manager="txManager">
66         <tx:attributes>
67             <!-- 查询时只读 -->
68             <tx:method name="find*" read-only="true" />
69             <!-- 所有的方法都有 isolation="DEFAULT" propagation="REQUIRED" 默认事务 -->
70             <tx:method name="*" />
71         </tx:attributes>
72     </tx:advice>
73     <!-- 配置aop -->
74     <aop:config>
75         <aop:pointcut
76             expression="execution(* com.sm.service.impl.*.*(..) )" id="pt" />
77         <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
78     </aop:config>
79 </beans>
applicationContext2.xml

猜你喜欢

转载自www.cnblogs.com/xiaoxingxingyu/p/9330906.html