Spring boot3 集成Mybatis-Plus
发布时间
阅读量:
阅读量
Mybatis-Plus:MyBatis 最佳搭档,只做增强不做改变,为简化开发、提高效率而生。
一、导入相关依赖
<!--web开发需要 为了写测试接口特加入了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus starter-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!--mysql 连接驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
二、yml配置
spring:
datasource:
# 数据库连接驱动
driver-class-name: com.mysql.cj.jdbc.Driver
# 数据库连接地址
url: jdbc:
# 数据库连接用户名
username: root
# 数据库连接密码
password:XXX
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三、mapper 、service、pojo代码
1、pojo
package com.ld.druid.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
//数据库表名称
@TableName(value = "contract_company_info")
public class SubCompany implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String companyType;
private String companyName;
private String companyNameSub;
private String idNoUrl2;
//数据库表不存在的字段
@TableField(exist = false)
private String showIdNoUrl1;
@TableField(exist = false)
private String reason;
//getter setter
}
2、mapper
package com.ld.druid.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ld.druid.pojo.SubCompany;
public interface SubCompanyDao extends BaseMapper<SubCompany>{
}
3、service
接口
package com.ld.druid.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ld.druid.pojo.SubCompany;
public interface ISubCompanyService {
SubCompany findById(Integer id);
}
实现类
package com.ld.druid.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ld.druid.dao.SubCompanyDao;
import com.ld.druid.pojo.SubCompany;
import com.ld.druid.service.ISubCompanyService;
@Service("subCompanyService")
public class SubCompanyServiceImpl implements ISubCompanyService {
@Autowired
private SubCompanyDao subCompanyDao;
@Override
public SubCompany findById(Integer id) {
//dao中并没有设计该接口,其实是调用了BaseMapper的接口
//BaseMapper封装了常用的CRUD操作,直接调用就节省了代码开发
return subCompanyDao.selectById(id);
}
}
4、Controller
package com.ld.druid.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ld.druid.service.ISubCompanyService;
@RestController
@RequestMapping("/plus")
public class MyBatisPlusController {
@Autowired
private ISubCompanyService subCompanyService;
@GetMapping(value = "/findById")
public Object findById(){
return subCompanyService.findById(2);
}
}
5、@MapperScan
package com.ld.druid;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.ld.druid.dao") // 替换为你的Mapper接口所在包的路径 非MyBatis官方starter 所以需要配置包扫描 如果不加该注解则要在每个mapper接口添加@Mapper注解
public class SpringDruidDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDruidDemoApplication.class, args);
}
}
四、分页处理 马上补上
全部评论 (0)
还没有任何评论哟~
