spring-boot整合mybatis-plus
首先我们需要创建一个sprngboot项目,首先引入依赖
<!-- springboot项目依赖父项 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version> <!-- 此版本号,boot号 所有子类参考 -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- 开启web启动项目,自动配置依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入bybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 连接数据库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- lombook -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 代码自动生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 多数据源读写分离 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<!-- 普通打包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后创建主函数
@SpringBootApplication
//是sql接口的类
@MapperScan("com.text.mapper")
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}
配置yml,数据库读写分离
server:
servlet:
context-path: /
port: 9020
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
datasource:
master:
url: jdbc:mysql://localhost:3306/park?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&useLegacyDatetimeCode=false
username: examsys
password: examsys
driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
slave_1:
url: jdbc:mysql://localhost:3307/park?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&useLegacyDatetimeCode=false
username: examsys
password: examsys
driver-class-name: com.mysql.jdbc.Driver
//日志打印
logging:
level:
root: info
com:
text:
mapper: debug
//打印参数日志
mybatis:
mapper-locations: classpath:mapper/zidong/*.xml
type-aliases-package: com.text.po
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
springboot与分页插件结合
@Configuration
@MapperScan("com.text.mapper")
public class MybatisPlusConfig{
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
po
@Data
//对应的表名
@TableName(value = "emp")
public class EmpPojo {
//主键名字
@TableId(value="user_id")
private String userId;
private String userName;
private String phone;
private String inTime;
private String outTime;
private String status;
private String payMoney;
private String time;
}
mapper
public interface EmpMapper extends BaseMapper<EmpPojo> {
}
controller(由于是demo我直接调用的mapper)(外加读写分离,利用DS注解)
//查询所有数据(不分页)
@DS("slave_1")
@PostMapping("/showall")
public Map<String, Object> showall() {
Map<String, Object> map = new HashMap<>();
map.put("data", empMapper.selectList(null));
return map;
}
//查询数据(分页)
@DS("slave_1")
@PostMapping("/fenye")
public Map<String ,Object> fenye (){
Map<String,Object> map = new HashMap<>();
IPage<EmpPojo> page = new Page<>(1,10);
map.put("data",empMapper.selectPage(page,null)) ;
return map;
}
//读写分离查从库
@DS("slave_1")
@PostMapping("/findone")
public Map<String, Object> findOne(@RequestBody EmpPojo empPojo) {
Map<String, Object> map = new HashMap<>();
String id = empPojo.getUserId();
log.info("------------"+id);
map.put("data",empMapper.selectById(id));
return map;
}
//读写分离写主库
@PostMapping("/save")
@DS("master")
public int save(@RequestBody EmpPojo empPojo) {
log.info("-------"+empPojo.getUserId());
return empMapper.insert(empPojo);
}
//修改
@PostMapping("/update")
@DS("master")
public int update(@RequestBody EmpPojo empPojo) {
return empMapper.updateById(empPojo);
}
//删除
@PostMapping("/delete")
@DS("master")
public int delete (@RequestBody EmpPojo empPojo){
return empMapper.deleteById(empPojo.getUserId());
}