springboot 集成 beetsql

引言:进入本公司后,发现公司框架使用的持久层是beetlsql,按照大佬的说法,这个持久层框架只需要学习半个小时就会使用了。

起初刚进公司时还是传统ssm项目,现在升级使用springboot微服务了。所以自己特意学习整合一下。

1、引入pom.xml中的beetlsql依赖。【其他springboot的依赖在这里不列出来了】

<!--mysql 这就不用说了-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

<!--beetlsql 这是必须的-->
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl-framework-starter</artifactId>
            <version>1.2.30.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetlsql</artifactId>
            <version>2.12.28.RELEASE</version>
        </dependency>
<!--这是找了好久的坑,可能我是小白的原因吧-->
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

2、application.yml文件中配置beetlsql。【以下配置信息官网都有】

################### beetsql配置 ########################
beetlsql:
  sqlPath: /sql/mysql #存放resources/sql文件目录 (我是sql文件夹下还有一个mysql文件夹)
  #默认com,此选项配置beetlsql.daoSuffix来自动扫描com包及其子包下的所有以Dao结尾的Mapper类。
  basePackage: com.example.exa
  daoSuffix: Dao
  nameConversion: org.beetl.sql.core.UnderlinedNameConversion #将数据库里的含有下划线字段转化为java的驼峰命名风格
  dbStyle: org.beetl.sql.core.db.MySqlStyle #数据库风格
beetl-beetlsql:
  dev: true #将sql打印到控制台

# 数据源配置
datasource:
url: localhost:3306/test
spring:
datasource: # 数据库配置
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${datasource.url}?serverTimezone=UTC
username: root
password: 123456
hikari: #(以下是简单配置,请根据业务自行添加配置)
maximum-pool-size: 10 # 最大连接池数
max-lifetime: 1770000

3、最后一个重要的配置,需要配置。不然会报下面的错误。【这是最重要的一步

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.example.exa.dao.StudentDao' that could not be found.

Action:

Consider defining a bean of type 'com.example.exa.dao.StudentDao' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:8712', transport: 'socket'

Process finished with exit code 1

这是因为没有 DataSourceConfig 这个类。

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
/** * @Description: 数据库配置 */ @Configuration public class DataSourceConfig { /** * BeetlSQL 官方推荐配置 * 使用的是 Hikari 连接池 springboot2.0 默认支持 */ @Bean(name="datasource") public DataSource datasource(Environment env) { HikariDataSource hides = new HikariDataSource(); hides.setJdbcUrl(env.getProperty("spring.datasource.url")); hides.setUsername(env.getProperty("spring.datasource.username")); hides.setPassword(env.getProperty("spring.datasource.password")); hides.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); return hides; } }

相关jar包依赖如下:

<dependency>
     <groupId>com.zaxxer</groupId>
     <artifactId>HikariCP</artifactId>
     <version>3.3.1</version>
      <exclusions>
          <exclusion>
          <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
          </exclusion>
      </exclusions>
</dependency>

保证以上三步是正确的,就正常可以使用beetlsql了。

可以使用自动生成md文件、dao、service、action、pojo工具自动生成代码。

原文地址:https://www.cnblogs.com/liyh321/p/13163320.html