SpringBoot_Mybatis MyBatisPlus
2024-08-30 12:25:10
一.SpringBoot中使用Mybatis
springBoot中使用mybatis跟以前spring中使用方法一样.
1.mybatis配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: ******
#mybatis配置
#需要配置别名的实体类的包
mybatis:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml
2.配置mapper接口.
package com.lanou.spring_bootm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;
public interface EmpMapper extends BaseMapper {
List<Emp> findAll(); }
3.配置mapper文件.
在resources文件夹下创建mapper包
在mapper包中创建mapper.xml来写sql语句.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lanou.spring_bootm.mapper.EmpMapper">
<select id="findAll" resultType="emp">
select uuid,username,birthday,email from emp
</select>
</mapper>
二.MybatisPlus
mybatisPlus是对mybatis的一个扩展.
使用mybatisplus直接将配置文件中mybatis改为mybatis-plus
mybatis-plus:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml
mybatis-plus中定义好了很多对数据库简单的增删改查,和分页功能
用法很简单,将mapper中的接口继承于BaseMapper接口,
使用该接口中的方法不需要经过mapper.xml文件,
package com.lanou.spring_bootm.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;
//BaseMapper是mybatisplus中的接口
public interface EmpMapper extends BaseMapper {
//自定义的查询方法,对应xml中的id
List<Emp> findAll(); }
mybatis-plus中的分页功能需要要在config文件中配置Bean
//在springBoot中习惯使用java的配置方式进行配置
//SSM项目中也能用java方式进行配置
//这个注解相当于给这个类配置成配置文件.
@Configuration
public class MyBatisConfig { //配置MybatisPlus中的分页拦截
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
} }
最新文章
- 爱上MVC系列~前端验证与后端数据有效性验证
- [Spring] - 读写分离
- java基本加密算法
- BZOJ 1266 上学路线route(最小割)
- BZOJ 2337 XOR和路径(高斯消元)
- java学习笔记_GUI(3)
- 定制属于自己的Chrome起始页
- HTML之学习笔记(七)列表
- excel导出、导入功能
- UVA 322 ships (POJ 1138)
- [poj2185]Milking Grid_KMP
- 分布式定时任务框架——python定时任务框架APScheduler扩展
- table表格固定前几列,其余的滚动
- 【Linux】gdb调试
- Linux内核读书笔记第六周
- 【leetcode】58-LengthofLastWord
- Java内存泄漏简单说明
- mysql mysqld.sock文件丢失问题
- JVM中OutOFMemory和StackOverflowError异常代码
- RBAC 介绍 (权限)