云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

SpringBoot集成mybatis-plus springboot集成mybatisplus的配置

jxf315 2024-12-25 12:45:40 教程文章 41 ℃

前言

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();
    }

看完觉得还不错可以关注一下!欢迎转发,点赞!

最近发表
标签列表