forked from zhoustone/middle-ware-parent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpringBoot2 配置多数据源,整合MybatisPlus增强插件
- Loading branch information
cicadasmile
committed
Oct 11, 2019
1 parent
8bb17e7
commit cefa123
Showing
15 changed files
with
1,298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?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"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starters</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.plus.batis</groupId> | ||
<artifactId>ware-plus-batis</artifactId> | ||
<packaging>jar</packaging> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>1.3.2</version> | ||
</dependency> | ||
<!-- Mybatis --> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-boot-starter</artifactId> | ||
<version>3.0.7.1</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-generator</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus</artifactId> | ||
<version>3.0.7.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.38</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.1.13</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.1.10</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.6</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.pagehelper</groupId> | ||
<artifactId>pagehelper-spring-boot-starter</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
</dependencies> | ||
<!-- 项目构建 --> | ||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
ware-plus-batis/src/main/java/com/plus/batis/Application7014.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.plus.batis; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
@MapperScan(basePackages = {"com.plus.batis.mapper"}) | ||
public class Application7014 { | ||
public static void main(String[] args) { | ||
SpringApplication.run(Application7014.class,args) ; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
ware-plus-batis/src/main/java/com/plus/batis/config/DruidConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.plus.batis.config; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import com.alibaba.druid.support.http.StatViewServlet; | ||
import com.alibaba.druid.support.http.WebStatFilter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.boot.web.servlet.ServletRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
public class DruidConfig { | ||
private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class); | ||
@Value("${spring.datasource.druid.url}") | ||
private String dbUrl; | ||
@Value("${spring.datasource.druid.username}") | ||
private String username; | ||
@Value("${spring.datasource.druid.password}") | ||
private String password; | ||
@Value("${spring.datasource.druid.driverClassName}") | ||
private String driverClassName; | ||
@Value("${spring.datasource.druid.initial-size}") | ||
private int initialSize; | ||
@Value("${spring.datasource.druid.max-active}") | ||
private int maxActive; | ||
@Value("${spring.datasource.druid.min-idle}") | ||
private int minIdle; | ||
@Value("${spring.datasource.druid.max-wait}") | ||
private int maxWait; | ||
@Value("${spring.datasource.druid.pool-prepared-statements}") | ||
private boolean poolPreparedStatements; | ||
@Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") | ||
private int maxPoolPreparedStatementPerConnectionSize; | ||
@Value("${spring.datasource.druid.time-between-eviction-runs-millis}") | ||
private int timeBetweenEvictionRunsMillis; | ||
@Value("${spring.datasource.druid.min-evictable-idle-time-millis}") | ||
private int minEvictableIdleTimeMillis; | ||
@Value("${spring.datasource.druid.max-evictable-idle-time-millis}") | ||
private int maxEvictableIdleTimeMillis; | ||
@Value("${spring.datasource.druid.validation-query}") | ||
private String validationQuery; | ||
@Value("${spring.datasource.druid.test-while-idle}") | ||
private boolean testWhileIdle; | ||
@Value("${spring.datasource.druid.test-on-borrow}") | ||
private boolean testOnBorrow; | ||
@Value("${spring.datasource.druid.test-on-return}") | ||
private boolean testOnReturn; | ||
@Value("${spring.datasource.druid.filters}") | ||
private String filters; | ||
@Value("{spring.datasource.druid.connection-properties}") | ||
private String connectionProperties; | ||
@Bean | ||
public DataSource dataSource() { | ||
DruidDataSource datasource = new DruidDataSource(); | ||
datasource.setUrl(dbUrl); | ||
datasource.setUsername(username); | ||
datasource.setPassword(password); | ||
datasource.setDriverClassName(driverClassName); | ||
datasource.setInitialSize(initialSize); | ||
datasource.setMinIdle(minIdle); | ||
datasource.setMaxActive(maxActive); | ||
datasource.setMaxWait(maxWait); | ||
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); | ||
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); | ||
datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis); | ||
datasource.setValidationQuery(validationQuery); | ||
datasource.setTestWhileIdle(testWhileIdle); | ||
datasource.setTestOnBorrow(testOnBorrow); | ||
datasource.setTestOnReturn(testOnReturn); | ||
datasource.setPoolPreparedStatements(poolPreparedStatements); | ||
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); | ||
try { | ||
datasource.setFilters(filters); | ||
} catch (Exception e) { | ||
logger.error("druid configuration initialization filter", e); | ||
} | ||
datasource.setConnectionProperties(connectionProperties); | ||
return datasource; | ||
} | ||
@Bean | ||
public ServletRegistrationBean statViewServlet(){ | ||
ServletRegistrationBean srb = | ||
new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); | ||
//设置控制台管理用户 | ||
srb.addInitParameter("loginUsername","root"); | ||
srb.addInitParameter("loginPassword","root"); | ||
//是否可以重置数据 | ||
srb.addInitParameter("resetEnable","false"); | ||
return srb; | ||
} | ||
@Bean | ||
public FilterRegistrationBean statFilter(){ | ||
//创建过滤器 | ||
FilterRegistrationBean frb = | ||
new FilterRegistrationBean(new WebStatFilter()); | ||
//设置过滤器过滤路径 | ||
frb.addUrlPatterns("/*"); | ||
//忽略过滤的形式 | ||
frb.addInitParameter("exclusions", | ||
"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); | ||
return frb; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
ware-plus-batis/src/main/java/com/plus/batis/controller/UserBaseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.plus.batis.controller; | ||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
import com.baomidou.mybatisplus.core.metadata.IPage; | ||
import com.github.pagehelper.PageInfo; | ||
import com.plus.batis.entity.QueryParam; | ||
import com.plus.batis.entity.UserBase; | ||
import com.plus.batis.service.UserBaseService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import javax.annotation.Resource; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
public class UserBaseController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(UserBaseController.class) ; | ||
|
||
@Resource | ||
private UserBaseService userBaseService ; | ||
|
||
@RequestMapping("/info") | ||
public UserBase getUserBase (){ | ||
return userBaseService.getById(1) ; | ||
} | ||
|
||
@RequestMapping("/queryInfo") | ||
public String queryInfo (){ | ||
|
||
UserBase userBase1 = userBaseService.getOne(new QueryWrapper<UserBase>().orderByDesc("create_time")) ; | ||
LOGGER.info("倒叙取值:{}",userBase1.getUserName()); | ||
|
||
Integer count = userBaseService.count() ; | ||
LOGGER.info("查询总数:{}",count); | ||
|
||
UserBase userBase2 = new UserBase() ; | ||
userBase2.setId(1); | ||
userBase2.setUserName("spring"); | ||
boolean resFlag = userBaseService.saveOrUpdate(userBase2) ; | ||
LOGGER.info("保存更新:{}",resFlag); | ||
|
||
Map<String, Object> listByMap = new HashMap<>() ; | ||
listByMap.put("state","0") ; | ||
Collection<UserBase> listMap = userBaseService.listByMap(listByMap) ; | ||
LOGGER.info("ListByMap查询:{}",listMap); | ||
|
||
boolean removeFlag = userBaseService.removeById(3) ; | ||
LOGGER.info("删除数据:{}",removeFlag); | ||
|
||
return "success" ; | ||
} | ||
|
||
@RequestMapping("/queryPage") | ||
public IPage<UserBase> queryPage (){ | ||
QueryParam param = new QueryParam() ; | ||
param.setPage(1); | ||
param.setPageSize(10); | ||
param.setUserName("cicada"); | ||
param.setState(0); | ||
return userBaseService.queryPage(param) ; | ||
} | ||
|
||
@RequestMapping("/pageHelper") | ||
public PageInfo<UserBase> pageHelper (){ | ||
return userBaseService.pageHelper(new QueryParam()) ; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
ware-plus-batis/src/main/java/com/plus/batis/entity/BaseParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.plus.batis.entity; | ||
|
||
public class BaseParam { | ||
private long page ; | ||
private long pageSize ; | ||
|
||
public long getPage() { | ||
return page; | ||
} | ||
|
||
public void setPage(long page) { | ||
this.page = page; | ||
} | ||
|
||
public long getPageSize() { | ||
return pageSize; | ||
} | ||
|
||
public void setPageSize(long pageSize) { | ||
this.pageSize = pageSize; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
ware-plus-batis/src/main/java/com/plus/batis/entity/QueryParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.plus.batis.entity; | ||
|
||
public class QueryParam extends BaseParam { | ||
private String userName ; | ||
private Integer state ; | ||
public String getUserName() { | ||
return userName; | ||
} | ||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
public Integer getState() { | ||
return state; | ||
} | ||
public void setState(Integer state) { | ||
this.state = state; | ||
} | ||
} |
Oops, something went wrong.