链接: https://pan.baidu.com/s/1reHZKSvVq7oaKSdl4UvN0w 提取码: 9jvi
必须的前提依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 导入ibeetl全家桶,其中包括应用数据库的ibeetlSQL以及应用前端的ibeetl -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.60.RELEASE</version>
</dependency>
一.整合Beetl前端技术
1.创建conf包,在该包下创建BeetlConf配置类
package com.springbootofbeetlsql.springbootinit.config;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeetlConf {
@Value("templates") String templatesPath; //模板根目录 ,比如 "templates"
@Bean(name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
//获取Spring Boot 的ClassLoader
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader==null){
loader = BeetlConf.class.getClassLoader();
}
//beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
templatesPath);
beetlGroupUtilConfiguration.setResourceLoader(cploder);
beetlGroupUtilConfiguration.init();
//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
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);
beetlSpringViewResolver.setSuffix(".html"); //设置后缀后即可在controller中使用 return "hello" 而不必 return "hello.html"
return beetlSpringViewResolver;
}
}
2.在resources目录下配置beetl.properties
TEMPLATE_CHARSET=UTF-8
ENGINE=org.beetl.core.engine.DefaultTemplateEngine
# 定义 共享数据引用符号
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
# 定义开始符号 注意:这只是公司的项目开发规范,官方的开发规范分别为<%代表开始,%>代表结束
DELIMITER_STATEMENT_START=@
DELIMITER_STATEMENT_END=
# 设置编码为utf-8
TEMPLATE_CHARSET = UTF-8
# 是否支持自定义标签
HTML_TAG_SUPPORT = true
# FNP.strutil = org.beetl.ext.fn.StringTuil
# FNP.reg = org.beetl.ext.fn.RegxFunctionUtil
3.在Controller层创建Web进行测试
@Controller
public class WebController {
@GetMapping("/index")
public String getInfoTest(HttpServletRequest req) {
//通过request共享数据.
req.setAttribute("paraminfo", "hello,world");
return "test";//指向一个要共享数据并跳转的页面,因为在配置类中设置了省略后缀,所以不需要'.html'
}
}
4.在resources/templates下创建html页面文件
test.html
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
${paraminfo}
</body>
</html>
二.整合BeetlSQL数据库操作技术
1.引入依赖
<!-- mysql的驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- BeetlSQL所需要的Hikari数据池 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
2.创建conf包,在该包下创建DataSourceConfig配置类
package com.springbootofbeetlsql.springbootinit.config;
import javax.sql.DataSource;
import org.beetl.sql.ext.spring4.BeetlSqlDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.zaxxer.hikari.HikariDataSource;
//用于配置BeetlSQL
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
public DataSource dataSource(Environment environment) {
//配置数据源连接器,此处使用hikari,你也可以换为Druid
HikariDataSource dataSourceObjForHikari = new HikariDataSource();
dataSourceObjForHikari.setJdbcUrl(environment.getProperty("spring.datasource.url"));
dataSourceObjForHikari.setUsername(environment.getProperty("spring.datasource.username"));
dataSourceObjForHikari.setPassword(environment.getProperty("spring.datasource.password"));
dataSourceObjForHikari.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
return dataSourceObjForHikari;
}
@Bean
public BeetlSqlDataSource beetlSqlDataSource(@Qualifier("dataSource") DataSource dataSource) {
BeetlSqlDataSource source = new BeetlSqlDataSource();
source.setMasterSource(dataSource);
return source;
}
}
3.在resources目录下配置application.properties
# mysql数据库连接配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/param?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 用于存放sql语句文件的目录
beetlsql.sqlPath=/sql
# Dao层的包位置
beetlsql.basePackage=com.springbootofbeetlsql.springbootinit.dao
beetl-beetlsql.dev=true
# 默认是org.beetl.sql.core.UnderlinedNameConversion,能将下划线分割的数据库命名风格转化为java驼峰命名风格,
# 还有常用的DefaultNameConversion,数据库命名完全和Java命名一直,以及JPA2NameConversion,兼容JPA命名
# beetlsql.nameConversion=DefaultNameConversion
beetlsql.daoSuffix=Dao
beetlsql.dbStyle=org.beetl.sql.core.db.MySqlStyle
# 指定表字段映射规则 此处的数据代表Java实体类字段是什么样的,数据表字段就也是什么样的。
beetlsql.nameConversion=org.beetl.sql.core.DefaultNameConversion