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 编写自定义属性类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 创建自定义配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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 { @Autowired private DruidProperties properties; @Bean public 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,如何自动加载配置)
1 2 3 | # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cnblogs.yjmyzz.druid.DruidAutoConfiguration |
1.4 application.yml中配置
1 2 3 4 5 6 7 8 9 | druid: url: jdbc:mysql: //localhost:3306/study?useSSL=false driver- class : com.mysql.jdbc.Driver username: root password: *** initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true |
二、集成mybatis
2.1 常规的mapper/xml配置
这个跟常规使用mybatis并没有什么不同,参考上图的结构
2.2 抽象一个通用Mapper
1 2 3 4 5 6 7 8 | 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都应该继承它,类似:
1 2 3 4 5 6 7 | 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配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | mybatis: type-aliases- package : com.cnblogs.yjmyzz.service.dao mapper-locations: classpath:mapper/*.xml mapper: mappers: - com.cnblogs.yjmyzz.util.MyMapper not-empty: false identity: MYSQL pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql |
还有一个常见问题:如何在调试时输出SQL语句,以及屏蔽掉一些不需要的日志?可参考下面的配置
1 2 3 4 5 6 7 | logging: level: root: DEBUG tk.mybatis: DEBUG com.alibaba.dubbo: ERROR org.apache.zookeeper: ERROR file: "/var/log/application/dubbo-provider.log" |
最后使用的地方,代码就跟常规代码完全一样了,参考下面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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 ); @Autowired CityMapper cityMapper; @Override public 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
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一步一步教你部署ktransformers,大内存单显卡用上Deepseek-R1
· 一次Java后端服务间歇性响应慢的问题排查记录
2009-06-15 内存数据库到底有多快?