关于spring整合mybatis
第一步导入依赖
第二部获取必要的bean以及applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
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");
Listtb_brands = tb_brandMapper.selectAll();
for (Tb_brand tb_brand : tb_brands) {
System.out.println(tb_brand);
}
}
声明事务