Spring Boot第五篇:springboot整合 beatlsql

news/2024/7/3 4:14:40

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


http://lihuaxi.xjx100.cn/news/281409.html

相关文章

印在基因里面的先验知识,或许是通往强人工智能的必经之路

作者 | 张拳石来源 | 知乎导读&#xff1a;近日&#xff0c;几何智能&#xff08;后被 Uber 收购&#xff09;创始人兼 CEO Gary Marcus 在他的文章 “Deep Learning: A Critical Appraisal” in arXiv:1801.00631 中分析了目前深度学习发展的瓶颈和面临的挑战。之后&#xff0…

使用 NumPy 的标准化技巧

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达数据科学不仅仅是开发模型&#xff0c;也有很多像清洗数据和选择有效特征的工作。将特征插入到一个具有相似的分布但是方式明显不同的模型中&#xff0c;或者是在极不相同…

JavaScript初学者编程题(12)

JavaScript初学者编程题(12) 题目&#xff1a;输入两个正整数m和n&#xff0c;求其最大公约数和最小公倍数。 HTMl部分 <input type"text" id"num1"><input type"text" id"num2"><button onclick"getNum()"…

Linux 守护进程,编写(转载)

设置守护进程过程&#xff1a;1.创建子进程&#xff0c;父进程退出2.在子进程中创建新会话3.改变当前目录为根目录4.重设文件权限掩码5.关闭文件描述符*6.守护进程里&#xff0c;忽略SIGCHLD信号 (即使fork两次&#xff0c;新进程由init接管&#xff0c;不进行signal(SIGCHLD,S…

在 Ubuntu 上通过命令行改变 Linux 系统语言

这是一个快速教程&#xff0c;展示了在 Ubuntu 和其他 Linux 发行版上从命令行改变语言的步骤。事实上&#xff0c;我一直在写西班牙语的文章。如果你没有访问过它并且/或你是一个讲西班牙语的人&#xff0c;请访问 Its FOSS en Espaol 并查看所有西班牙语的 Linux 内容。 你可…

在科研路上,大家有什么经验教训?

点上方蓝字视学算法获取更多干货在右上方 设为星标 ★&#xff0c;与你不见不散仅作学术分享&#xff0c;不代表本公众号立场&#xff0c;侵权联系删除转载于&#xff1a;知乎&#xff0c;https://www.zhihu.com/question/65354878&#xff0c;Amusi&#xff0c;CVer编辑昨天推…

__proto__和prototype

_proto_ 每个对象都有一个__proto__&#xff08;前后各两个下划线&#xff09;属性来标识自己所继承的原型对象。__proto__属性对性能影响非常严重&#xff0c;不建议使用。 prototype 只有函数才有prototype属性。当你创建函数时&#xff0c;JS会为这个函数自动添加prototype…

最新剑桥《AI全景报告》出炉:中国发表机器学习学术研究超过美国

来源 | 转载自新智元&#xff08;ID:AI_era&#xff09;2019年即将过去一半&#xff0c;剑桥大学的两位研究人员近日推出了本年度的State AI 2019全景报告。本报告基本沿袭去年的大体框架&#xff0c;从产业、人才、政策、预测等方面对过去一年来AI领域的技术的新进步、产业格局…