网站首页 > 教程文章 正文
前言
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();
}
看完觉得还不错可以关注一下!欢迎转发,点赞!
猜你喜欢
- 2024-12-25 mybatis-plus-join编码实现Join联表查询,真香!
- 2024-12-25 SpringBoot咋使用PageHelper实现数据分页?
- 2024-12-25 MyBatis-Plus中如何使用ResultMap
- 2024-12-25 通过Mybatis Plus实现代码生成器,常见接口实现讲解
- 2024-12-25 MyBatis-Plus码之重器 lambda 表达式使用指南,开发效率瞬间提升80%
- 2024-12-25 MyBatis Plus—CRUD 接口 mybatis plus typehandler
- 2024-12-25 聊聊关于Mybatis分页操作PageHelper及实现原理?
- 2024-12-25 Springboot+MybatisPlus实现用户CRUD操作(后端实现)
- 2024-12-25 MybatisPlus方法详细使用,实现无SQL式开发
- 2024-12-25 mybatis-plus 团队新作 mybatis-mate 轻松搞定企业级数据处理
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)