欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis-Plus+Druid配置及應(yīng)用詳解

 更新時(shí)間:2020年11月15日 11:06:21   作者:whatkevin1984  
這篇文章主要介紹了MyBatis-Plus+Druid配置及應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Mybatis-Plus的配置

1.Maven插件

velocity-engine-core是mybatis-plus自動(dòng)生成代碼所依賴的模板(不用自動(dòng)生成代碼功能可不用)

  <dependency>
  <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.0.6</version>
 </dependency>
 <dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity-engine-core</artifactId>
  <version>2.2</version>
 </dependency>

2.application.properties配置

和普通mysql配置相同,沒有額外配置

#mysql 連接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

3.Mapper相關(guān)注解

@MapperScan中直接mapper文件所在的package

@SpringBootApplication
@EnableTransactionManagement
@EnableEurekaClient
@MapperScan("com.example.demo.dao")
public class DemoApplication {

 public static void main(String[] args) {
 SpringApplication.run(DemoApplication.class, args);
 }
}

4.創(chuàng)建數(shù)據(jù)庫表


5.創(chuàng)建JavaBean(推薦使用Lombok插件方式) - Model層

@Data
@TableName("area")
@ToString(callSuper=true, includeFieldNames=true)
@AllArgsConstructor
@NoArgsConstructor
public class Area implements Serializable{
 
 private static final long serialVersionUID = 1L;
 private String id;
 private String name;
 private int sort; 
}

6.創(chuàng)建Mapper - DAO層

只有自定義SQL時(shí),mapper中才有內(nèi)容, 使用mybatis-plus自帶CRUD語句或者構(gòu)造器拼接語句時(shí), mapper通常為空

public interface AreaMapper extends BaseMapper<Area> {
 
 //通過mybatis-plus提供的注解,直接自定義SQL
 @Select("select name from area where sort > ${sort}")
 List<String> getBySort2(@Param("sort")int sort);
}

8.創(chuàng)建邏輯接口 - Service層

public interface IAreaService {
 
 public Area getById(String id);
 
 public List<Area> selectAll();
 
 public int updateByPrimaryKeySelective(Area record);
 
 public int deleteByPrimaryKey(String id);
 
 public int insertSelective(Area record);
 
 public List<Area> getBySort();
 
 public List<String> getBySort2(int sort);

}

9.創(chuàng)建邏輯類 - Service層

Mybatis-Plus使用SQL的3種常見方法:
a. 使用mybatis-plus自帶CRUD方法: 直接用mapper的自帶方法
b. 使用mybatis-plus提供的條件構(gòu)造器,拼接where條件
c. 使用@Select,@Update等注解, 自己寫SQL+參數(shù)

@Service
public class AreaServiceImpl implements IAreaService {

 @Autowired
 public AreaMapper areaMapper;

 //使用mybatis-plus自帶CRUD方法
 @Override
 public Area getById(String id) {
 return areaMapper.selectById(id);
 } 
 
 @Override
 public List<Area> selectAll() {
 return areaMapper.selectList(null);
 }
 
 @Override
 @Transactional
 public int updateByPrimaryKeySelective(Area record) {
 return areaMapper.updateById(record);
 }
 
 @Override
 public int deleteByPrimaryKey(String id) {
 return areaMapper.deleteById(id);
 }
 
 @Override
 public int insertSelective(Area record) {
 return areaMapper.insert(record);
 }
 
 //使用mybatis-plus提供的條件構(gòu)造器,拼接條件
 @Override
 public List<Area> getBySort(){
 QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
 queryWrapper.lambda().gt(Area::getSort, 40)
  .lt(Area::getSort, 80);
 return areaMapper.selectList(queryWrapper);
 }
 
 //通過mybatis-plus提供的注解,直接自定義SQL(定義在mapper中)
 @Override
 public List<String> getBySort2(int sort){
 return areaMapper.getBySort2(sort);
 
 }

}

10.創(chuàng)建接口 - Controller層

@RestController
@RequestMapping("/area")
public class AreaController {

 @Autowired
 private IAreaService areaService;
 
 @Autowired
 private DbpropertiesService dbproService;
 
 @Autowired
 private Area area;
 
 @Resource
 protected HttpServletRequest request;

 @RequestMapping(value = "/getAreaInfo",method=RequestMethod.GET)
 public Area getAreaInfo(@RequestParam(value="id") String id) {
 area = areaService.getById(id);
 return area;
 }
 
 @RequestMapping(value = "/getAreaAllInfo",method=RequestMethod.GET)
 public List<Area> getAreaInfo() {
 List<Area> arlist = areaService.selectAll();
 return arlist;
 }
 
 @RequestMapping(value ="/updateAreaName",method=RequestMethod.PUT)
 public int updateAreaInfo(@RequestParam(value="id") String id,@RequestParam(value="name",required=false) String name,@RequestParam(value="sort",required=false) Integer sort) {
 area.setId(id);
 area.setName(name); 
 area.setSort(sort);
 int ar = areaService.updateByPrimaryKeySelective(area);
 return ar;
 }
 
 @RequestMapping(value ="/deleteAreaName",method=RequestMethod.DELETE)
 public int deleteAreaInfo(@RequestParam(value="id") String id) {
 int ar = areaService.deleteByPrimaryKey(id);
 return ar;
 }
 
// @RequestMapping(value ="/insertAreaName",method=RequestMethod.POST)
// public int insertAreaInfo(@RequestParam(value="id") String id,@RequestParam(value="name",required=false) String name,@RequestParam(value="sort",required=false) Integer sort) {
// area.setId(id);
// area.setName(name); 
// area.setSort(sort);
// int ar = areaService.insertSelective(area);
// return ar;
// } 
 
 @RequestMapping(value ="/insertAreaName",method=RequestMethod.POST)
 public int insertAreaInfo(@RequestBody Area area) {
 int ar = areaService.insertSelective(area);
 return ar;
 }
 
 @RequestMapping(value ="/selectBySort",method=RequestMethod.GET)
 public List<Area> selectAreaSort() {
 List<Area> arList = areaService.getBySort();
 return arList;
 }
 
 @RequestMapping(value ="/selectBySort2",method=RequestMethod.GET)
 public List<String> selectAreaSort2(@RequestParam(value="sort") int sort) {
 List<String> arList = areaService.getBySort2(sort);
 return arList;
 }
}

參考文檔:
https://mp.baomidou.com/

數(shù)據(jù)源datasource

數(shù)據(jù)源是數(shù)據(jù)庫連接的規(guī)范接口

Springboot默認(rèn)支持4種數(shù)據(jù)源類型,定義在 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 中,分別是:

org.apache.tomcat.jdbc.pool.DataSource
 com.zaxxer.hikari.HikariDataSource
 org.apache.commons.dbcp.BasicDataSource
 org.apache.commons.dbcp2.BasicDataSource

在不指定數(shù)據(jù)源類型時(shí), SpringBoot默認(rèn)使用tomcat.jdbc

如果需要使用第三方數(shù)據(jù)源, 比如Druid, 步驟如下:

Druid的配置

1.Maven配置

 <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.3</version>
  </dependency>

2.application.properties配置

spring.datasource.type需要指定為Druid
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

其他的Druid自有屬性,可以寫在properties中也可以寫在Druid的配置類中

@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfiguration {
 
 private String url;
 private String username;
 private String password;
 private String driverClassName;
 private int initialSize = 5;
 private int minIdle = 5;
 private int maxActive = 10;
 private int maxWait = 2;
 private int timeBetweenEvictionRunsMillis = 1000 * 60;
 private int minEvictableIdleTimeMillis = 1000 * 60 * 30;
 private String validationQuery;
 private boolean testWhileIdle = false;
 private boolean testOnBorrow = true;
 private boolean testOnReturn = false;
 private boolean poolPreparedStatements = false;
 private int maxPoolPreparedStatementPerConnectionSize = -1;
 private String filters;
 private boolean useGlobalDataSourceStat = false;
 private String connectionProperties;

3.定義數(shù)據(jù)源

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSource2Config {
 
 private String url;
 private String username;
 private String password;
 
 @Bean
 public DataSource getDataSource() {
 DruidDataSource dataSource = new DruidDataSource();
 dataSource.setUrl(url);
 dataSource.setUsername(username);// 用戶名
 dataSource.setPassword(password);// 密碼
 return dataSource;
 }
 
 public String getUrl() {
 return url;
 }
 
 public void setUrl(String url) {
 this.url = url;
 }
 
 public String getUsername() {
 return username;
 }
 
 public void setUsername(String username) {
 this.username = username;
 }
 
 public String getPassword() {
 return password;
 }
 
 public void setPassword(String password) {
 this.password = password;
 }
}

4.配置Druid的監(jiān)控器和為事務(wù)管理器指定Druid數(shù)據(jù)源, 最終Configuration文件如下:

Druid監(jiān)控配置:

ServletRegistrationBean
FilterRegistrationBean

事務(wù)管理器配置:
PlatformTransactionManager

package com.example.demo.dao.druid;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;


@Configuration
public class DruidConfiguration {
 
 @Bean
 public ServletRegistrationBean druidServlet() {
  //logger.info("init Druid Servlet Configuration ");
  ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
  servletRegistrationBean.setServlet(new StatViewServlet());
  servletRegistrationBean.addUrlMappings("/druid/*");
  Map<String, String> initParameters = new HashMap<String, String>();
  initParameters.put("loginUsername", "admin");
  initParameters.put("loginPassword", "admin");
  initParameters.put("resetEnable", "false");
  initParameters.put("allow", ""); 
  initParameters.put("deny", "");
  servletRegistrationBean.setInitParameters(initParameters);
  return servletRegistrationBean;
 }

 @Bean
 public FilterRegistrationBean filterRegistrationBean() {
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  filterRegistrationBean.setFilter(new WebStatFilter());
  filterRegistrationBean.addUrlPatterns("/*");
  filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  return filterRegistrationBean;
 }
 
 @Bean
 @Primary
 public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
  DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
  dataSourceTransactionManager.setDataSource(dataSource);
  return dataSourceTransactionManager;
 }
 
 
}

//Druid配置Bean
@Component
@ConfigurationProperties(prefix = "spring.datasource")
class DruidDataSourceProperties {
 
 private String url;
 private String username;
 private String password;
 private String driverClassName;
 private int initialSize = 5;
 private int minIdle = 5;
 private int maxActive = 10;
 private int maxWait = 2;
 private int timeBetweenEvictionRunsMillis = 1000 * 60;
 private int minEvictableIdleTimeMillis = 1000 * 60 * 30;
 private String validationQuery;
 private boolean testWhileIdle = false;
 private boolean testOnBorrow = true;
 private boolean testOnReturn = false;
 private boolean poolPreparedStatements = false;
 private int maxPoolPreparedStatementPerConnectionSize = -1;
 private String filters = "stat";
 private boolean useGlobalDataSourceStat = false;
 private String connectionProperties;
 
 @Bean  //聲明其為Bean實(shí)例,將數(shù)據(jù)源設(shè)置為druid
 @Primary //在同樣的DataSource中,首先使用被標(biāo)注的DataSource
 public DataSource dataSource() {
  DruidDataSource datasource = new DruidDataSource();
  datasource.setUrl(url);
  datasource.setUsername(username);
  datasource.setPassword(password);
  datasource.setDriverClassName(driverClassName);

  //configuration
  datasource.setInitialSize(initialSize);
  datasource.setMinIdle(minIdle);
  datasource.setMaxActive(maxActive);
  datasource.setMaxWait(maxWait);
  datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  datasource.setValidationQuery(validationQuery);
  datasource.setTestWhileIdle(testWhileIdle);
  datasource.setTestOnBorrow(testOnBorrow);
  datasource.setTestOnReturn(testOnReturn);
  datasource.setPoolPreparedStatements(poolPreparedStatements);
  datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
  try {
   datasource.setFilters(filters);
  } catch (SQLException e) {
   System.err.println("druid configuration initialization filter: " + e);
  }
  datasource.setConnectionProperties(connectionProperties);
  return datasource;
 }
 
 public String getUrl() {
  return url;
 }

 public void setUrl(String url) {
  this.url = url;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getDriverClassName() {
  return driverClassName;
 }

 public void setDriverClassName(String driverClassName) {
  this.driverClassName = driverClassName;
 }

 public int getInitialSize() {
  return initialSize;
 }

 public void setInitialSize(int initialSize) {
  this.initialSize = initialSize;
 }

 public int getMinIdle() {
  return minIdle;
 }

 public void setMinIdle(int minIdle) {
  this.minIdle = minIdle;
 }

 public int getMaxActive() {
  return maxActive;
 }

 public void setMaxActive(int maxActive) {
  this.maxActive = maxActive;
 }

 public int getMaxWait() {
  return maxWait;
 }

 public void setMaxWait(int maxWait) {
  this.maxWait = maxWait;
 }

 public int getTimeBetweenEvictionRunsMillis() {
  return timeBetweenEvictionRunsMillis;
 }

 public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
  this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
 }

 public int getMinEvictableIdleTimeMillis() {
  return minEvictableIdleTimeMillis;
 }

 public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
  this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
 }

 public String getValidationQuery() {
  return validationQuery;
 }

 public void setValidationQuery(String validationQuery) {
  this.validationQuery = validationQuery;
 }

 public boolean isTestWhileIdle() {
  return testWhileIdle;
 }

 public void setTestWhileIdle(boolean testWhileIdle) {
  this.testWhileIdle = testWhileIdle;
 }

 public boolean isTestOnBorrow() {
  return testOnBorrow;
 }

 public void setTestOnBorrow(boolean testOnBorrow) {
  this.testOnBorrow = testOnBorrow;
 }

 public boolean isTestOnReturn() {
  return testOnReturn;
 }

 public void setTestOnReturn(boolean testOnReturn) {
  this.testOnReturn = testOnReturn;
 }

 public boolean isPoolPreparedStatements() {
  return poolPreparedStatements;
 }

 public void setPoolPreparedStatements(boolean poolPreparedStatements) {
  this.poolPreparedStatements = poolPreparedStatements;
 }

 public int getMaxPoolPreparedStatementPerConnectionSize() {
  return maxPoolPreparedStatementPerConnectionSize;
 }

 public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
  this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
 }

 public String getFilters() {
  return filters;
 }

 public void setFilters(String filters) {
  this.filters = filters;
 }


 public boolean isUseGlobalDataSourceStat() {
  return useGlobalDataSourceStat;
 }

 public void setUseGlobalDataSourceStat(boolean useGlobalDataSourceStat) {
  this.useGlobalDataSourceStat = useGlobalDataSourceStat;
 }

 public String getConnectionProperties() {
  return connectionProperties;
 }

 public void setConnectionProperties(String connectionProperties) {
  this.connectionProperties = connectionProperties;
 }
}

5.Druid性能監(jiān)控臺(tái)
http://localhost:8092/druid/index.html

到此這篇關(guān)于MyBatis-Plus+Druid配置及應(yīng)用詳解的文章就介紹到這了,更多相關(guān)MyBatis-Plus Druid配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot java-jar命令行啟動(dòng)原理解析

    SpringBoot java-jar命令行啟動(dòng)原理解析

    這篇文章主要介紹了SpringBoot java-jar命令行啟動(dòng)原理解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Spring boot連接MySQL 8.0可能出現(xiàn)的問題

    Spring boot連接MySQL 8.0可能出現(xiàn)的問題

    這篇文章主要給大家介紹了關(guān)于Spring boot連接MySQL 8.0可能出現(xiàn)的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果

    使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果

    這篇文章主要為大家詳細(xì)介紹了使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Java集合框架之WeakHashMap詳解

    Java集合框架之WeakHashMap詳解

    這篇文章主要介紹了Java集合框架之WeakHashMap詳解,在 WeakHashMap 中,當(dāng)某個(gè)鍵不再正常使用時(shí),會(huì)被從WeakHashMap中被自動(dòng)移除,更精確地說,對(duì)于一個(gè)給定的鍵,其映射的存在并不阻止垃圾回收器對(duì)該鍵的丟棄,這就使該鍵成為可終止的,需要的朋友可以參考下
    2023-09-09
  • IDEA更改Terminal的方法步驟

    IDEA更改Terminal的方法步驟

    Windows上開發(fā)有時(shí)候cmd不支持bash命令,有些操作就會(huì)非常麻煩,本文主要介紹了IDEA更改Terminal的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • SpringBoot?集成短信和郵件的配置示例詳解

    SpringBoot?集成短信和郵件的配置示例詳解

    這篇文章主要介紹了SpringBoot?集成短信和郵件的相關(guān)知識(shí),項(xiàng)目中使用lombok插件和swagger依賴,無相關(guān)依賴的請(qǐng)自行修改,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • springboot集成swagger、knife4j及常用注解的使用

    springboot集成swagger、knife4j及常用注解的使用

    這篇文章主要介紹了springboot集成swagger、knife4j及常用注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java中對(duì)象快速復(fù)制的幾種方式詳解

    Java中對(duì)象快速復(fù)制的幾種方式詳解

    這篇文章主要介紹了Java中對(duì)象快速復(fù)制的幾種方式詳解,對(duì)象的克隆是指創(chuàng)建一個(gè)新的對(duì)象,且新的對(duì)象的狀態(tài)與原始對(duì)象的狀態(tài)相同,當(dāng)對(duì)克隆的新對(duì)象進(jìn)行修改時(shí),不會(huì)影響原始對(duì)象的狀態(tài),需要的朋友可以參考下
    2023-08-08
  • 仿京東平臺(tái)框架開發(fā)開放平臺(tái)(包含需求,服務(wù)端代碼,SDK代碼)

    仿京東平臺(tái)框架開發(fā)開放平臺(tái)(包含需求,服務(wù)端代碼,SDK代碼)

    現(xiàn)在開放平臺(tái)越來越多了,下面針對(duì)仿京東開放平臺(tái)框架,封裝自己的開放平臺(tái),分享給大家。先感謝一下京東開放平臺(tái)的技術(shù)大佬們,下面從開放平臺(tái)需求,服務(wù)端代碼,SDK代碼三大塊進(jìn)行分享
    2021-06-06
  • Java中線程休眠編程實(shí)例

    Java中線程休眠編程實(shí)例

    這篇文章主要介紹了Java中線程休眠編程實(shí)例,本文直接給出代碼實(shí)例,并對(duì)休眠方法做了一番講解,需要的朋友可以參考下
    2015-06-06

最新評(píng)論