一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - springboot整合 beatlsql的實(shí)例代碼

springboot整合 beatlsql的實(shí)例代碼

2020-09-24 15:22方志朋 JAVA教程

這篇文章主要介紹了springboot整合 beatlsql的實(shí)例代碼,BeetSql是一個(gè)全功能DAO工具,同時(shí)具有hibernate 優(yōu)點(diǎn) & Mybatis優(yōu)點(diǎn)功能,有興趣的可以了解一下

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)

  • 開(kāi)發(fā)效率
    • 無(wú)需注解,自動(dòng)使用大量?jī)?nèi)置SQL,輕易完成增刪改查功能,節(jié)省50%的開(kāi)發(fā)工作量
    • 數(shù)據(jù)模型支持Pojo,也支持Map/List這種快速模型,也支持混合模型
    • SQL 模板基于Beetl實(shí)現(xiàn),更容易寫(xiě)和調(diào)試,以及擴(kuò)展
  • 維護(hù)性
    • SQL 以更簡(jiǎn)潔的方式,Markdown方式集中管理,同時(shí)方便程序開(kāi)發(fā)和數(shù)據(jù)庫(kù)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ù)庫(kù)支持的開(kāi)源工具
    • 支持跨數(shù)據(jù)庫(kù)平臺(tái),開(kāi)發(fā)者所需工作減少到最小,目前跨數(shù)據(jù)庫(kù)支持mysql,postgres,oracle,sqlserver,h2,sqllite,DB2.

引入依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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沒(méi)有對(duì) beatlsql的快速啟動(dòng)裝配,所以需要我自己導(dǎo)入相關(guān)的bean,包括數(shù)據(jù)源,包掃描,事物管理器等。

在application加入以下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@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());//開(kāi)啟駝峰
    factory.setSqlLoader(new ClasspathLoader("/sql"));//sql文件路徑
    return factory;
  }
 
 
  //配置數(shù)據(jù)庫(kù)
  @Bean(name = "datasource")
  public DataSource getDataSource() {
    return DataSourceBuilder.create().url("jdbc:mysql://127.0.0.1:3306/test").username("root").password("123456").build();
  }
 
  //開(kāi)啟事務(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:

?
1
2
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就能夠訪問(wèn)到數(shù)據(jù)庫(kù)類。

舉個(gè)restful的栗子

初始化數(shù)據(jù)庫(kù)的表

?
1
2
3
4
5
6
7
8
9
10
# 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

?
1
2
3
4
5
6
7
8
9
10
public class Account {
  private int id ;
  private String name ;
  private double money;
 
  getter...
 
  setter...
 
 }

數(shù)據(jù)訪問(wèn)dao層

?
1
2
3
4
5
public interface AccountDao extends BaseMapper<Account> {
 
  @SqlStatement(params = "name")
  Account selectAccountByName(String name);
}

接口繼承BaseMapper,就能獲取單表查詢的一些性質(zhì),當(dāng)你需要自定義sql的時(shí)候,只需要在resouses/sql/account.md文件下書(shū)寫(xiě)文件:

?
1
2
3
4
5
selectAccountByName
===
*根據(jù)name獲account
 
  select * from account where name= #name#

其中“=== ”上面是唯一標(biāo)識(shí),對(duì)應(yīng)于接口的方法名,“* ”后面是注釋,在下面就是自定義的sql語(yǔ)句,具體的見(jiàn)官方文檔。

web層

這里省略了service層,實(shí)際開(kāi)發(fā)補(bǔ)上。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@RestController
@RequestMapping("/account")
public class AccountController {
 
  @Autowired
  AccountDao 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";
    }
  }
}

通過(guò)postman 測(cè)試,代碼已全部通過(guò)。

個(gè)人使用感受,使用bealsql做了一些項(xiàng)目的試驗(yàn),但是沒(méi)有真正用于真正的生產(chǎn)環(huán)境,用起來(lái)非常的爽。但是springboot沒(méi)有提供自動(dòng)裝配的直接支持,需要自己注解bean。另外使用這個(gè)orm的人不太多,有木有坑不知道,在我使用的過(guò)程中沒(méi)有遇到什么問(wèn)題。另外它的中文文檔比較友好。

源碼下載:https://github.com/forezp/SpringBootLearning

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/forezp/article/details/70662983

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品aaa | 韩国日本香港毛片免费 | 国产成人99久久亚洲综合精品 | 顶级欧美做受xxx000 | 91精品国产色综合久久不卡蜜 | 我们日本在线观看免费动漫下载 | ass巨大胖女人sias | 亚洲精品精品一区 | 深夜福利免费在线观看 | 成人123| 黑人biglackon10十| 九九影院午夜理论片无码 | 国产婷婷高清在线观看免费 | 999国产| 亚洲免费一 | 99在线资源| 99精品偷自拍 | 国产成人精品一区二三区在线观看 | 欧美撒尿屁股嘘嘘撒尿 | 天天色踪合 | 西施打开双腿下面好紧 | 欧美一级久久久久久久大片 | 99视频都是精品热在线播放 | 嫩草影院永久在线播放 | 亚洲色图欧美图片 | 亚洲电影不卡 | 99精品国产自产在线观看 | 亚洲日韩精品欧美一区二区一 | 北岛玲在线视频 | 久久综合狠狠综合狠狠 | 成人猫咪maomiav永久网址 | 火影小南被爆羞羞网站 | 女人把扒开给男人爽的 | 亚洲欧洲日产v特级毛片 | 成人免费播放 | 天天天做天天天天爱天天想 | 暖暖中国免费观看高清完整版 | 亚洲上最大成网人站4438 | 消息称老熟妇乱视频一区二区 | 韩剧消失的眼角膜免费完整版 | 日韩在线一区二区三区免费视频 |