SpringBoot入门教程(七)--Spring Boot集成BeetlSql

引用BeetSql官网的一段话:

  BeetSql是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。具体介绍可自行登录官网查看。

1、pom.xml相关依赖





4.0.0



com.example

fly-beetl

0.0.1-SNAPSHOT

jar



fly-beetl

Demo project for Spring Boot





org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

 







UTF-8

UTF-8

1.8











org.springframework.boot

spring-boot-starter-jdbc







org.springframework.boot

spring-boot-starter-web







org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2







mysql

mysql-connector-java

runtime







org.projectlombok

lombok

true







org.springframework.boot

spring-boot-starter-test

test







org.projectlombok

lombok







com.ibeetl

beetl

2.8.5





com.ibeetl

beetlsql

2.10.30





com.ibeetl

beetl-framework-starter

1.1.35.RELEASE





com.zaxxer

HikariCP

3.2.0







org.apache.commons

commons-dbcp2

2.4.0













org.springframework.boot

spring-boot-maven-plugin







 

2、配置application.properties文件

server.port=9090

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/s_shop?useUnicode=true&characterEncoding=utf-8&useSSL=true

spring.datasource.username=root

spring.datasource.password=lin123

 

3、配置beet.properties文件

#默认配置

ENGINE=org.beetl.core.engine.DefaultTemplateEngine

DELIMITER_PLACEHOLDER_START=${

DELIMITER_PLACEHOLDER_END=}

DELIMITER_STATEMENT_START=<%

DELIMITER_STATEMENT_END=%>

DIRECT_BYTE_OUTPUT = FALSE

HTML_TAG_SUPPORT = true

HTML_TAG_FLAG = #

HTML_TAG_BINDING_ATTRIBUTE = var

NATIVE_CALL = TRUE

TEMPLATE_CHARSET = UTF-8

ERROR_HANDLER = org.beetl.core.ConsoleErrorHandler

NATIVE_SECUARTY_MANAGER= org.beetl.core.DefaultNativeSecurityManager

MVC_STRICT = FALSE



#资源配置,resource后的属性只限于特定ResourceLoader

RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader

#classpath 根路径

#RESOURCE.root= /templates/

#是否检测文件变化,开发用true合适,但线上要改为false

RESOURCE.autoCheck= true

 

4、编写BeetlConf类,启动项目时加载该类进行初始化

package com.example.config;

import lombok.extern.slf4j.Slf4j;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.beetl.sql.core.ClasspathLoader;
import org.beetl.sql.core.Interceptor;
import org.beetl.sql.core.UnderlinedNameConversion;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.beetl.sql.ext.spring4.BeetlSqlScannerConfigurer;
import org.beetl.sql.ext.spring4.SqlManagerFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


/**
 * @description:
 * @author: Martin
 * @create: 2018-08-02 11:34
 * @version: 1.0
 **/
@Configuration
@Slf4j
public class BeetlConf {

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
        beetlGroupUtilConfiguration.setResourceLoader(classpathResourceLoader);
        beetlGroupUtilConfiguration.init();
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setPrefix("/templates/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }

    //=============  以下是beetsql配置  =============
    @Bean(name = "beetlSqlScannerConfigurer")
    public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
        BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
        //扫面dao所在的包位置
        conf.setBasePackage("com.example.dao");
        //扫描的类是已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(Environment env) {
        String url = env.getProperty("spring.datasource.url");
        String userName = env.getProperty("spring.datasource.username");
        String password = env.getProperty("spring.datasource.password");
        return DataSourceBuilder.create().url(url).username(userName).password(password).build();
    }

    /**
     * 开启事务
     */
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
        DataSourceTransactionManager dsm = new DataSourceTransactionManager();
        dsm.setDataSource(datasource);
        return dsm;
    }
}

5、编写UserDao继承BaseMapper

package com.example.dao;

import com.example.entity.User;
import org.beetl.sql.core.mapper.BaseMapper;

/**
 * @description:
 * @author: Martin
 * @create: 2018-08-02 11:31
 * @version: 1.0
 **/
public interface UserDao extends BaseMapper{
}

 

6、编写UserService

package com.example.service;

import com.example.dao.UserDao;
import com.example.entity.User;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: Martin
 * @create: 2018-08-02 11:41
 * @version: 1.0
 **/
@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    SQLManager sqlManager;
    public User getUserInfo(Integer id){
        return userDao.unique(id);
    }

    public List getUserList(String name){
        Query query = sqlManager.query(User.class);
        List list = query.andLike("name","%"+name+"%").select();
        return list;
    }
}

7、编写Controller

package com.example.controller;

import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description:
 * @author: Martin
 * @create: 2018-08-02 11:44
 * @version: 1.0
 **/
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping("/getUserInfo")
    public Object getUserInfo(@RequestParam(value = "id") Integer id) {
        return userService.getUserInfo(id);
    }

    @ResponseBody
    @GetMapping("/getUserInfoList")
    public Object getUserInfoList(@RequestParam(value = "name") String name) {
        return userService.getUserList(name);
    }

}

访问测试结果:

SpringBoot入门教程(七)--Spring Boot集成BeetlSql_第1张图片

 

 

你可能感兴趣的:(Java框架整合开发,Spring,Boot)