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

引用BeetSql官网的一段话:

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

1、pom.xml相关依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>



<groupId>com.example</groupId>

<artifactId>fly-beetl</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>



<name>fly-beetl</name>

<description>Demo project for Spring Boot</description>



<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.4.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>



<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>



<dependencies>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>



<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.2</version>

</dependency>



<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>



<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>



<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>



<dependency>

<groupId>com.ibeetl</groupId>

<artifactId>beetl</artifactId>

<version>2.8.5</version>

</dependency>

<dependency>

<groupId>com.ibeetl</groupId>

<artifactId>beetlsql</artifactId>

<version>2.10.30</version>

</dependency>

<dependency>

<groupId>com.ibeetl</groupId>

<artifactId>beetl-framework-starter</artifactId>

<version>1.1.35.RELEASE</version>

</dependency>

<dependency>

<groupId>com.zaxxer</groupId>

<artifactId>HikariCP</artifactId>

<version>3.2.0</version>

</dependency>



<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-dbcp2</artifactId>

<version>2.4.0</version>

</dependency>

</dependencies>



<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

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<User>{
}

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<User> getUserList(String name){
        Query<User> query = sqlManager.query(User.class);
        List<User> 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);
    }

}

访问测试结果:

猜你喜欢

转载自blog.csdn.net/qq_756589808/article/details/82862454