版本(均为目前最新版本):
springboot : 2.2.1
mybatis-plus : 3.2.0
dynamic-datasource : 2.5.7
mysql : 8.0.18
druid : 1.1.21
ps:dynamic-datasource : 2.5.7必须要spring-boot-starter-actuator的依赖,2.5.6之前的则不需要
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.lusen
mybatisplus
0.0.1-SNAPSHOT
mybatisplus
Demo project for Spring Boot
1.8
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-web
com.baomidou
mybatis-plus-boot-starter
3.2.0
com.baomidou
dynamic-datasource-spring-boot-starter
2.5.7
org.springframework.boot
spring-boot-starter-actuator
mysql
mysql-connector-java
8.0.18
runtime
com.alibaba
druid-spring-boot-starter
1.1.21
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
#端口
server:
port: 8080
spring:
application:
name: mybatisplus-demo
# DB 配置
datasource:
dynamic:
#主数据源
primary: db1
p6spy: true
druid:
initial-size: 5
min-idle: 5
max-active: 20
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
#test-on-borrow: true
validation-query-timeout: 20
filters: stat,wall,slf4j
datasource:
# db1-主数据源
db1:
url: jdbc:mysql://123.207.xx.xx:3306/lusen?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true
username: root
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
# db2-数据源
db2:
url: jdbc:mysql://123.207.xx.xx:3306/mysql?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true
username: root
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
# db3-数据源
db3:
url: jdbc:mysql://123.207.xx.xx:3306/mysql?useSSL=false&characterEncoding=UTF8
username: root
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
global-config:
db-config:
id-type: auto
capital-mode: true
configuration:
#打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#mapper的xml文件路径
mapper-locations: classpath:mybatis/mapper/**/*.xml
package com.lusen.mybatisplus.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
/**
* @author
* @description
* @data2019/11/23
*/
@TableName("t_programmer")//对应数据库表名
public class Programmer
{
/**
* 主键id
*/
private Long id;
/**
* 程序员类型
*/
private String type;
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private String sex;
/**
* 入坑时间
*/
private Date joinDate;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public Date getJoinDate()
{
return joinDate;
}
public void setJoinDate(Date joinDate)
{
this.joinDate = joinDate;
}
@Override
public String toString()
{
return "Programmer{" + "id=" + id + ", type='" + type + '\''
+ ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", joinDate="
+ joinDate + '}';
}
}
package com.lusen.mybatisplus.dao;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lusen.mybatisplus.domain.Programmer;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author
* @description
* @data2019/11/23
*/
@Repository
public interface ProgrammerDao extends BaseMapper
{
/**
* 按名字查询(对应xml文件里的selectByName)
* @param name
* @return
*/
List selectByName(@Param("name") String name);
/**
* 分页查询
* @param page
* @return
*/
@Select("select * from t_programmer")
IPage listProgrammer(Page page);
}
package com.lusen.mybatisplus.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author
* @description
* @data2019/11/23
*/
@Configuration
@MapperScan({"com.lusen.mybatisplus.dao"})
@EnableTransactionManagement
public class MybatisPlusConfig
{
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor()
{
PaginationInterceptor paginationInterceptor =
new PaginationInterceptor();
paginationInterceptor.setLimit(100);
return paginationInterceptor;
}
}
package com.lusen.mybatisplus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class MybatisplusApplication
{
public static void main(String[] args)
{
SpringApplication.run(MybatisplusApplication.class, args);
}
}
package com.lusen.mybatisplus.dao;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lusen.mybatisplus.domain.Programmer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ProgrammerDaoTest
{
@Resource
private ProgrammerDao programmerDao;
@Test
void selectByName()
{
List programmerList = programmerDao.selectByName("lusen");
programmerList.forEach(System.out::println);
}
@Test
void listProgrammer()
{
Page page = new Page();
programmerDao.listProgrammer(page);
page.getRecords().forEach(System.out::println);
}
}
ok,就是这么简单,详细参考github地址