微服务 教程
1085个小节阅读:195.9k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建Mapper接口。
使用MyBatis时,在编写Mapper接口后,需要手动编写CRUD方法,并需要在Mapper映射文件中手动编写每个方法对应的SQL语句。而在MyBatisPlus中,只需要创建Mapper接口并继承BaseMapper,此时该接口获得常用增删改查功能,不需要自己手动编写Mapper配置文件
xxxxxxxxxx
public interface StudentMapper extends BaseMapper<Student> {
}
创建Spring配置文件applicationContext.xml
xxxxxxxxxx
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///school"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- MyBatisPlus提供的session工厂对象-->
<bean id="sqlSessionFactory"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 自动扫描所有mapper ,将mapper接口生成代理注入到Spring-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.itbaizhan.mapper"
p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
</beans>
测试Mapper方法
xxxxxxxxxx
SpringJUnit4ClassRunner.class) (
locations = "classpath:applicationContext.xml") (
public class StudentMapperTest {
private StudentMapper studentMapper;
public void testFindById(){
Student student = studentMapper.selectById(1);
System.out.println(student);
}
}