myBatis与Spring框架整合
myBatis中文指导手册地址:
https://github.com/mybatis/spring/tree/master/src/site/zh/markdown
版本依赖关系
1.添加依赖
pom.xml文件中添加依赖
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--druid->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
2. spring配置文件中配置
配置bean :SqlSessionFactoryBean,需要配置连接池数据源
<!--整合myBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置myBatis的数据源-->
<property name="dataSource" ref="dataSource" />
<!--configLocation用于指定全局配置文件 (一般这里里面的配置是通过直接在这个bean上面来注入) -->
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
<!--mapper映射配置文件的位置: 可以利用Spring的资源表示法(通配符)-->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
设置myBatis自动扫描,base-package表示要扫描的位置,annotation表示扫描的注解类型,默认不设置,因为Mapper对象都是基于插件自动创建出来的
<mybatis:scan base-package="com.example.mappers" annotation="org.apache.ibatis.annotations.Mapper">
</mybatis:scan>
整体配置文件:
<?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"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描service访问-->
<context:component-scan base-package="com.example.service"></context:component-scan>
<!--加载jdbc配置文件-->
<context:property-placeholder location="classpath:properties/jdbc-info.properties"></context:property-placeholder>
<!--放置数据源连接池对象-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--将配置文件参数传入连接池-->
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
<!--将Spring支持的jdbcTemplate放入容器中(使用myBatis可以不配)-->
<bean name="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource" ></constructor-arg>
</bean>
<!--事务管理器-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务管理驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--整合myBatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置myBatis的数据源-->
<property name="dataSource" ref="dataSource" />
<!--configLocation用于指定全局配置文件 (一般这里里面的配置是通过直接在这个bean上面来注入) -->
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
<!--mapper映射配置文件的位置: 可以利用Spring的资源表示法(通配符)-->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<!--mvc扫描mapper层,自动注册到spring容器中-->
<mybatis:scan base-package="com.example.mapper" />
</beans>