SpringCloud分佈式微服務b2b2c電子商務(五)springboot整合 beatlsql

BeetSql是一個全功能DAO工具, 同時具備Hibernate 優勢 & Mybatis優勢功能,適用於認可以SQL爲中心,同時又需求工具能自動能生成大量經常使用的SQL的應用。
beatlsql 優勢
開發效率
無需註解,自動使用大量內置SQL,輕易完成增刪改查功能,節省50%的開發工做量
數據模型支持Pojo,也支持Map/List這種快速模型,也支持混合模型SQL 模板基於Beetl實現,更容易寫和調試,以及擴展維護性
SQL 以更簡潔的方式,Markdown方式集中管理,同時方便程序開發和數據庫SQL調試。
能夠自動將sql文件映射爲dao接口類
靈活直觀的支持支持一對一,一對多,多對多關係映射而不引入複雜的OR Mapping概念和技術。
具有Interceptor功能,能夠調試,性能診斷SQL,瞭解springcloud架構能夠加求求:三五三六二四七二五,以及擴展其餘功能其餘內置支持主從數據庫支持的開源工具支持跨數據庫平臺,開發者所需工做減小到最小,目前跨數據庫支持mysql,postgres,oracle,sqlserver,h2,sqllite,DB2.
引入依賴html

<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>

這幾個依賴都是必須的。java

整合階段
因爲springboot沒有對 beatlsql的快速啓動裝配,因此須要我本身導入相關的bean,包括數據源,包掃描,事物管理器等。mysql

在application加入如下代碼:web

@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:spring

restart.include.beetl=/beetl-2.3.2.jar
restart.include.beetlsql=/beetlsql-2.3.1.jar

在templates下加一個index.btl文件。sql

加入jar和配置beatlsql的這些bean,以及resources這些配置以後,springboot就可以訪問到數據庫類。數據庫

舉個restful的栗子
初始化數據庫的表springboot

# 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');

beanrestful

public class Account {
    private int id ;
    private String name ;
    private double money;

    getter...

    setter...

  }
相關文章
相關標籤/搜索