0,介绍两种方式,一种是单独的Mybatis包,一种是用spring-mybatis整合包。
1,先用第一种,pom.xml导入mybatis依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency><!--mybatis 3.5.6依赖-->
2,使用java配置mybatis:先配置DataSource,然后配置SqlSessionFactory ,最后配置SqlSession (切记,所有的***mapper.xml和***mapper.java必须要相同的前缀,这是因为mybatis会按照这个路径去匹配mapper接口和XML文件):
如下图,作者的所有mapper接口都在mybatis包下,所有的mapper.xml也在同名的mybatis下(idea的resource中):
@Configuration
@Component
public class MybatisConfig {
@Bean
public PooledDataSource pooledDataSource(){
PooledDataSource pooled= new PooledDataSource() ;
pooled.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
pooled.setPassword("123456");
pooled.setUsername("xmr");
pooled.setDriver("oracle.jdbc.driver.OracleDriver");
return pooled;
}
@Bean
public SqlSessionFactory sqlSessionFactory(){
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, pooledDataSource());
Configuration configuration = new Configuration(environment);
configuration.addMappers("mybatis");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
return sqlSessionFactory;
}
@Bean
public SqlSession sqlSession(SqlSessionFactory sqlSessionFactory){
SqlSession sqlSession=sqlSessionFactory.openSession();
return sqlSession;
}
}
3,mapper接口和mapper.xml实例:
package mybatis;
public interface StudentMapper {
String selectAll(int age);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="mybatis.StudentMapper">
<select id="selectAll" resultType="java.lang.String">
select name from student where age=#{age}
</select>
</mapper>
4,第一种总结:
|
1,声明一个DataSource的bean; 2,声明一个SqlSessionFactory 的bean,并注入下列三个依赖组件: TransactionFactory,Environment,DataSource 3,声明一个sqlSession,并注入SqlSessionFactory依赖(其实这个sqlSession是从SqlSessionFactory当中获取的,因此可以不用声明这个sqlSession,但是呢,如果不声明,后面也会多一行代码来调用openSession()方法来获取,因此还是在这声明比较好些) 4,通过转型,从sqlSession.getMapper()方法来获取相应的对象,就能执行接口中的方法了(mybatis为这个接口实现了代理类,就能执行其中的方法了) |
二,第二种,引入依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
1,设置javaconfig:
@Component
public class MybatisConfig {
@Bean //数据源
public PooledDataSource pooledDataSource(){
PooledDataSource pooled= new PooledDataSource() ;
pooled.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
pooled.setPassword("123456");
pooled.setUsername("xmr");
pooled.setDriver("oracle.jdbc.driver.OracleDriver");
return pooled;
}
@Bean //只需要数据源
public SqlSessionFactory sqlSessionFactory() throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(pooledDataSource());
return sqlSessionFactoryBean.getObject();
}
@Bean //映射器,映射mybatis_spring包下的所有mapper接口以及同名路径下的mapper.xml
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer MSC=new MapperScannerConfigurer();
MSC.setBasePackage("mybatis_spring");
return MSC;
}
@Bean //从中能够获取mapper接口的实现类
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(sqlSessionFactory);
return sqlSessionTemplate;
}
2,调用mapper接口实现访问数据库:
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext an=
new AnnotationConfigApplicationContext(MybatisConfig.class);
SqlSessionTemplate sqlSessionTemplate=(SqlSessionTemplate)an.getBean("sqlSessionTemplate");
StudentMapper studentMapper=(StudentMapper)sqlSessionTemplate.getMapper(StudentMapper.class);
System.out.println(studentMapper.selectAll(21));
}
}
3,mapper接口及mapper.xml实例同第一种方法,只是mybatis包名换成了mybatis_spring,mapper.xml所在的mybatis包也换成了mybatis_spring(注意mapper.xml中的<namesapce>也需要变为实际的)。

Spring整合MyBatis JavaConfig配置详解
本文介绍了如何在Spring中整合MyBatis,包括两种方式:单独的Mybatis包和使用spring-mybatis整合包。详细讲解了使用JavaConfig配置Mybatis的过程,包括DataSource、SqlSessionFactory和SqlSession的配置,以及mapper接口和XML文件的匹配规则。同时,提供了引入spring-mybatis整合包的配置步骤,并展示了调用mapper接口进行数据库操作的方法。
563

被折叠的 条评论
为什么被折叠?



