企業級 SpringBoot 教程 (五)springboot整合 beatlsql
阿新 • • 發佈:2018-11-22
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語句,具體的見官方文件。