Jfinal 的sql语句中 如何使用if判断属性是否为空

 1. controller 层代码:

public void questionInfo(){
        // 获取 产品线id 、 技术点id, 进行查询
        Long productLineId = this.getParaToLong("productLineId");
        Integer level = this.getParaToInt("level");
        String questionTypes = this.getPara("questionTypes");
        String difficulty = this.getPara("difficulty");
        Kv cond = Kv.create().set("productLineId",productLineId).set("questionTypes",questionTypes).set("difficulty",difficulty);

        String sql = null;
        List<TopicInformation> topicInformationList = new ArrayList<TopicInformation>();
        if(level == 1){
            SqlPara sqlPara = Db.getSqlPara("teacher.testFindByProduct", cond);
            topicInformationList = TopicInformation.dao.find(sqlPara);
        }else if(level == 2){
            Long technologyId = this.getParaToLong("technologyId");
            sql = Db.getSql("teacher.testFindByProductAndTechnology");
            topicInformationList = TopicInformation.dao.find(sql,productLineId,technologyId);
        }
}

2.sql语句

#sql("testFindByProduct")
  select * from topic_information where  productLineId = #para(productLineId) and flag = 1
  #if(questionTypes)
    and questionTypes = #para(questionTypes)
  #end
  #if(difficulty)
    and difficulty = #para(difficulty)
  #end
#end

在jfinal3.2中  使用Kv.create().set().....方法,设置属性, 然后使用 SqlPara   获取你想要的SQL语句,

在然后使用  实体类.dao.find(sql), 就可以查询数据库,得到自己想要的数据了。

看了一天半jfinal, 就开始写代码了, 如有错误,请您多多指点。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JFinal 是一款基于 Java 的轻量级 Web 开发框架,支持使用 Java 语言进行 Web 应用开发。JFinal 提供了一个简单的 ORM 框架来操作数据库,使用 JFinal 可以很方便地将文转换为 SQL 语句。 以下是实现步骤: 1. 首先需要在项目引入 jfinal 和 druid 的 jar 包。 2. 在 src/main/resources 目录下创建一个名为 druid.properties 的文件,配置数据库连接信息。例如: ``` jdbcUrl=jdbc:mysql://localhost:3306/test username=root password=123456 driverClassName=com.mysql.jdbc.Driver ``` 3. 在项目创建一个继承自 JFinalConfig 的配置类,配置数据库连接池和路由。例如: ``` public class AppConfig extends JFinalConfig { public void configConstant(Constants constants) { // 配置开发模式 constants.setDevMode(true); } public void configRoute(Routes routes) { // 配置路由 routes.add("/user", UserController.class); } public void configPlugin(Plugins plugins) { // 配置 druid 数据库连接池插件 DruidPlugin druidPlugin = new DruidPlugin( getProperty("jdbcUrl"), getProperty("username"), getProperty("password"), getProperty("driverClassName") ); plugins.add(druidPlugin); // 配置 ActiveRecord 插件 ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin); plugins.add(arp); // 配置 MySQL 数据库方言 arp.setDialect(new MysqlDialect()); } } ``` 4. 创建一个 UserController 类,处理文转 SQL 的请求。例如: ``` public class UserController extends Controller { public void index() { renderText("Welcome to JFinal!"); } public void search() { String keyword = getPara("keyword"); String sql = Db.getSql("user.search", keyword); List<User> userList = User.dao.find(sql); renderJson(userList); } } ``` 5. 在 src/main/resources 目录下创建一个名为 sql 的文件夹,用于存放 SQL 文件。例如: ``` user.search=SELECT * FROM user WHERE name LIKE '%?%' ``` 6. 运行项目,在浏览器访问 http://localhost:8080/user/search?keyword=张三,即可将关键字“张三”转换为 SQL 语句并执行查询。查询结果将以 JSON 格式返回。 以上就是使用 JFinal 实现文转 SQL 的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值