利用Spring 集成MyBatis 创建所需要的Mapper对象
spring集成MyBatis官方文档: mybatis.org/spring/zh/f…
一、传统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 文件和相关依赖即可。
下载地址: repo.spring.io/release/org…
maven项目:
如果使用 Maven 作为构建工具,仅需要在 pom.xml 中加入以下代码即可:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
12345
复制代码
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
1234
复制代码
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>
12345678910111213141516171819202122232425262728293031323334353637383940
复制代码
-
mybatis-config.xml (mybatis本身的配置文件)
123456789101112131415161718192021222324252627282930313233
-
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);
}
}
1234567891011121314
复制代码
总结:
- 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
也欢迎大家关注我的公众号:Java程序员聚集地,麦冬每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!