网站首页 > 开源技术 正文
前言
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-09-14 Redis集群搭建很easy(redis5.0.8集群搭建)
- 2024-09-14 带gui界面的mybatis代码生成工具-spring boot 手脚架
- 2024-09-14 P2-2 springboot整合mybatis(springboot怎么整合mybatis)
- 2024-09-14 spring boot 项目中自动执行 sql 语句
- 2024-09-14 Intellij-Idea 好用的插件(idea2021插件)
- 2024-09-14 IntelliJ IDEA一个实用的插件(idea2019插件)
- 2024-09-14 Spring bean 加载顺序导致的 bug 问题
- 2024-09-14 今日给大家推荐idea的一系列开发插件
- 2024-09-14 程序员都在用的 IDEA 插件(不断更新)
- 2024-09-14 第二弹!安排!安利几个让你爽到爆的IDEA必备插件
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)