24.springboot整合mybatis
步骤:
1.引入mybatis的stater
<!--导入mybatis的stater依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
导入的文件
配置版
1.在springboot的配置文件中yaml中配置数据源信息和mybatis的配置文件信息
重点1:数据源的连接信息
spring:
datasource:
url: jdbc:mysql://192.168.2.129:3306/mybatis
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 521521
mybatis的配置
mybatis:
#配置mybatis主配置文件的位置
config-location: classpath:mybatis\mybatis-config.xml
#配置mybatis的mapper.xml的位置
mapper-locations: classpath:mybatis\mapper\*.xml
#配置别名包
type-aliases-package: com.cn.springboot.mybatis.entity
#其他配置可以完全查看源码:MybatisAutoConfiguration中的配置文件绑定
2.启动类代码:
@SpringBootApplication
重点2:加上@MapperScan标签指定mybatis的接口类位置,这样就不用在mybatis的主配置文件中设置
@MapperScan(value = "com.cn.springboot.mybatis.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
3.接口层的代码:
重点3:接口层上并没有加注解
public interface PersonMapper {
@Select("select * from person where id=#{id}")
Person queryPersonById(@Param("id") int id);
Person queryPersonByName(@Param("name") String name);
}
4.控制层代码:
@RestController
public class PersonController {
重点4:使用依赖注入(接口层上未加注解,但此处可以依赖注入成功)
@Autowired
private PersonMapper personMapper;
@RequestMapping("/getPerson/id/{id}")
public Person getPersonById(@PathVariable("id")int id){
Person person = personMapper.queryPersonById(id);
return person;
}
@RequestMapping("/getPerson/name/{name}")
public Person getPersonByname(@PathVariable("name") String name){
return personMapper.queryPersonByName(name);
}
}
这两个方法均可以正常查询返回!
项目结构如下
注解版
样例:
1.在每个dao层接口上加上:@mapper注解,这样就不需要在mybatis的主配置文件中设置dao层接口位置和mapper.xml位置
@Mapper
public interface UserMapper{
@Select("select * from public.user where id=${id}")
User selectById(@Param("id") int id);
}
2.如果dao层接口类比较多时,可以在springboot启动类位置使用@MapperScan(value = "com.cn.springboo.dao")去指定,这时接口类上就不需要加上@mapper标签了
2.1启动类代码:
@SpringBootApplication
重点1:指定dao层接口类的文件夹路径
@MapperScan(value = "com.cn.springboo.dao")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
2.2接口类上就不需要加上@mapper标签了
public interface UserMapper{
@Select("select * from public.user where id=${id}")
User selectById(@Param("id") int id);
}
3.当然注解版和配置版可以配合使用,不过需要在springboot的yaml配置文件中指定mybatis的主配置文件位置和mapper.xml配置文件位置
mybatis-plus:
config-location: classpath:mybatis\mybatis-config.xml
mapper-locations: classpath*:mybatis\*.xml
mybatis-plus的使用
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
里面的basermapper里封装了基本的CRUD
1.导包:导入mybatis-plus的包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
2.接口层代码:接口直接继承BaseMapper的接口,BaseMapper接口中封装了基本的CRUD操作
public interface PetMapper extends BaseMapper<Pet> {
}
3.BaseMapper接口中的方法如下:
public interface BaseMapper<T> extends Mapper<T> {
/**
* 插入一条记录
*
* @param entity 实体对象
*/
int insert(T entity);
/**
* 根据 ID 删除
*
* @param id 主键ID
*/
int deleteById(Serializable id);
/**
* 根据 columnMap 条件,删除记录
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,删除记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 删除(根据ID 批量删除)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/**
* 根据 ID 修改
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity);
/**
* 根据 whereEntity 条件,更新记录
*
* @param entity 实体对象 (set 条件值,可以为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);
/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/**
* 查询(根据 columnMap 条件)
*
* @param columnMap 表字段 map 对象
*/
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,查询一条记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
* <p>注意: 只返回第一个字段的值</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
<P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}
分析如下:
基本的CRUD方法中传入的参数有以下几种:
1.id(标中唯一确认记录的主键:如果绑定实体类的字段名称和表中主键字段id不一致时,需要使用标签@TableId去对应,后面详解)
2.map(传入的参数时map,会将map中的key拼入where条件中,并且各个字段之间是and的关系)
3.Wrapper对象,调用对象中的方法去类比sql中的操作,如图,用法如下
实体类的代码:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName("pet")
public class Pet {
@TableField("id")
@TableId("id")
private int petid;
private String name;
private String type;
private int masterId;
}
注解:
@Tableld:属性与主键的映射关系。
@TableField:列与属性的映射关系。
因为在接口层直接为定义方法直接调用的是BaseMapper中的方法,如
接口层代码:
重点1:指定BaseMapper的泛型,即操作的表对应的实体类
public interface PetMapper extends BaseMapper<Pet> {}
控制层代码
@RequestMapping("/getPet/{id}")
public Pet getPet(@PathVariable("id")int id){
//重点1:调用的是BaseMapper中的selectById方法,所以在实体类中必须指定实体类中的id字段和表中主键的对应:即使用@Tableld("id")标签,即where语句后的条件
return petMapper.selectById(id);
这样执行的sql是:SELECT id AS petid,name,type,master_i FROM pet WHERE id=1;
//重点2:因实体类中的字段是petid和id字段对应,所以需要使用@TableField("id")标签,起的作用是SELECT id AS petid
}
建议:简单的sql可以使用mybatis-plus中的基本CRUD,复杂的尽量自己写!
分类:
springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?