SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot5-beatlsql/
本文出自方志朋的博客
BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。
beatlsql 优点
开发效率
- 无需注解,自动使用大量内置SQL,轻易完成增删改查功能,节省50%的开发工作量
- 数据模型支持Pojo,也支持Map/List这种快速模型,也支持混合模型
- SQL 模板基于Beetl实现,更容易写和调试,以及扩展
维护性
- SQL 以更简洁的方式,Markdown方式集中管理,同时方便程序开发和数据库SQL调试。
- 可以自动将sql文件映射为dao接口类
- 灵活直观的支持支持一对一,一对多,多对多关系映射而不引入复杂的OR Mapping概念和技术。
- 具备Interceptor功能,可以调试,性能诊断SQL,以及扩展其他功能
其他
- 内置支持主从数据库支持的开源工具
- 支持跨数据库平台,开发者所需工作减少到最小,目前跨数据库支持mysql,postgres,oracle,sqlserver,h2,sqllite,DB2.
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetlsql</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
这几个依赖都是必须的。
整合阶段
由于springboot没有对 beatlsql的快速启动装配,所以需要我自己导入相关的bean,包括数据源,包扫描,事物管理器等。
在application加入以下代码:
@Bean(initMethod = "init", name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try {
// WebAppResourceLoader 配置root路径是关键
WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/templates").getFile().getPath());
beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
} catch (IOException e) {
e.printStackTrace();
}
//读取配置文件信息
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
//配置包扫描
@Bean(name = "beetlSqlScannerConfigurer")
public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
conf.setBasePackage("com.forezp.dao");
conf.setDaoSuffix("Dao");
conf.setSqlManagerFactoryBeanName("sqlManagerFactoryBean");
return conf;
}
@Bean(name = "sqlManagerFactoryBean")
@Primary
public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("datasource") DataSource datasource) {
SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
BeetlSqlDataSource source = new BeetlSqlDataSource();
source.setMasterSource(datasource);
factory.setCs(source);
factory.setDbStyle(new MySqlStyle());
factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
factory.setNc(new UnderlinedNameConversion());//开启驼峰
factory.setSqlLoader(new ClasspathLoader("/sql"));//sql文件路径
return factory;
}
//配置数据库
@Bean(name = "datasource")
public DataSource getDataSource() {
return DataSourceBuilder.create().url("jdbc:mysql://127.0.0.1:3306/test").username("root").password("123456").build();
}
//开启事务
@Bean(name = "txManager")
public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
DataSourceTransactionManager dsm = new DataSourceTransactionManager();
dsm.setDataSource(datasource);
return dsm;
}
在resouces包下,加META_INF文件夹,文件夹中加入spring-devtools.properties:
restart.include.beetl=/beetl-2.3.2.jar
restart.include.beetlsql=/beetlsql-2.3.1.jar
在templates下加一个index.btl文件。
加入jar和配置beatlsql的这些bean,以及resources这些配置之后,springboot就能够访问到数据库类。
举个restful的栗子
初始化数据库的表
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');
bean
public class Account {
private int id ;
private String name ;
private double money;
getter...
setter...
}
数据访问dao层
public interface AccountDao extends BaseMapper<Account> {
@SqlStatement(params = "name")
Account selectAccountByName(String name);
}
接口继承BaseMapper,就能获取单表查询的一些性质,当你需要自定义sql的时候,只需要在resouses/sql/account.md文件下书写文件:
selectAccountByName
===
*根据name获account
select * from account where name= #name#
其中“=== ”上面是唯一标识,对应于接口的方法名,“* ”后面是注释,在下面就是自定义的sql语句,具体的见官方文档。
web层
这里省略了service层,实际开发补上。
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
AccountDao accountDao;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<Account> getAccounts(){
return accountDao.all();
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id){
return accountDao.unique(id);
}
@RequestMapping(value = "",method = RequestMethod.GET)
public Account getAccountById(@RequestParam("name") String name){
return accountDao.selectAccountByName(name);
}
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money" ,required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
int t=accountDao.updateById(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}
@RequestMapping(value = "",method = RequestMethod.POST)
public String postAccount( @RequestParam(value = "name")String name,
@RequestParam(value = "money" )double money) {
Account account = new Account();
account.setMoney(money);
account.setName(name);
KeyHolder t = accountDao.insertReturnKey(account);
if (t.getInt() > 0) {
return account.toString();
} else {
return "fail";
}
}
}
通过postman 测试,代码已全部通过。
个人使用感受,使用bealsql做了一些项目的试验,但是没有真正用于真正的生产环境,用起来非常的爽。但是springboot没有提供自动装配的直接支持,需要自己注解bean。另外使用这个orm的人不太多,有木有坑不知道,在我使用的过程中没有遇到什么问题。另外它的中文文档比较友好。
源码下载:https://github.com/forezp/SpringBootLearning
参考资料
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
SpringBoot非官方教程 | 第五篇:springboot整合 beatlsql的更多相关文章
- SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源
这篇文章主要介绍,通过Spring Boot整合Mybatis后如何实现在一个工程中实现多数据源.同时可实现读写分离. 准备工作 环境: windows jdk 8 maven 3.0 IDEA 创建 ...
- (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...
- SpringBoot非官方教程 | 第二十三篇: 异步方法
转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...
- (转)SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql
本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: ...
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-mybatis/ 本文出自方志朋的博客 本文主要 ...
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot4-jpaJ/ 本文出自方志朋的博客 JPA全称J ...
- SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...
- SpringBoot非官方教程 | 第二十篇: 处理表单提交
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/ 本文出自方志朋的博客 这篇文件主要介 ...
随机推荐
- 运算符重载关键字operator
operator关键字用来重载内置运算符,使用方法如下: public class OperatorController : Controller { // // GET: /Operator/ pu ...
- EFCodeFirst 各种命令整理
1.Enable-Migrations (创建迁移目录:Migrations,如果有多个数据上下文可以用 -ContextTypeName 命令迁移对应的数据上下文 ) 2.Add-Migratio ...
- ubuntu 下查找某个文件的方法
1.whereis 文件名 特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来. 2.find / -name ...
- PAT 1043 Is It a Binary Search Tree
#include <cstdio> #include <climits> #include <cstdlib> #include <vector> co ...
- Python爬虫之requests模块(1)
一.引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃 ...
- List的设置值,跟变量的位置关系(变量范围的变化导致结果差别很大)
我们想要的结果是: [RegnTypeCharge: null,null,null,null,1,null,null,null,null,null,null,null,null,null,null,] ...
- CSS3控制单行文本的溢出
text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出.语法: 但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省略号的效果,还须定义 ...
- python 初识
一.Python介绍 Python简介 Python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...
- nagios外部命令接口
http://nagios.manubulon.com/traduction/docs14en/extcommands.html https://old.nagios.org/developerinf ...
- 深入浅出 关于SQL Server中的死锁问题
深入浅出 关于SQL Server中的死锁问题 博客2013-02-12 13:44 分享到:我要吐槽 死锁的本质是一种僵持状态,是多个主体对于资源的争用而导致的.理解死锁首先需要对死锁所涉及的相 ...