千家信息网

springboot beatlsql怎么使用

发表于:2025-02-09 作者:千家信息网编辑
千家信息网最后更新 2025年02月09日,这篇文章主要介绍"springboot beatlsql怎么使用",在日常操作中,相信很多人在springboot beatlsql怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法
千家信息网最后更新 2025年02月09日springboot beatlsql怎么使用

这篇文章主要介绍"springboot beatlsql怎么使用",在日常操作中,相信很多人在springboot beatlsql怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"springboot 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,h3,sqllite,DB2.

引入依赖

            org.springframework.boot            spring-boot-devtools            true                            com.ibeetl            beetl            2.3.2                              com.ibeetl            beetlsql            2.3.1                              mysql            mysql-connector-java            5.0.5        

这几个依赖都是必须的。

整合阶段
由于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.jarrestart.include.beetlsql=/beetlsql-2.3.1.jar

在templates下加一个index.btl文件。

加入jar和配置beatlsql的这些bean,以及resources这些配置之后,springboot就能够访问到数据库类。

举个restful的栗子
初始化数据库的表

# DROP TABLE `account` IF EXISTSCREATE 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...   }

到此,关于"springboot beatlsql怎么使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0