MybatisPlus配置及使用
环境如下:
SpringBoot2.6.4
Mysql8.0
Idea 2020.3
一、添加依赖
<!-- mybatisPlus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、配置
application.yml文件
spring:
datasource:
#数据源类型
type: com.zaxxer.hikari.HikariDataSource
#配置连接信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三、创建类和接口
User实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
UserMapper接口
@Repository
public interface UserMapper extends BaseMapper<User> {
}
SpringBoot主程序类
@SpringBootApplication
@MapperScan("com.snow.mybatisplus.mapper")
public class MybatisPlusDemo1Application {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemo1Application.class, args);
}
}
四、测试
测试类
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
查询功能
/**
* 测试查询功能
*/
@Test
public void testSelect(){
//查询全部记录
/*List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);*/
//根据id查询
/*User user = userMapper.selectById(1);
System.out.println(user);*/
//根据map条件查询
Map<String,Object> map = new HashMap<>();
map.put("name","小刘");
List<User> list = userMapper.selectByMap(map);
list.forEach(System.out::println);
}
添加功能
/**
* 测试添加功能
*/
@Test
public void testInsert(){
User user = new User();
user.setName("小王");
user.setAge(22);
user.setEmail("wang@qq.com");
int insert = userMapper.insert(user);
System.out.println("受影响的行数:"+insert);
System.out.println(user.getId());
}
删除功能
/**
* 测试删除功能
*/
@Test
public void testDelete(){
//根据id删除
// int delete = userMapper.deleteById(1506133530385633281L);
//根据实体类删除
User user = new User();
user.setName("小王");
int count = userMapper.deleteById(user);
//根据id批量删除
// int deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(1506134546514542593l,1506134570237485058l));
//根据map集合设置的条件删除(必须全满足)
/*Map<String,Object> map = new HashMap<>();
map.put("age",25);
map.put("name","小王");
int count = userMapper.deleteByMap(map);*/
System.out.println("受影响的行数:"+count);
}
修改功能
/**
* 测试修改功能
*/
@Test
public void testUpdate(){
User user = new User();
user.setId(1506135641521471490L);
user.setName("小刘");
user.setAge(19);
int count = userMapper.updateById(user);
System.out.println(count);
}
}
五、常用注解
-
@TableName
在实体类类型上添加@TableName("t_user")
,标识实体类对应的表 -
@tableId
- value属性
在实体类中属性上通过@TableId
将其标识为主键 - type属性
type属性用来定义主键策略,常用主键策略:
可以直接在全局配置中配置主键策略
- value属性
#mybatis-plus配置
mybatis-plus:
#配置全局主键策略
global-config:
db-config:
# 配置mybatis-plus的主键策略
id-type: auto
# 配置mybatis-plus操作表的前缀
# table-prefix: t_
-
@tableField
若实体类中的属性和表中的字段不一致时,在实体类属性上使用@TableField("username")
设置属性所对应的字段名 -
@tableLogic
在是否删除字段上添加@tableLogic
注解实现逻辑删除功能。此时删除功能和查询功能都是加上是否删除的条件
User实体类:
@TableName("user")
public class User {
@TableId(value = "id")
private Long id;
@TableField(value = "user_name")
private String name;
private Integer age;
private String email;
@TableLogic
private Integer isDeleted;
}
本文作者:香酥豆腐皮
本文链接:https://www.cnblogs.com/Snowclod/p/16039012.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步