使用springboot + druid + mybatisplus完成多数据源配置
一. 简介
1. 版本
springboot版本为2.0.3.RELEASE,mybatisplus版本为2.1.9, druid版本为1.1.9,swagger版本为2.7.0
2. 项目地址
https://gitee.com/wbsxch/ssm.git
3. 留个记录,方便查找
开发步骤:
1. 新建springboot项目。
2. 导入依赖 --> devtools,lombok,web,thymeleaf,mysql,aop,mybatisplus,druid,swagger。
3. maven多环境配置。
4. 编写application.yml --> 项目端口,项目路径名,mybatisplus配置,mysql多数据源配置。
5. 新建DataSourceContextHolder 用于设置,获取,清空 当前线程内的数据源变量。
6. 新建 MultipleDataSource 实现 AbstractRoutingDataSource 类。重写determineCurrentLookupKey(),通过
DataSourceContextHolder 获取数据源变量,用于当作lookupKey取出指定的数据源。
7. 新建DataSourceEnum 用于存放数据源名称。
8. 新建注解 DataSource,用于aop类中当作切入点来选择数据源。
编写aop类 --> DataSourceAspect.java
9. 新建并配置DruidConfiguration,MybatisplusConfiguration,SwaggerConfiguration 三个类。
DruidConfiguration --> StatViewServlet 和 WebStatFilter Druid监控配置和监控过滤器。
MybatisplusConfiguration --> mybatisplus 分页插件,SQL执行效率插件
数据源Bean,MultipleDataSource 注入
SqlSessionFactory注入
SwaggerConfiguration --> 常规配置。
10. crud Controller接口。
11. 完整项目结构
二. 详细步骤
1. 新建springboot项目
idea新建Empty项目 然后创建springboot项目 勾选 devtools,lombok,web,thymeleaf,mysql
2. 导入依赖
pom.xml
<?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.seawaterbt</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>ssm</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
<druid.version>1.1.9</druid.version>
<swagger.version>2.7.0</swagger.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.1.9</version>
</dependency> <!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies> <profiles>
<profile>
<id>prod</id>
<properties>
<config.dir>prod</config.dir>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<config.dir>dev</config.dir>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>qa</id>
<properties>
<config.dir>qa</config.dir>
</properties>
</profile>
</profiles> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins> <resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/config/${config.dir}</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
</build> </project>
pom.xml
3. Maven多环境配置 看pom文件
4. 编写application.yml
application.yml
server:
port: 8080
servlet:
context-path: /ssm
spring:
datasource:
druid:
db1:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///mds
initialSize: 5
minIdle: 5
maxActive: 20
db2:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///ssj
initialSize: 5
minIdle: 5
maxActive: 20
mybatis-plus:
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
# 如果是放在resource目录 classpath:/mapper/*Mapper.xml
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.seawatebt.ssm.entity
global-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: 0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: 2
#驼峰下划线转换
db-column-underline: true
#mp2.3+ 全局表前缀 mp_
#table-prefix: mp_
#刷新mapper 调试神器
refresh-mapper: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置(下面3个配置)
logic-delete-value: 4
logic-not-delete-value: 0
configuration:
#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
map-underscore-to-camel-case: true
cache-enabled: false
#配置JdbcTypeForNull, oracle数据库必须配置
jdbc-type-for-null: 'null'
application.yml
5. 新建DataSourceContextHolder
DataSourceContextHolder.java
package com.seawaterbt.ssm.multiple; public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new InheritableThreadLocal<>(); /**
* 设置数据源
* @param db
*/
public static void setDataSource(String db){
contextHolder.set(db);
} /**
* 取得当前数据源
* @return
*/
public static String getDataSource(){
return contextHolder.get();
} /**
* 清除上下文数据
*/
public static void clear(){
contextHolder.remove();
}
}
DataSourceContextHolder.java
6. 新建 MultipleDataSource
MultipleDataSource.java
package com.seawaterbt.ssm.multiple; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class MultipleDataSource extends AbstractRoutingDataSource { @Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
MultipleDataSource.java
7. 新建DataSourceEnum
DataSourceEnum.java
package com.seawaterbt.ssm.enums; public enum DataSourceEnum { DB1("db1"),DB2("db2"); private String value; DataSourceEnum(String value){this.value=value;} public String getValue() {
return value;
}
}
DataSourceEnum.java
8. 新建注解 DataSource
DataSource.java
package com.seawaterbt.ssm.annotation; import com.seawaterbt.ssm.enums.DataSourceEnum; import java.lang.annotation.*; @Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource { DataSourceEnum value() default DataSourceEnum.DB1;
}
DataSource.java
DataSourceAspect.java
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect { @Pointcut("@within(com.seawaterbt.ssm.annotation.DataSource) || @annotation(com.seawaterbt.ssm.annotation.DataSource)")
public void pointCut(){ } @Before("pointCut() && @annotation(dataSource)")
public void doBefore(DataSource dataSource){
log.info("选择数据源---"+dataSource.value().getValue());
DataSourceContextHolder.setDataSource(dataSource.value().getValue());
} @After("pointCut()")
public void doAfter(){
DataSourceContextHolder.clear();
}
}
9. 新建并配置DruidConfiguration,MybatisplusConfiguration,SwaggerConfiguration
DruidConfiguration.java
package com.seawaterbt.ssm.config; import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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; @Configuration
public class DruidConfiguration { @Bean
public ServletRegistrationBean startViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
// IP白名单
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
// IP黑名单(共同存在时,deny优先于allow)
servletRegistrationBean.addInitParameter("deny","127.0.0.1");
//控制台管理用户
servletRegistrationBean.addInitParameter("loginUsername","admin");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否能够重置数据
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
} @Bean
public FilterRegistrationBean statFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
//忽略过滤的格式
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}
DruidConfiguration.java
MyBatiesPlusConfiguration.java
package com.seawaterbt.ssm.config; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.seawaterbt.ssm.enums.DataSourceEnum;
import com.seawaterbt.ssm.multiple.MultipleDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.context.annotation.Profile; import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map; @Configuration
@MapperScan("com.seawaterbt.ssm.mapper*")
public class MyBatiesPlusConfiguration { /*
* 分页插件,自动识别数据库类型
* 多租户,请参考官网【插件扩展】
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 开启 PageHelper 的支持
paginationInterceptor.setLocalPage(true);
return paginationInterceptor;
} /**
* SQL执行效率插件
*/
@Bean
@Profile({"dev","qa"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(1000);
performanceInterceptor.setFormat(true);
return performanceInterceptor;
} @Bean(name = "db1")
@ConfigurationProperties(prefix = "spring.datasource.druid.db1" )
public DataSource db1() {
return DruidDataSourceBuilder.create().build();
} @Bean(name = "db2")
@ConfigurationProperties(prefix = "spring.datasource.druid.db2" )
public DataSource db2() {
return DruidDataSourceBuilder.create().build();
} /**
* 动态数据源配置
* @return
*/
@Bean
@Primary
public DataSource multipleDataSource(@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2) {
MultipleDataSource multipleDataSource = new MultipleDataSource();
Map< Object, Object > targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceEnum.DB1.getValue(), db1);
targetDataSources.put(DataSourceEnum.DB2.getValue(), db2);
//添加数据源
multipleDataSource.setTargetDataSources(targetDataSources);
//设置默认数据源
multipleDataSource.setDefaultTargetDataSource(db1);
return multipleDataSource;
} @Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(multipleDataSource(db1(),db2()));
//sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*/*Mapper.xml")); MybatisConfiguration configuration = new MybatisConfiguration();
//configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
sqlSessionFactory.setConfiguration(configuration);
sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
paginationInterceptor() //添加分页功能
});
//sqlSessionFactory.setGlobalConfig(globalConfiguration());
return sqlSessionFactory.getObject();
} /*@Bean
public GlobalConfiguration globalConfiguration() {
GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
conf.setLogicDeleteValue("-1");
conf.setLogicNotDeleteValue("1");
conf.setIdType(0);
//conf.setMetaObjectHandler(new MyMetaObjectHandler());
conf.setDbColumnUnderline(true);
conf.setRefresh(true);
return conf;
}*/
}
MyBatiesPlusConfiguration.java
SwaggerConfiguration.java
package com.seawaterbt.ssm.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfiguration { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.seawaterbt.ssm.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("多数据源 SSM 测试服务")
.description("多数据源 SSM 测试文档")
.termsOfServiceUrl("http://www.seawaterbt.com")
//.contact(new Contact("海水不甜","http://www.seawaterbt.com","809318068@qq.com"))
.version("1.0")
.build();
} }
SwaggerConfiguration.java
10. crud Controller接口
1. Entity
Student.java
package com.seawaterbt.ssm.entity; import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Api("学生实体对象")
@Data
@TableName("t_student")
public class Student { @ApiModelProperty("学生id")
@TableId(type = IdType.AUTO)
private Integer id; @ApiModelProperty("学生姓名")
private String name; @ApiModelProperty("学生年龄")
private Integer age; @ApiModelProperty("学生班级")
private String classname; }
Student.java
Teacher.java
package com.seawaterbt.ssm.entity; import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Api("老师实体对象")
@Data
@TableName("t_teacher")
public class Teacher { @ApiModelProperty("老师id")
@TableId(type = IdType.AUTO)
private Integer id; @ApiModelProperty("老师姓名")
private String name; @ApiModelProperty("老师年龄")
private Integer age; @ApiModelProperty("老师所教学科")
private String subject;
}
Teacher.java
2. Mapper 接口及其xml
StudentMapper.java
package com.seawaterbt.ssm.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.seawaterbt.ssm.entity.Student; public interface StudentMapper extends BaseMapper<Student> {
}
StudentMapper.java
StudentMapper.xml
<?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.seawaterbt.ssm.mapper.StudentMapper"> </mapper>
StudentMapper.xml
TeacherMapper.java
package com.seawaterbt.ssm.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.seawaterbt.ssm.entity.Teacher; public interface TeacherMapper extends BaseMapper<Teacher> {
}
TeacherMapper.java
TeacherMapper.xml
<?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.seawaterbt.ssm.mapper.TeacherMapper"> </mapper>
TeacherMapper.xml
3. Service 接口及其实现类
StudentService.java
package com.seawaterbt.ssm.service; import com.baomidou.mybatisplus.service.IService;
import com.seawaterbt.ssm.entity.Student; public interface StudentService extends IService<Student> {
}
StudentService.java
StudentServiceImpl.java
package com.seawaterbt.ssm.service.impl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.seawaterbt.ssm.entity.Student;
import com.seawaterbt.ssm.mapper.StudentMapper;
import com.seawaterbt.ssm.service.StudentService;
import org.springframework.stereotype.Service; @Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {
}
StudentServiceImpl.java
TeacherService.java
package com.seawaterbt.ssm.service; import com.baomidou.mybatisplus.service.IService;
import com.seawaterbt.ssm.entity.Teacher; public interface TeacherService extends IService<Teacher> {
}
TeacherService.java
TeacherServiceImpl.java
package com.seawaterbt.ssm.service.impl; import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.seawaterbt.ssm.annotation.DataSource;
import com.seawaterbt.ssm.entity.Teacher;
import com.seawaterbt.ssm.enums.DataSourceEnum;
import com.seawaterbt.ssm.mapper.TeacherMapper;
import com.seawaterbt.ssm.service.TeacherService;
import org.springframework.stereotype.Service; import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map; @Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper,Teacher> implements TeacherService { @Override
@DataSource(DataSourceEnum.DB2)
public boolean insert(Teacher entity) {
return super.insert(entity);
} @Override
@DataSource(DataSourceEnum.DB2)
public boolean deleteById(Serializable id) {
return super.deleteById(id);
} @Override
@DataSource(DataSourceEnum.DB2)
public boolean updateById(Teacher entity) {
return super.updateById(entity);
} @Override
@DataSource(DataSourceEnum.DB2)
public Teacher selectById(Serializable id) {
return super.selectById(id);
} @Override
@DataSource(DataSourceEnum.DB2)
public List<Teacher> selectList(Wrapper<Teacher> wrapper) {
return super.selectList(wrapper);
} @Override
@DataSource(DataSourceEnum.DB2)
public Page<Teacher> selectPage(Page<Teacher> page) {
return super.selectPage(page);
} @Override
@DataSource(DataSourceEnum.DB2)
public Page<Teacher> selectPage(Page<Teacher> page, Wrapper<Teacher> wrapper) {
return super.selectPage(page, wrapper);
}
}
TeacherServiceImpl.java
4. Vo
StudentVo.java
package com.seawaterbt.ssm.vo; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Data
@ApiModel("学生vo")
public class StudentVo { @ApiModelProperty("学生姓名")
private String name; @ApiModelProperty("学生年龄")
private Integer age; @ApiModelProperty("学生班级")
private String classname;
}
StudentVo.java
TeacherVo.java
package com.seawaterbt.ssm.vo; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Data
@ApiModel("老师vo")
public class TeacherVo { @ApiModelProperty("老师姓名")
private String name; @ApiModelProperty("老师年龄")
private Integer age; @ApiModelProperty("老师教的学科")
private String subject;
}
TeacherVo.java
5. Controller
StudentController.java
package com.seawaterbt.ssm.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.seawaterbt.ssm.entity.Student;
import com.seawaterbt.ssm.service.StudentService;
import com.seawaterbt.ssm.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @Api("对学生表CRUD")
@RestController
@RequestMapping("/student")
public class StudentController { @Autowired
private StudentService studentService; @ApiOperation("添加学生")
@PostMapping("/add")
public String add(@RequestBody StudentVo student){
Student stu = new Student();
stu.setName(student.getName());
stu.setAge(student.getAge());
stu.setClassname(student.getClassname());
return studentService.insert(stu)?"添加成功":"添加失败";
} @ApiOperation("删除学生")
@DeleteMapping("/delete/{id}")
public String delete(@ApiParam("学生的主键id")@PathVariable(value = "id") Integer id){
return studentService.deleteById(id)?"删除成功":"删除失败";
} @ApiOperation("修改学生")
@PostMapping("/update")
public String update(@RequestBody Student student){
return studentService.updateById(student)?"修改成功":"修改失败";
} @ApiOperation(value = "查询学生")
@GetMapping("/list")
public List<Student> list(){
Wrapper<Student> wrapper = new EntityWrapper<>();
return studentService.selectList(wrapper);
}
}
StudentController.java
TeacherController.java
package com.seawaterbt.ssm.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.seawaterbt.ssm.entity.Teacher;
import com.seawaterbt.ssm.service.TeacherService;
import com.seawaterbt.ssm.vo.TeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @Api("对老师表CRUD")
@RestController
@RequestMapping("/teacher")
public class TeacherController { @Autowired
private TeacherService teacherService; @ApiOperation(value = "添加老师")
@PostMapping("/add")
public String add(@RequestBody TeacherVo teacher){
Teacher tea = new Teacher();
tea.setName(teacher.getName());
tea.setAge(teacher.getAge());
tea.setSubject(teacher.getSubject());
return teacherService.insert(tea)?"添加成功":"添加失败";
} @ApiOperation("删除老师")
@DeleteMapping("/delete/{id}")
public String delete(@ApiParam("老师的主键id")@PathVariable(value = "id") Integer id){
return teacherService.deleteById(id)?"删除成功":"删除失败";
} @ApiOperation("修改老师")
@PostMapping("/update")
public String update(@RequestBody Teacher teacher){
return teacherService.updateById(teacher)?"修改成功":"修改失败";
} @ApiOperation(value = "查询老师")
@GetMapping("/list")
public List<Teacher> list(){
Wrapper<Teacher> wrapper = new EntityWrapper<>();
return teacherService.selectList(wrapper);
}
}
TeacherController.java
使用springboot + druid + mybatisplus完成多数据源配置的更多相关文章
- springboot+druid+mybatis-Plus 配置详解
网上找了很多关于springboot+druid+mybatis-Plus的配置,遇见的很多问题 也没找到好的解决方案.折腾了好几天终于自己配置通过了. springboot的pom文件 <pa ...
- spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务
文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...
- springboot+druid连接池及监控配置
1. 问题描述 阿里巴巴的数据库连接池Druid在效率与稳定性都很高,被很多开发团队使用,并且自带的Druid监控也很好用,本章简单介绍下springboot+druid配置连接池及监控. 2. 解决 ...
- Springboot 2.x下多数据源配置
本文同样适用于2.x版本下Mybatis的多数据源配置 项目中经常会遇到一个项目需要访问多个数据源的情况,多数情况下可以参考这个教程进行配置. 不过该教程适合springboot1.x版本,由于2.x ...
- springboot-整合多数据源配置
简介 主要介绍两种整合方式,分别是 springboot+mybatis 使用分包方式整合,和 springboot+druid+mybatisplus 使用注解方式整合. 一.表结构 在本地新建两个 ...
- SpringBoot整合MyBatisPlus配置动态数据源
目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...
- springboot+druid+mybatis plus的多数据源配置
思路 yml中配置多个数据源信息 通过AOP切换不同数据源 配合mybatis plus使用 POM依赖 <dependency> <groupId>org.springfra ...
- Springboot+Druid 动态数据源配置监控
一.引入maven依赖,使用 starter 与原生 druid 依赖配置有所不同 <dependency> <groupId>com.alibaba</groupId& ...
- SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了 ...
随机推荐
- Myeclipse jdk的安装
- 2018-8-10-Roslyn-节点的-Span-和--FullSpan-有什么区别
title author date CreateTime categories Roslyn 节点的 Span 和 FullSpan 有什么区别 lindexi 2018-08-10 19:16:52 ...
- QT 获取系统时间
1.导入QTime #include <QTime> 2.定义QTime 对象接受当前时间 QTime t=QTime::currentTime(); t就是系统时间. 3.将t转化为st ...
- pytorch旧版安装
https://pytorch.org/get-started/previous-versions 可以直接下载文件 用 pip 直接在下载目录安装就可以了
- Jmeter处理数据库
安装环境: jmeter版本:3.1版本 java1.8版本 安装步骤: 1.下载连接mysql数据库jar包,地址:https://pan.baidu.com/s/10k6zD6CU4mo7xYJF ...
- MySQL常用函数大全讲解
MySQL数据库中提供了很丰富的函数.MySQL函数包括数学函数.字符串函数.日期和时间函数.条件判断函数.系统信息函数.加密函数.格式化函数等.通过这些函数,可以简化用户的操作.例如,字符串连接函数 ...
- part10.3-字符驱动访问揭秘
- iptables 详细使用
检查状态 先检查是否安装了iptables $ service iptables status 安装iptables $ yum install iptables 升级iptables $ yum u ...
- behavior planning——15.cost function design weightTweaking
Designing cost functions is difficult and getting them all to cooperate to produce reasionable vehic ...
- 【codeforces 520A】Pangram
[题目链接]:http://codeforces.com/problemset/problem/520/A [题意] 给你一个字符串. 统计里面有没有出现所有的英文字母->'a'..'z' 每个 ...