网站首页 > 文章精选 正文
前言
Mybatis-Plus是一个优秀的Mybatis增强工具。Mybatis-Plus原生提供了很多单表操作的方法,极大简化了繁琐的curd的操作,同时又支持xml配置、自定义sql的编写。这篇文章介绍SpringBoot集成Mybatis-Plus,同时介绍使用easyCode通过指定的数据库表生成对应的bean、mapper.xml、mapper.java、service.java、serviceImpl.java和controller。
pom文件引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
配置mybatis-plus
#mybatis-plush配置
mybatis-plus:
type-aliases-package: com.example.demo.pojo
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰模式
配置druid数据源
spring:
datasource:
# 配置数据源
driver-class-name: com.mysql.jdbc.Driver
# 使用druid连接池
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=true
username: root
password: root
application.java配置@MapperScan
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
使用easyCode插件生成的pojo如下
/**
* (User)实体类
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User implements Serializable {
private static final long serialVersionUID = 625687871874587410L;
@TableId(type = IdType.AUTO)
private Integer userId;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
private Date createTime;
private Date updateTime;
/**
* 1:删除,0:正常
*/
private Integer isDelete;
}
生成的dao.java和service和serviceImpl分别如下
public interface UserDao extends BaseMapper<User> {
}
public interface UserService{
}
@Slf4j
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {
}
生成的mapper.xml
<?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.example.demo.dao.UserDao">
<resultMap type="com.example.demo.pojo.User" id="UserMap">
<result property="userId" column="user_id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="password" column="password" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
</resultMap>
</mapper>
mybatis-plus单表crud
@Autowired
private MpUserService mpUserService;
@Test
public void test() {
// 插入新记录
User mpUser = new MpUser();
mpUser.setId(1L);
mpUser.setOpenid("openId");
mpUser.setAddress("广东深圳");
mpUser.setUsername("David Hong");
mpUserService.save(mpUser);
// 或者
mpUser.insertOrUpdate();
// 更新完成后,mpUser对象的id会被补全
log.info("mpUser={}", mpUser.toString());
// 通过主键id查询
mpUser = mpUserService.getById(8);
log.info("mpUser={}", mpUser.toString());
// 条件查询,下面相当于xml中的 select * from mp_user where address = '"广东深圳' and username = 'David Hong' limit 1
mpUser = mpUserService.getOne(new QueryWrapper<MpUser>().eq("address", "广东深圳").eq("username", "David Hong").last("limit 1"));
// 批量查询
List<MpUser> mpUserList = mpUserService.list();
// 分页查询
int pageNum = 1;
int pageSize = 10;
IPage<MpUser> mpUserIPage = mpUserService.page(new Page<>(pageNum, pageSize), new QueryWrapper<MpUser>().eq("openid", "openId"));
// IPage to List
List<MpUser> mpUserList1 = mpUserIPage.getRecords();
// 总页数
long allPageNum = mpUserIPage.getPages();
// 修改更新
mpUser.setAddress("广东广州");
mpUserService.updateById(mpUser);
// 或者
mpUser.insertOrUpdate();
// 通过主键id删除
mpUserService.removeById(1);
// 或者
mpUser.deleteById();
}
看完觉得还不错可以关注一下!欢迎转发,点赞!
猜你喜欢
- 2025-06-09 PageHelper - 最方便的 MyBatis 分页插件
- 2025-06-09 50个Java编程技巧,免费送给大家(java编程基础知识入门)
- 2025-06-09 SpringBoot 各种分页查询方式详解(全网最全)
- 2025-06-09 SpringBatch - R&W, 我与富婆的这一年
- 2025-06-09 面试官:说说MyBatis分页插件(PageHelper)工作原理和配置过程?
- 2025-06-09 面试二:pagehelper是怎么实现分页的,
- 2025-06-09 SpringBoot集成Mybatis-Plus分页插件
- 2025-06-09 【开发技术】Mybatis中进行多表关联查询?性能是不是会变好呢?
- 2025-06-09 每天从外包系统同步百万数据,用什么方案?Java实战讲解
- 2025-06-09 MyBatis插件开发实战:手写一个分页插件
- 最近发表
-
- 面试中常被问到的Hash表,你了解吗
- JAVA面试考点:一文搞懂一致性Hash的原理和实现
- 一次性搞清楚equals和hashCode(hashcode() 与equals()区别,简单说明)
- HashMap.Key的故事:Key为什么出现Hash碰撞及冲突呢?
- hash冲突的几种解决方案对比(hash冲突的解决方式)
- 游戏王LN 无头骑士(无头骑士cv)
- Linux ln、unlink命令用法(linux link命令详解)
- n和l分不清矫正发音方法,这三步就够了
- golang引用私有gitlab项目代码(golang引入当前包下的文件)
- Instamic:录音领域中的 GoPro,让你想录就录,随心所欲
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)