利用Spring 集成MyBatis 創建所需要的Mapper物件
spring集成MyBatis官方檔案: http://mybatis.org/spring/zh/factorybean.html
一、傳統Mybatis創建Mapper物件:
- sqlSessionFactory
- sqlSession
- UserMapper
二、利用Spring創建Mapper:
既然是利用Spring來創建Mybatis的Mapper代理物件;所以上面的步驟是不能少的,只是在Spring幫我們了,但是必要的組態檔還是需要我們來寫:
1、導包:
- 匯入mybatis包
- mysql驅動包
- mybatis-spring集成的包
- spring對jdbc/orm支持的包
- 其他包(log4j)

[注意] mybatis-spring的版本必須與Mybatis的版本要匹配,否則報錯
普通工程:
要使用 MyBatis-Spring 模塊:只需要在類路徑下包含 mybatis-spring-2.0.6.jar 檔案和相關依賴即可,
下載地址: https://repo.spring.io/release/org/springframework/spring/
maven專案:
如果使用 Maven 作為構建工具,僅需要在 pom.xml 中加入以下代碼即可:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
2、配置xml檔案:
- beans-datasource.xml(Spring生成mybatis物件 的xml檔案)
Properties檔案配置如下:
url=jdbc:mysql://localhost:3306/easybuy?useUnicode=true&&characterEncoding=utf-8
driver=com.mysql.jdbc.Driver
username2=root
password=123456
beans-datasource.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、加載資料庫的配置資訊 -->
<context:property-placeholder location="database.properties"/>
<!--2、datasource資料源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${username2}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 3、sqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 別名 -->
<property name="typeAliasesPackage" value="com.cc.model"></property>
<!-- mapper XML映射 -->
<property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property>
<!-- 資料源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- mapper包位置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>
<!-- 5、事務管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
- mybatis-config.xml (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>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="pojo"/>
<package name="vo"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easybuy?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="mapper"/>
</mappers>
</configuration>
- XXXMapper.xml(mybatis介面檔案)
(使用Mybatis逆行工程或手動寫sql陳述句的xml檔案)
3、撰寫測驗類:
/**
*@auther Nical
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-datasource.xml")
public class TestMyBatis {
@Autowired
UserMapper userMapper;
@test
public void testUser() {
List<User> users = userMapper.selectById(1);
System.out.println("users==>"+users);
}
}
總結:
- spring-database.xml中的${username}默認獲取的是win作業系統的用戶名
故:datasource.properties檔案 username=xxx 改為userName=xxxx - spring集成mybait的包的版本問題
mybatis-spring-1.2.2只能集成mybatis 3.2 版本 不能繼承最新的 3.5.5
需要maven倉庫獲取mybatis-spring-2.x的版本的jar
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/236572.html
標籤:java
上一篇:問題排查之JVM記憶體泄漏
