日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring-boot 速成(8) 集成druid+mybatis

發布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-boot 速成(8) 集成druid+mybatis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring-boot與druid、mybatis集成(包括pageHelper分頁插件), 要添加以下幾個依賴項:

compile('mysql:mysql-connector-java:6.0.5')compile('tk.mybatis:mapper-spring-boot-starter:1.1.1')compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')compile('com.github.pagehelper:pagehelper-spring-boot-starter:1.1.1')compile('com.alibaba:druid:1.0.28')

?

一、集成druid

1.1 編寫自定義屬性類

package com.cnblogs.yjmyzz.druid;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "druid") @Data public class DruidProperties {private String url;private String username;private String password;private String driverClass;private int maxActive;private int minIdle;private int initialSize;private boolean testOnBorrow; }

注:這里只列出了主要屬性,其它屬性如果需要,可自行添加

?

1.2 創建自定義配置類

package com.cnblogs.yjmyzz.druid;import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import javax.sql.DataSource; import java.sql.SQLException;@Configuration @EnableConfigurationProperties(DruidProperties.class) @ConditionalOnClass(DruidDataSource.class) @ConditionalOnProperty(prefix = "druid", name = "url") @AutoConfigureBefore(DataSourceAutoConfiguration.class) public class DruidAutoConfiguration {@Autowiredprivate DruidProperties properties;@Beanpublic DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setUrl(properties.getUrl());dataSource.setUsername(properties.getUsername());dataSource.setPassword(properties.getPassword());if (properties.getInitialSize() > 0) {dataSource.setInitialSize(properties.getInitialSize());}if (properties.getMinIdle() > 0) {dataSource.setMinIdle(properties.getMinIdle());}if (properties.getMaxActive() > 0) {dataSource.setMaxActive(properties.getMaxActive());}dataSource.setTestOnBorrow(properties.isTestOnBorrow());try {dataSource.init();} catch (SQLException e) {throw new RuntimeException(e);}return dataSource;} }  

注1:如果多數據源的,參考上面的代碼自行修改

注2:上面這二個類,可以抽出來放到公用類庫里,方便以后復用。 

?

1.3 添加 META-INF/spring.factories

參考如下內容(告訴spring-boot,如何自動加載配置)

# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cnblogs.yjmyzz.druid.DruidAutoConfiguration

?

?

1.4 application.yml中配置

druid:url: jdbc:mysql://localhost:3306/study?useSSL=falsedriver-class: com.mysql.jdbc.Driverusername: rootpassword: ***initial-size: 1min-idle: 1max-active: 20test-on-borrow: true

?

二、集成mybatis

2.1 常規的mapper/xml配置

這個跟常規使用mybatis并沒有什么不同,參考上圖的結構

?

2.2 抽象一個通用Mapper

package com.cnblogs.yjmyzz.util;import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper;public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {}

寫這個通用Mapper是為了后面寫crud代碼更簡單,其它具體的XXXMapper都應該繼承它,類似:

package com.cnblogs.yjmyzz.dao.mapper;import com.cnblogs.yjmyzz.dao.model.City; import com.cnblogs.yjmyzz.util.MyMapper;public interface CityMapper extends MyMapper<City> { }

?

2.3 application.yml配置

mybatis:type-aliases-package: com.cnblogs.yjmyzz.service.daomapper-locations: classpath:mapper/*.xmlmapper:mappers:- com.cnblogs.yjmyzz.util.MyMappernot-empty: falseidentity: MYSQLpagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql

?

還有一個常見問題:如何在調試時輸出SQL語句,以及屏蔽掉一些不需要的日志?可參考下面的配置

logging:level:root: DEBUGtk.mybatis: DEBUGcom.alibaba.dubbo: ERRORorg.apache.zookeeper: ERRORfile: "/var/log/application/dubbo-provider.log"

?

最后使用的地方,代碼就跟常規代碼完全一樣了,參考下面:

package com.cnblogs.yjmyzz.service.impl;import com.alibaba.dubbo.config.annotation.Service; import com.cnblogs.yjmyzz.dao.mapper.CityMapper; import com.cnblogs.yjmyzz.dao.model.City; import com.cnblogs.yjmyzz.service.api.DemoService; import com.cnblogs.yjmyzz.service.api.vo.CityVO; import com.github.pagehelper.PageHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils;import java.util.ArrayList; import java.util.List;/*** Created by 菩提樹下的楊過(http:/yjmyzz.cnblogs.com) on 2017/5/21.*/ @Service(version = "1.0.0") public class DemoServiceImpl implements DemoService {Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);@AutowiredCityMapper cityMapper;@Overridepublic List<CityVO> getCityList(int pageIndex, int pageSize) {PageHelper.startPage(pageIndex, pageSize);//設置分頁參數List<City> list = cityMapper.selectAll();com.github.pagehelper.PageInfo page = new com.github.pagehelper.PageInfo<>(list);//取頁面信息List<CityVO> result = new ArrayList<>();if (!CollectionUtils.isEmpty(list)) {for (City c : list) {CityVO v = new CityVO();v.setCityName(c.getName());v.setProvinceName(c.getState());result.add(v);}}logger.info("pageInfo=> page:" + page.getPageNum() + "/" + page.getPages());return result;}}

?

文中的示例代碼,已經托管在github上,地址:https://github.com/yjmyzz/spring-boot-dubbo-demo

轉載于:https://www.cnblogs.com/yjmyzz/p/spring-boot-integrate-with-mybatis-and-druid-tutorial.html

總結

以上是生活随笔為你收集整理的spring-boot 速成(8) 集成druid+mybatis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。