SoJpt Boot
介绍
在Spring Boot框架下使用Jfinal特性极速开发
可以在Spring Boot中向使用Jfinal一样使用Enjoy, controller的一系列方法(如: getFile(), renderFile....),以及ActiveRecord
软件架构
基于 Spring Boot 2.2.0.M1 制作
安装教程
- 在Spring Boot 2.0以上项目中 加入maven坐标
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>com.gitee.sohnny</groupId>
<artifactId>sojpt-boot</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
复制代码
- 添加 MySqlActiveRecordPluginConfiguration 与 SoJptViewResolver配置类
package com.sojpt.jfinal;
import java.sql.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.sojpt.boot.ActiveRecordPluginProperties;
@Configuration
@EnableConfigurationProperties(ActiveRecordPluginProperties.class)
public class MySqlActiveRecordPluginConfiguration{
private static DruidPlugin dp;
private static ActiveRecordPlugin arp;
@Autowired
private ActiveRecordPluginProperties arpProperties;
@Bean
public ActiveRecordPlugin ininitActiveRecordPlugin(){
dp = new DruidPlugin(arpProperties.getJdbcUrl(), arpProperties.getUsername(), arpProperties.getPassword());
WallFilter wallFilter = new WallFilter(); // 加强数据库安全
wallFilter.setDbType("mysql");
dp.addFilter(wallFilter);
dp.addFilter(new StatFilter()); // 添加 StatFilter 才会有统计数据
dp.setDriverClass("com.mysql.cj.jdbc.Driver");
dp.stop();
dp.start();
arp = new ActiveRecordPlugin(dp);
arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
arp.setDialect(new MysqlDialect());
arp.setShowSql(arpProperties.getIsDevMode());
arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
arp.addSqlTemplate("/sql/all_sqls.sql");
//******** 在此添加dao层sql文件 *********//*
//_MappingKit.mapping(arp);
System.out.println("ActiveRecordPlugin --- started");
// 必须手动调用start
arp.stop();
arp.start();
return arp;
}
}
复制代码
package com.sojpt.jfinal;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jfinal.config.Constants;
import com.jfinal.render.RenderManager;
import com.jfinal.template.ext.spring.JFinalViewResolver;
import com.jfinal.template.source.ClassPathSourceFactory;
@Configuration
public class SoJptViewResolver {
@Autowired
private ServletContext servletContext;
@Bean(name = "jfinalViewResolver")
public JFinalViewResolver getJFinalViewResolver() {
JFinalViewResolver jfr = new JFinalViewResolver();
// setDevMode 配置放在最前面
jfr.setDevMode(true);
// 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
jfr.setSourceFactory(new ClassPathSourceFactory());
jfr.setSuffix(".html");
jfr.setContentType("text/html;charset=UTF-8");
jfr.setOrder(0);
//jfr.addSharedFunction("/view/common/_layout.html");
//jfr.addSharedFunction("/view/common/_paginate.html");
initRender();
return jfr;
}
private void initRender() {
//ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext(); //已失效
Constants constants = new Constants();
constants.setDevMode(true);
//constants.setJsonFactory(new FastJsonFactory());
RenderManager.me().init(JFinalViewResolver.engine, constants, servletContext);
}
}
复制代码
- 在 application.properties 配置文件中添加如下
arp.jdbc-url = jdbc:mysql://127.0.0.1/sojpt?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
arp.username = root
arp.password = 123456
arp.is-dev-mode= true
复制代码
- 在spring启动类中添加注解 @ComponentScan("com.sojpt"), 如下:
@ComponentScan("com.sojpt") //添加你定义的包名称
@SpringBootApplication
public class SoJptSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SoJptSpringbootApplication.class, args);
}
}
复制代码
使用说明
- 案例一 返回json
@RestController
public class HelloController extends SoJptController {
@Tx //添加事务 多数据源时: @Tx(value = "configName")
@RequestMapping("/list")
public String index() {
SqlPara sqlPara = Db.getSqlPara("admin_log.select");
Page<Record> page = Db.paginate(1, 10, sqlPara);
return JsonKit.toJson(page)
}
@Tx //添加事务 多数据源时: @Tx(value = "configName")
@RequestMapping("/list/jfinal")
public void index() {
System.out.println(getPara("id")); //获取参数示例
SqlPara sqlPara = Db.getSqlPara("admin_log.select");
Page<Record> page = Db.paginate(1, 10, sqlPara);
renderJson(page);
}
}
复制代码
- 案例二 文件下载
@RestController
public class HelloPageController extends SoJptController {
@RequestMapping("/file")
public void index() {
setAttr("msg", "123123");
renderFile(new File("d://test.txt"));
}
}
复制代码
- 案例三 返回页面
@Controller
public class HelloPageController extends SoJptController {
@RequestMapping("/page")
public String index() {
setAttr("msg", "123123");
return "/view/index.html"; //放在resources目录下
}
@RequestMapping("/page/jfinal")
public void index() {
setAttr("msg", "123123");
render("/view/index.html") //放在resources目录下
}
}
复制代码