关于spring整合mybatis


第一步导入依赖



org.mybatis
mybatis
3.5.7


junit
junit
4.12
test


mysql
mysql-connector-java
5.1.46


log4j
log4j
1.2.17


org.springframework
spring-context
5.2.10.RELEASE


org.springframework
spring-jdbc
5.2.10.RELEASE


org.mybatis
mybatis-spring
2.0.7



org.springframework
spring-tx
5.2.5.RELEASE


org.aspectj
aspectjweaver
1.8.13


org.springframework
spring-webmvc
5.2.10.RELEASE


com.alibaba
druid
1.1.16

第二部获取必要的bean以及applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
















需要实现接口类如



 另一种使用继承SQLSessionDaoSupport的方式

不需要引入SQLSessionTemplate




 直接扫描接口的方式(推荐方式)

需要service层的接口和dao层接口有相同方法,但不一定全部实现(不需要extend已经implement),dao层里面的@param注解也不需要,然后在由service层的实现类注入dao层接口


    class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        

        
    

 然后就是编写测试类

@Test
public void testAnn(){
//获取ioc容器
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
Tb_brandMapper tb_brandMapper = (Tb_brandMapper) Context.getBean("tb_brandMapperImpl");
    //Tb_brandMapper tb_brandMapper = (Tb_brandMapper) Context.getBean("tb_brandMapperImpl2");

List tb_brands = tb_brandMapper.selectAll();
for (Tb_brand tb_brand : tb_brands) {
System.out.println(tb_brand);
}

}


声明事务