环境介绍
技术栈 |
springboot+mybatis-plus+druid+baomidou+mysql+oracle+dm |
软件 |
版本 |
mysql |
8 |
IDEA |
IntelliJ IDEA 2022.2.1 |
JDK |
1.8 |
Spring Boot |
2.7.13 |
mybatis |
2.3.1 |
pom.xml所需依赖
org.springframework.boot
spring-boot-starter-web
com.baomidou
mybatis-plus-boot-starter
3.5.3.2
com.baomidou
dynamic-datasource-spring-boot-starter
3.5.0
com.alibaba
druid-spring-boot-starter
1.1.14
com.mysql
mysql-connector-j
runtime
com.alibaba
druid
1.2.15
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.dameng
Dm8JdbcDriver18
8.1.1.49
com.oracle.database.jdbc
ojdbc8
runtime
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
逆向工程:通过数据库表接口,逆向生成java工程结构
实体类、mapper接口、mapper映射文件、Service接口、service实现类
application.yml
spring:
datasource:
dynamic:
primary: #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
mysql1:
username: root
password: PWD
url: jdbc:mysql://IP:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.cj.jdbc.Driver
# driver-class-name: com.mysql.jdbc.Driver
mysql2:
url: jdbc:mysql://IP:3306/sys?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: PWD
driver-class-name: com.mysql.cj.jdbc.Driver
oracle:
username: system
password: PWD
url: jdbc:oracle:thin:@IP:1521:orcl
driver-class-name: oracle.jdbc.driver.OracleDriver
# driver-class-name: com.mysql.jdbc.Driver
dm:
url: jdbc:dm://127.0.0.1:5236/demo?
username: SYSDBA
password: SYSDBA
driver-class-name: dm.jdbc.driver.DmDriver
# driver-class-name: com.mysql.jdbc.Driver
server:
port: 8089
mybatis-plus:
configuration:
#输出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置映射规则
map-underscore-to-camel-case: true #表示支持下划线到驼蜂的映射
#隐藏mybatis图标
global-config:
banner: false
db-config:
logic-delete-field: status
logic-not-delete-value: 1
logic-delete-value: 0
spy.properties
#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
MybatisX逆向工程只是一个便携的工具,自定义接口和实现类还是要自己写
@Mapper
public interface UserMapper extends BaseMapper {
}
@Mapper
public interface TUserMapper extends BaseMapper {
}
@Mapper
public interface TestMapper extends BaseMapper {
List selectTestAll();
}
@Mapper
public interface DmuserMapper extends BaseMapper {
List selectDmUserAll();
Integer addDmUser(Dmuser dmuser);
}
public interface UserService extends IService {
}
public interface TUserService extends IService {
}
public interface TestService extends IService {
List selectTestAll();
}
public interface DmuserService extends IService {
List selectDmUserAll();
Integer addDmUser(Dmuser dmuser);
}
id,name,age,
email,status,gender,
contact,create_time,update_time
id,name,age,
email
id,name
id,name,address
insert into "SYSDBA"."DMUser"("id", "name", "address") VALUES(#{id},#{name},#{address});
@Service
@DS("mysql2")
public class UserServiceImpl extends ServiceImpl
implements UserService{
}
@Service
@DS("mysql1")
public class TUserServiceImpl extends ServiceImpl
implements TUserService{
}
@Service
@DS("oracle")
public class TestServiceImpl extends ServiceImpl
implements TestService{
@Autowired
private TestMapper testMapper;
@Override
public List selectTestAll() {
return testMapper.selectTestAll();
}
}
@Service
@DS("dm")
public class DmuserServiceImpl extends ServiceImpl
implements DmuserService{
@Autowired
private DmuserMapper dmuserMapper;
@Override
public List selectDmUserAll() {
return dmuserMapper.selectDmUserAll();
}
@Override
public Integer addDmUser(Dmuser dmuser) {
return dmuserMapper.addDmUser(dmuser);
}
}
@SpringBootTest
class DatasourcesApplicationTests {
@Autowired
private TUserService tUserService;
@Autowired
private UserService userService;
@Autowired
private TestService testService;
@Autowired
private DmuserService dmuserService;
@Test
void sys(){
userService.list();
}
@Test
void oracleDemo(){
testService.selectTestAll();
}
@Test
void dmDemo(){
dmuserService.selectDmUserAll();
}
@Test
void oracleToDMTest(){
List oracles =testService.selectTestAll();
for (com.example.domain.Test oracle : oracles) {
Dmuser dmuser =new Dmuser();
dmuser.setId(Math.toIntExact(oracle.getId()));
dmuser.setName(oracle.getName());
dmuser.setAddress("null");
dmuserService.addDmUser(dmuser);
System.out.println(""+oracle);
}
}
@Test
void mysqlToDMTest(){
List mysqls =tUserService.list();
for (TUser mysql : mysqls) {
Dmuser dmuser =new Dmuser();
dmuser.setId(Math.toIntExact(mysql.getId()));
dmuser.setName(mysql.getName());
dmuser.setAddress(mysql.getEmail());
dmuserService.addDmUser(dmuser);
System.out.println(""+mysql);
}
}
}
https://baomidou.com/
为简化开发而生
MyBatis-Plus(opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。