這篇文章給大家介紹使用springboot如何實(shí)現(xiàn)對(duì) beatlsql進(jìn)行整合,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
從事成都移動(dòng)云計(jì)算中心,服務(wù)器租用,云主機(jī),網(wǎng)頁空間,域名注冊,CDN,網(wǎng)絡(luò)代維等服務(wù)。
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)
引入依賴
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
這幾個(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... }
數(shù)據(jù)訪問dao層
public interface AccountDao extends BaseMapper{ @SqlStatement(params = "name") Account selectAccountByName(String name); }
接口繼承BaseMapper,就能獲取單表查詢的一些性質(zhì),當(dāng)你需要自定義sql的時(shí)候,只需要在resouses/sql/account.md文件下書寫文件:
selectAccountByName === *根據(jù)name獲account select * from account where name= #name#
其中“=== ”上面是唯一標(biāo)識(shí),對(duì)應(yīng)于接口的方法名,“* ”后面是注釋,在下面就是自定義的sql語句,具體的見官方文檔。
web層
這里省略了service層,實(shí)際開發(fā)補(bǔ)上。
@RestController @RequestMapping("/account") public class AccountController { @Autowired AccountDao accountDao; @RequestMapping(value = "/list",method = RequestMethod.GET) public ListgetAccounts(){ 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 測試,代碼已全部通過。
個(gè)人使用感受,使用bealsql做了一些項(xiàng)目的試驗(yàn),但是沒有真正用于真正的生產(chǎn)環(huán)境,用起來非常的爽。但是springboot沒有提供自動(dòng)裝配的直接支持,需要自己注解bean。另外使用這個(gè)orm的人不太多,有木有坑不知道,在我使用的過程中沒有遇到什么問題。另外它的中文文檔比較友好。
關(guān)于使用springboot如何實(shí)現(xiàn)對(duì) beatlsql進(jìn)行整合就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。