微服务 教程
1085个小节阅读:196.7k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
Spring提供了MapperScannerConfigurer对象,该对象可以自动扫描包创建代理对象,并将代理对象放入容器中,此时不需要使用SqlSession手动创建代理对象。
创建MapperScannerConfigurer对象
xxxxxxxxxx
<!-- 该对象可以自动扫描持久层接口,并为接口创建代理对象 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描的接口包 -->
<property name="basePackage" value="com.itbaizhan.dao"></property>
</bean>
Service类直接使用代理对象即可
xxxxxxxxxx
@Service
public class StudentService {
// 直接注入代理对象
@Autowired
private StudentDao studentDao;
// 直接使用代理对象
public void addStudent(Student student){
studentDao.add(student);
}
}
测试
xxxxxxxxxx
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
public void testAdd(){
Student student = new Student(0, "百战2", "男", "上海");
studentService.addStudent(student);
}
}
1.在Spring中整合MyBatis中,对象可以自动扫描包创建代理对象
A SqlSessionFactoryBean
B SqlSessionTemplate
C SqlSessionBuilder
D MapperScannerConfigurer
1=>D