MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

SpringBoot集成mybatis-plus(springboot集成Elasticsearch)

2025-04-10 23:12 huorong 精选文章 9 ℃ 0 评论

前言

Mybatis-Plus是一个优秀的Mybatis增强工具。Mybatis-Plus原生提供了很多单表操作的方法,极大简化了繁琐的curd的操作,同时又支持xml配置、自定义sql的编写。这篇文章介绍SpringBoot集成Mybatis-Plus,同时介绍使用easyCode通过指定的数据库表生成对应的bean、mapper.xml、mapper.java、service.java、serviceImpl.java和controller。


项目结构


pom文件引入依赖


	mysql
	mysql-connector-java


	com.alibaba
	druid-spring-boot-starter
	1.1.12


	com.baomidou
	mybatis-plus-boot-starter
	3.0.6

配置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 {
}

public interface UserService{
}

@Slf4j
@Service("userService")
public class UserServiceImpl extends ServiceImpl implements UserService {
}

生成的mapper.xml





    
        
        
        
        
        
        
    
    

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().eq("address", "广东深圳").eq("username", "David Hong").last("limit 1"));
        // 批量查询
        List mpUserList = mpUserService.list();
        // 分页查询
        int pageNum = 1;
        int pageSize = 10;
        IPage mpUserIPage = mpUserService.page(new Page<>(pageNum, pageSize), new QueryWrapper().eq("openid", "openId"));
        // IPage to List
        List mpUserList1 = mpUserIPage.getRecords();
        // 总页数
        long allPageNum = mpUserIPage.getPages();
        
        // 修改更新
        mpUser.setAddress("广东广州");
        mpUserService.updateById(mpUser);
        // 或者
        mpUser.insertOrUpdate();

        // 通过主键id删除
        mpUserService.removeById(1);
        // 或者
        mpUser.deleteById();
    }

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

Tags:mybatis-plus删除

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言