source is null for getProperty(null, “name“)

source is null for getProperty(null, “name”)

这个问题的发生一般是在 Mapper.xml 的 sql语句中出错。

我在项目中使用到了,用到了XML中的判断条件查询方式,代码如下:

<if test="employee.name != null and employee.name != ''">
    AND e.`name` LIKE CONCAT( '%', #{employee.name}, '%' )
</if>

上述语句的简单来说,当传过来的employee.name不等于 null 并且不等于 '' ,就按这个条件查询,这时候运行项目,报错:

source is null for getProperty(null, “name”)

这时候可能有两个原因:

一、你并没有对应的对象参数

你可以查看方法参数中是否有对应的对象参数

例如:我这个方法中要查看是否有 employee

//  错误
List<Employee> getEmployee();

//  正确
List<Employee> getEmployee(Employee employee);
二、有对应对象,但是对象传值为NULL

这种情况可以在外层加入判断:

<if test="employee != null and employee != '' ">
	<if test="employee.name != null and employee.name != ''">
       AND e.`name` LIKE CONCAT( '%', #{employee.name}, '%' )
    </if>
</if>

这样,没有传入任何参数时,也不会报错了

个人博客为:
MoYu’s HomePage

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.core; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class ConnDB { public Connection conn = null; public Statement stmt = null; public ResultSet rs = null; private static String propFileName = "/com/connDB.properties"; private static Properties prop = new Properties(); private static String dbClassName = "com.mysql.jdbc.Driver"; private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=aaaaaa&useUnicode=true"; public ConnDB() { try { InputStream in = this.getClass().getResourceAsStream(propFileName); prop.load(in); dbClassName = prop.getProperty("DB_CLASS_NAME"); dbUrl = prop.getProperty("DB_URL", dbUrl); } catch (Exception var2) { var2.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { Class.forName(dbClassName).newInstance(); conn = DriverManager.getConnection(dbUrl); } catch (Exception var2) { var2.printStackTrace(); System.out.println("wgh:" + dbUrl); } if (conn == null) { System.err.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + dbClassName + "\r\n链接位置1:" + dbUrl); } return conn; } public ResultSet executeQuery(String sql) { try { this.conn = getConnection(); this.stmt = this.conn.createStatement(1004, 1007); this.rs = this.stmt.executeQuery(sql); } catch (SQLException var3) { System.err.println(var3.getMessage()); } return this.rs; } public int executeUpdate(String sql) { boolean var2 = false; int result; try { this.conn = getConnection(); this.stmt = this.conn.createStatement(1004, 1007); result = this.stmt.executeUpdate(sql); } catch (SQLException var4) { result = 0; } return result; } public void close() { try { if (this.rs != null) { this.rs.close(); } if (this.stmt != null) { this.stmt.close(); } if (this.conn != null) { this.conn.close(); } } catch (Exception var2) { var2.printStackTrace(System.err); } } }
05-19

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值