SoJpt Boot 1.1发布 (在Spring Boot框架下完美使用Jfinal特性)

   在Spring Boot框架下使用Jfinal特性极速开发,可以在Spring Boot中向使用Jfinal一样使用Enjoy, controller的一系列方法(如: getFile(), renderFile....),以及ActiveRecord

SoJpt Boot

Spring Boot框架下使用Jfinal特性极速开发

可以在Spring Boot中向使用Jfinal一样使用Enjoy, controller的一系列方法(如: getFile(), renderFile....),以及ActiveRecord

软件架构

基于 Spring Boot 2.2.0.M1 制作

安装教程

 1、在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>

 2、添加 MySqlActiveRecordPluginConfiguration 与 SoJptViewResolver配置类

@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;
        
}
}

@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() {
        Constants constants = new Constants();
        constants.setDevMode(true);
        //constants.setJsonFactory(new FastJsonFactory());
        RenderManager.me().init(JFinalViewResolver.engine, constants, servletContext);
    }
    
}

3、在 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

4、在spring启动类中添加注解 @ComponentScan("com.sojpt"), 如下:

@ComponentScan("com.sojpt") //添加你定义的包名称
@SpringBootApplication
public class SoJptSpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SoJptSpringbootApplication.class, args);
    }
}

5、使用说明

1. 案例一 返回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);
    }
    
}

2. 案例二 文件下载

@RestController
public class HelloPageController extends SoJptController {
    
    @RequestMapping("/file")
    public void index() {
        setAttr("msg", "123123");
        renderFile(new File("d://test.txt"));
    }
    
}

3. 案例三 返回页面

@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目录下
    }
    
}

www.sojpt.com


评论区

热门分享

扫码入社