springboot整合 beatlsql

转载https://blog.csdn.net/forezp/article/details/70662983

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")@Primarypublic 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获accountselect * from account where name= #name#

其中“=== ”上面是唯一标识,对应于接口的方法名,“* ”后面是注释,在下面就是自定义的sql语句,具体的见官方文档。

web层

这里省略了service层,实际开发补上。


@RestController
@RequestMapping("/account")
public class AccountController {@AutowiredAccountDao 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整合 beatlsql相关推荐

  1. Spring Boot第五篇:springboot整合 beatlsql

    BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用. beatls ...

  2. java版b2b2c社交电商spring cloud分布式微服务(五)springboot整合 beatlsql

    电子商务社交平台源码请加企鹅求求:三五三六二四七二五九.BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需 ...

  3. Java B2B2C多用户商城 springboot架构 (五)springboot整合 beatlsql

    spring cloud b2b2c电子商务社交平台源码请加企鹅求求:一零三八七七四六二六.BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能 ...

  4. 企业SpringBoot 教程(五)springboot整合beatlsql

    BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用.完整项目的源码 ...

  5. SpringBoot第九篇: springboot整合Redis

    这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...

  6. es springboot 不设置id_原创 | 一篇解决Springboot 整合 Elasticsearch

    ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务. ElasticSearch是一个基于Lucene的 ...

  7. springboot整合shiro使用shiro-spring-boot-web-starter

    此文章仅仅说明在springboot整合shiro时的一些坑,并不是教程 增加依赖 <!-- 集成shiro依赖 --> <dependency><groupId> ...

  8. db2 springboot 整合_springboot的yml配置文件通过db2的方式整合mysql的教程

    springboot整合MySQL很简单,多数据源就master,slave就行了,但是在整合DB2就需要另起一行,以下是同一个yml文件 先配置MySQL,代码如下 spring: datasour ...

  9. 九、springboot整合rabbitMQ

    springboot整合rabbitMQ 简介 rabbitMQ是部署最广泛的开源消息代理. rabbitMQ轻量级,易于在内部和云中部署. 它支持多种消息传递协议. RabbitMQ可以部署在分布式 ...

最新文章

  1. 3D打印产业化机遇与挑战
  2. Facade与Mediator模式的区别
  3. 上海事业编制 计算机 待遇怎么样,事业单位情况
  4. 任正非之女姚安娜正式出道
  5. Postgres invalid command \N数据恢复处理
  6. Python:Numpy库中的invert()函数的用法
  7. [转载] Java异常:选择Checked Exception还是Unchecked Exception?
  8. bmp格式图像的读写函数(对一个开源代码的封装)
  9. 中国制盐市场销售动态及需求潜力预测报告(新版)2022-2027年
  10. win7局域网共享设置_分享几个简单实用的局域网共享设置工具
  11. 人工智能重新定义管理
  12. ResNet再进化!重新思考ResNet:采用高阶方案的改进堆叠策略
  13. 关于计算机固态硬盘正确的是,如何对固态硬盘进行初始化?选择合适的格式及分区结构很重要...
  14. 2021SAAE上海第七届教育装备展览会
  15. react-native android打包失败: GC overhead limit exceeded
  16. PrecompiledAssemblyException: Multiple precompiled assemblies with the same name websocket-sharp.dll
  17. InnoDB---深入理解事务提交--02
  18. Java人脸识别相册分类按时间分类相册按城市分类相册app源码
  19. 20170909深度学习solar测试日志
  20. 李笑来 -把时间当作朋友

热门文章

  1. vc 时间字符串转时间戳_Instant(时间戳)
  2. 《你还在我身旁》 香港中文大学《独立时代》杂志社微情书征文大赛一等奖作品。作者为香港中文大学学生戴畅。
  3. 基于modelsim的十个Verilog入门试验程序(5)(数字秒表+自助售票机)—程序+测试代码+波形+结果分析
  4. java imageicon 路径_java awt ImageIcon icon 相对路径设置
  5. 计算机二级mysql工具_2020年全国计算机二级MySQL复习知识点:优化工具
  6. reflect动画_3DSMAX制作超时空未来动画场景-3D建模场景模型教程
  7. 【LeetCode】【HOT 100】2. 两数相加
  8. 【笔记】Java数据结构与算法
  9. mybatis如何防止sql注入
  10. golang字节数组拷贝BlockCopy函数实现