读写分离样例
This commit is contained in:
parent
dc79677f97
commit
e0454693e4
73
dxfl/pom.xml
Normal file
73
dxfl/pom.xml
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>dxfl</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>dxfl</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.4.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<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>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.0.9</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-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
14
dxfl/src/main/java/com/example/dxfl/DxflApplication.java
Normal file
14
dxfl/src/main/java/com/example/dxfl/DxflApplication.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.example.dxfl;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableTransactionManagement(order = 10)
|
||||||
|
public class DxflApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DxflApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.dxfl.config;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description 这里切换读/写模式
|
||||||
|
* 原理是利用ThreadLocal保存当前线程是否处于读模式(通过开始READ_ONLY注解在开始操作前设置模式为读模式,
|
||||||
|
* 操作结束后清除该数据,避免内存泄漏,同时也为了后续在该线程进行写操作时任然为读模式
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-08-31
|
||||||
|
*/
|
||||||
|
public class DbContextHolder {
|
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(DbContextHolder.class);
|
||||||
|
public static final String WRITE = "write";
|
||||||
|
public static final String READ = "read";
|
||||||
|
|
||||||
|
private static ThreadLocal<String> contextHolder= new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void setDbType(String dbType) {
|
||||||
|
if (dbType == null) {
|
||||||
|
log.error("dbType为空");
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
log.info("设置dbType为:{}",dbType);
|
||||||
|
contextHolder.set(dbType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDbType() {
|
||||||
|
return contextHolder.get() == null ? WRITE : contextHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearDbType() {
|
||||||
|
contextHolder.remove();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.example.dxfl.config;
|
||||||
|
|
||||||
|
import com.example.dxfl.util.NumberUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-03
|
||||||
|
*/
|
||||||
|
public class MyAbstractRoutingDataSource extends AbstractRoutingDataSource {
|
||||||
|
|
||||||
|
@Value("${mysql.datasource.num}")
|
||||||
|
private int num;
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object determineCurrentLookupKey() {
|
||||||
|
String typeKey = DbContextHolder.getDbType();
|
||||||
|
if (typeKey == DbContextHolder.WRITE) {
|
||||||
|
log.info("使用了写库");
|
||||||
|
return typeKey;
|
||||||
|
}
|
||||||
|
//使用随机数决定使用哪个读库
|
||||||
|
int sum = NumberUtil.getRandom(1, num);
|
||||||
|
log.info("使用了读库{}", sum);
|
||||||
|
return DbContextHolder.READ + sum;
|
||||||
|
}
|
||||||
|
}
|
20
dxfl/src/main/java/com/example/dxfl/config/ReadOnly.java
Normal file
20
dxfl/src/main/java/com/example/dxfl/config/ReadOnly.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.dxfl.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description 通过该接口注释的service使用读模式,其他使用写模式
|
||||||
|
*
|
||||||
|
* 接口注释只是一种办法,如果项目已经有代码了,通过注释可以不修改任何业务代码加持读写分离
|
||||||
|
* 也可以通过切面根据方法开头来设置读写模式,例如getXXX()使用读模式,其他使用写模式
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-08-31
|
||||||
|
*/
|
||||||
|
@Target({ElementType.METHOD,ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface ReadOnly {
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.example.dxfl.config;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-08-31
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class ReadOnlyInterceptor implements Ordered {
|
||||||
|
private static final Logger log= LoggerFactory.getLogger(ReadOnlyInterceptor.class);
|
||||||
|
|
||||||
|
@Around("@annotation(readOnly)")
|
||||||
|
public Object setRead(ProceedingJoinPoint joinPoint,ReadOnly readOnly) throws Throwable{
|
||||||
|
try{
|
||||||
|
DbContextHolder.setDbType(DbContextHolder.READ);
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}finally {
|
||||||
|
DbContextHolder.clearDbType();
|
||||||
|
log.info("清除threadLocal");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package com.example.dxfl.config;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
|
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-03
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = "com.example.dxfl.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
|
||||||
|
public class WriteOrReadDatabaseConfig {
|
||||||
|
|
||||||
|
@Value("${mysql.datasource.type-aliases-package}")
|
||||||
|
private String typeAliasesPackage;
|
||||||
|
|
||||||
|
@Value("${mysql.datasource.mapper-locations}")
|
||||||
|
private String mapperLocation;
|
||||||
|
|
||||||
|
@Value("${mysql.datasource.config-location}")
|
||||||
|
private String configLocation;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写数据源
|
||||||
|
*
|
||||||
|
* @Primary 标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。
|
||||||
|
* 多数据源配置的时候注意,必须要有一个主数据源,用 @Primary 标志该 Bean
|
||||||
|
*/
|
||||||
|
@Primary
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties(prefix = "mysql.datasource.write")
|
||||||
|
public DataSource writeDataSource() {
|
||||||
|
return new DruidDataSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读数据源
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties(prefix = "mysql.datasource.read")
|
||||||
|
public DataSource read1() {
|
||||||
|
return new DruidDataSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多数据源需要自己设置sqlSessionFactory
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public SqlSessionFactory sqlSessionFactory() throws Exception {
|
||||||
|
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
||||||
|
bean.setDataSource(routingDataSource());
|
||||||
|
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
||||||
|
// 实体类对应的位置
|
||||||
|
bean.setTypeAliasesPackage(typeAliasesPackage);
|
||||||
|
// mybatis的XML的配置
|
||||||
|
bean.setMapperLocations(resolver.getResources(mapperLocation));
|
||||||
|
bean.setConfigLocation(resolver.getResource(configLocation));
|
||||||
|
return bean.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置事务,事务需要知道当前使用的是哪个数据源才能进行事务处理
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public DataSourceTransactionManager dataSourceTransactionManager() {
|
||||||
|
return new DataSourceTransactionManager(routingDataSource());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置数据源路由,通过该类中的determineCurrentLookupKey决定使用哪个数据源
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public AbstractRoutingDataSource routingDataSource() {
|
||||||
|
MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource();
|
||||||
|
Map<Object, Object> targetDataSources = new HashMap<>(2);
|
||||||
|
targetDataSources.put(DbContextHolder.WRITE, writeDataSource());
|
||||||
|
targetDataSources.put(DbContextHolder.READ+"1", read1());
|
||||||
|
proxy.setDefaultTargetDataSource(writeDataSource());
|
||||||
|
proxy.setTargetDataSources(targetDataSources);
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.example.dxfl.controller;
|
||||||
|
|
||||||
|
import com.example.dxfl.service.TestService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-04
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
private final TestService testService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public TestController(TestService testService) {
|
||||||
|
this.testService = testService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/getAll")
|
||||||
|
public Object getAll(){
|
||||||
|
return testService.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("insertOne")
|
||||||
|
public void insertOne(){
|
||||||
|
testService.insertOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("readAndWrite")
|
||||||
|
public void readAndWrite(){
|
||||||
|
testService.testReadAndWrite();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("transInsert")
|
||||||
|
public void transInsert(){
|
||||||
|
testService.transInsert();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
dxfl/src/main/java/com/example/dxfl/entity/Test.java
Normal file
28
dxfl/src/main/java/com/example/dxfl/entity/Test.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.dxfl.entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-04
|
||||||
|
*/
|
||||||
|
public class Test {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
26
dxfl/src/main/java/com/example/dxfl/mapper/TestMapper.java
Normal file
26
dxfl/src/main/java/com/example/dxfl/mapper/TestMapper.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package com.example.dxfl.mapper;
|
||||||
|
|
||||||
|
import com.example.dxfl.entity.Test;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-04
|
||||||
|
*/
|
||||||
|
public interface TestMapper {
|
||||||
|
/**
|
||||||
|
* 获取全部数据
|
||||||
|
* @return List<Test>
|
||||||
|
*/
|
||||||
|
List<Test> getAll();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入一条数据
|
||||||
|
* @param test 数据
|
||||||
|
*/
|
||||||
|
void insertOne(@Param("test") Test test);
|
||||||
|
}
|
63
dxfl/src/main/java/com/example/dxfl/service/TestService.java
Normal file
63
dxfl/src/main/java/com/example/dxfl/service/TestService.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package com.example.dxfl.service;
|
||||||
|
|
||||||
|
import com.example.dxfl.config.ReadOnly;
|
||||||
|
import com.example.dxfl.entity.Test;
|
||||||
|
import com.example.dxfl.mapper.TestMapper;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-04
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class TestService {
|
||||||
|
|
||||||
|
private final TestMapper testMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public TestService(TestMapper testMapper) {
|
||||||
|
this.testMapper = testMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试读取,应该使用读库
|
||||||
|
*/
|
||||||
|
@ReadOnly
|
||||||
|
public List<Test> getAll() {
|
||||||
|
return testMapper.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试读取和插入,应该使用写库
|
||||||
|
*/
|
||||||
|
public void testReadAndWrite() {
|
||||||
|
this.getAll();
|
||||||
|
this.insertOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试插入数据,应该使用写库
|
||||||
|
*/
|
||||||
|
public void insertOne() {
|
||||||
|
Test test = new Test();
|
||||||
|
test.setName("fxb");
|
||||||
|
testMapper.insertOne(test);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试事务能否正常工作
|
||||||
|
*/
|
||||||
|
@Transactional(rollbackFor = RuntimeException.class)
|
||||||
|
public void transInsert(){
|
||||||
|
Test test = new Test();
|
||||||
|
test.setName("heiheihei");
|
||||||
|
testMapper.insertOne(test);
|
||||||
|
throw new RuntimeException("测试事务");
|
||||||
|
}
|
||||||
|
}
|
14
dxfl/src/main/java/com/example/dxfl/util/NumberUtil.java
Normal file
14
dxfl/src/main/java/com/example/dxfl/util/NumberUtil.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.example.dxfl.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-07
|
||||||
|
*/
|
||||||
|
public class NumberUtil {
|
||||||
|
|
||||||
|
public static int getRandom(int min, int max) {
|
||||||
|
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
|
}
|
||||||
|
}
|
17
dxfl/src/main/resources/application.yml
Normal file
17
dxfl/src/main/resources/application.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
mysql:
|
||||||
|
datasource:
|
||||||
|
#读库数目
|
||||||
|
num: 1
|
||||||
|
type-aliases-package: com.example.dxfl.dao
|
||||||
|
mapper-locations: classpath:/mapper/*.xml
|
||||||
|
config-location: classpath:/mybatis-config.xml
|
||||||
|
write:
|
||||||
|
url: jdbc:mysql://192.168.226.5:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.jdbc.Driver
|
||||||
|
read:
|
||||||
|
url: jdbc:mysql://192.168.226.6:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
driver-class-name: com.mysql.jdbc.Driver
|
14
dxfl/src/main/resources/mapper/test.xml
Normal file
14
dxfl/src/main/resources/mapper/test.xml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="com.example.dxfl.mapper.TestMapper">
|
||||||
|
|
||||||
|
<select id="getAll" resultType="com.example.dxfl.entity.Test">
|
||||||
|
select * from test
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<insert id="insertOne" parameterType="com.example.dxfl.entity.Test">
|
||||||
|
insert into test value (#{test.id},#{test.name})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
36
dxfl/src/main/resources/mybatis-config.xml
Normal file
36
dxfl/src/main/resources/mybatis-config.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||||
|
<configuration>
|
||||||
|
<settings>
|
||||||
|
<!-- 使全局的映射器启用或禁用缓存。 -->
|
||||||
|
<setting name="cacheEnabled" value="true" />
|
||||||
|
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
|
||||||
|
<setting name="lazyLoadingEnabled" value="true" />
|
||||||
|
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
|
||||||
|
<setting name="aggressiveLazyLoading" value="true"/>
|
||||||
|
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
|
||||||
|
<setting name="multipleResultSetsEnabled" value="true" />
|
||||||
|
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
|
||||||
|
<setting name="useColumnLabel" value="true" />
|
||||||
|
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
|
||||||
|
<setting name="useGeneratedKeys" value="false" />
|
||||||
|
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
|
||||||
|
<setting name="autoMappingBehavior" value="PARTIAL" />
|
||||||
|
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
|
||||||
|
<setting name="defaultExecutorType" value="SIMPLE" />
|
||||||
|
|
||||||
|
<setting name="defaultStatementTimeout" value="25" />
|
||||||
|
|
||||||
|
<setting name="defaultFetchSize" value="100" />
|
||||||
|
|
||||||
|
<setting name="safeRowBoundsEnabled" value="false" />
|
||||||
|
<!-- 使用驼峰命名法转换字段。 -->
|
||||||
|
<setting name="mapUnderscoreToCamelCase" value="true" />
|
||||||
|
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) default:session -->
|
||||||
|
<setting name="localCacheScope" value="SESSION" />
|
||||||
|
<!-- 默认为OTHER,为了解决oracle插入null报错的问题要设置为NULL -->
|
||||||
|
<setting name="jdbcTypeForNull" value="NULL" />
|
||||||
|
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
|
||||||
|
</settings>
|
||||||
|
|
||||||
|
</configuration>
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.example.dxfl;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class DxflApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
32
dxfl/src/test/java/com/example/dxfl/TestServiceTest.java
Normal file
32
dxfl/src/test/java/com/example/dxfl/TestServiceTest.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package com.example.dxfl;
|
||||||
|
|
||||||
|
import com.example.dxfl.service.TestService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description
|
||||||
|
*
|
||||||
|
* @author fxb
|
||||||
|
* @date 2018-09-05
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class TestServiceTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestService testService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insertOne(){
|
||||||
|
testService.insertOne();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAll(){
|
||||||
|
testService.getAll();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user