這篇文章主要介紹“springboot beatlsql怎么使用”,在日常操作中,相信很多人在springboot beatlsql怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”springboot beatlsql怎么使用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

BeetSql是一個(gè)全功能DAO工具, 同時(shí)具有Hibernate 優(yōu)點(diǎn) & Mybatis優(yōu)點(diǎn)功能,適用于承認(rèn)以SQL為中心,同時(shí)又需求工具能自動(dòng)能生成大量常用的SQL的應(yīng)用。
beatlsql 優(yōu)點(diǎn)
開發(fā)效率
無需注解,自動(dòng)使用大量內(nèi)置SQL,輕易完成增刪改查功能,節(jié)省50%的開發(fā)工作量
數(shù)據(jù)模型支持Pojo,也支持Map/List這種快速模型,也支持混合模型SQL 模板基于Beetl實(shí)現(xiàn),更容易寫和調(diào)試,以及擴(kuò)展維護(hù)性
SQL 以更簡潔的方式,Markdown方式集中管理,同時(shí)方便程序開發(fā)和數(shù)據(jù)庫SQL調(diào)試。
可以自動(dòng)將sql文件映射為dao接口類
靈活直觀的支持支持一對(duì)一,一對(duì)多,多對(duì)多關(guān)系映射而不引入復(fù)雜的OR Mapping概念和技術(shù)。
具備Interceptor功能,可以調(diào)試,性能診斷SQL,以及擴(kuò)展其他功能其他內(nèi)置支持主從數(shù)據(jù)庫支持的開源工具支持跨數(shù)據(jù)庫平臺(tái),開發(fā)者所需工作減少到最小,目前跨數(shù)據(jù)庫支持mysql,postgres,oracle,sqlserver,h3,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>
這幾個(gè)依賴都是必須的。
整合階段
由于springboot沒有對(duì) beatlsql的快速啟動(dòng)裝配,所以需要我自己導(dǎo)入相關(guān)的bean,包括數(shù)據(jù)源,包掃描,事物管理器等。
在application加入以下代碼:
@Bean(initMethod = "init", name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try {
// WebAppResourceLoader 配置root路徑是關(guān)鍵
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;
}
//配置數(shù)據(jù)庫
@Bean(name = "datasource")
public DataSource getDataSource() {
return DataSourceBuilder.create().url("jdbc:mysql://127.0.0.1:3306/test").username("root").password("123456").build();
}
//開啟事務(wù)
@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下加一個(gè)index.btl文件。
加入jar和配置beatlsql的這些bean,以及resources這些配置之后,springboot就能夠訪問到數(shù)據(jù)庫類。
舉個(gè)restful的栗子
初始化數(shù)據(jù)庫的表
# 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...
}到此,關(guān)于“springboot beatlsql怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站題目:springbootbeatlsql怎么使用-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.chinadenli.net/article18/djoigp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、面包屑導(dǎo)航、靜態(tài)網(wǎng)站、網(wǎng)站策劃、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容