Spring整合Mybatis
这个因为今天mybatis-spring官网进不了,也总结不出来什么,但是这个整合也没有什么,几乎就是代码吧。
- 导入需要的包(junit,mybatis,mysql,spring相关,aop织入,mybatis-spring)
<!--导入mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
<!--spring操作数据库的话,还需要导入spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.4</version>
</dependency>
<!-- AOP织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--mybatis整合spring的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
- 将sqlsessionfactory配置到spring的bean中
<!-- 配置DataSource:使用spring的数据源替换mybatis的配置
这里我们使用spring提供的JDBC:
org.springframework.jdbc.datasource.DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="XXX"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
- 将sqlSession配置到spring的bean中
<!--配置sqlSession
SqlSessionTemplate:mybatis整合spring中的类,就相当于原来的sqlSession
-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
-
测试
- Teacher类
public class Teacher { private int id; private String tname; }
- 直接测试
@Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); SqlSession sqlSession = (SqlSession) context.getBean("sqlSession"); TeacherDao mapper = sqlSession.getMapper(TeacherDao.class); for (Teacher teacher : mapper.getAll()) { System.out.println(teacher); } }
还有一种使用TeacherDao的实现类来完成
- TeacherDaoImpl
public class TeacherDaoImpl implements TeacherDao{ private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } @Override public List<Teacher> getAll() { return sqlSession.getMapper(TeacherDao.class).getAll(); } }
- 需要将这个实现类注册在spring容器中
<bean id="impl" class="com.yxx.dao.TeacherDaoImpl"> <property name="sqlSession" ref="sqlSession"/> </bean>
- 测试
@Test public void test3(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); TeacherDao impl = (TeacherDao) context.getBean("impl"); for (Teacher teacher : impl.getAll()) { System.out.println(teacher); } }