source is null for getProperty(null, "xxx")

本文介绍了一个关于MyBatis查询时出现的空指针异常问题,并给出了具体的解决方案。异常出现在尝试获取空对象的属性时,通过使用动态SQL进行条件判断避免了该问题。
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'itemCustom.title'. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "title")
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy24.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy31.findItemList(Unknown Source)
at com.taotao.service.impl.ItemServiceImpl.findItemsList(ItemServiceImpl.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)


部分异常,显示为source is null for getProperty(null, "title"),其实就是itemCustom对象为空,一个空指针异常,所以获取不到title属性,之前的mapper.xml代码为

	<select id="findItemList" parameterType="com.taotao.wto.ItemCustomVo"
		resultType="com.taotao.wto.ItemCustom">
	</select>
	
<!--为sql加上验证,另外使用动态sql: -->

	<sql id="query_items_where">
		<!-- 使用动态sql,即要通过if判断,判断的是查询条件 -->
		<if test="itemCustom!=null">
			<if test="itemCustom.title!=null and itemCustom.title!='' ">
				tb_item.title LIKE '%${itemCustom.title}%'
			</if>
		</if>
	</sql>
	<select id="findItemList" parameterType="com.taotao.wto.ItemCustomVo"
		resultType="com.taotao.wto.ItemCustom">
	</select>
			SELECT tb_item.* FROM tb_item 
		<where>
			<include refid="query_items_where"></include>
		</where>
	</select>


                
这是pom.xml文件:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>nosql_chapter06</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <dependencies> <!--单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--java操作mongoDB的驱动依赖--> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.1</version> </dependency> </dependencies> </project> 这是mongodb.properties: host1=192.168.85.101 host2=192.168.85.102 host3=192.168.85.103 port=27017 username=itcastAdmin password=123456 source=admin dbname=testfiles 这是MongoUtils.java package com.itcast.mongodb; import com.mongodb.MongoClientSettings; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoDatabase; import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBuckets; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Properties; public class MongoUtils { private static Properties properties; private static InputStream stream = null; private static String host1; private static String host2; private static String host3; private static String port; private static String dbname; private static String username; private static String password; private static String source; //1.创建一个静态代码块,用于初始化工具类中的静态变量,该静态代码块在类加载过程中的初始化阶段执行,并且只执行一次 static { //判断properties集合对象是否为空,为空则创建一个集合对象 if (properties == null) { properties = new Properties(); } /* 由于我们调用load方法,而load方法底层抛出了一个IOException异常,此异常为编译 时期异常。所以,我们调用load方法时,需要处理底层抛过来的异常*/ try { //创建一个InputStream字节输入流对象,用于接收mongodb.properties配置文件中的配置参数 stream = MongoUtils.class.getClassLoader().getResourceAsStream("mongodb.properties"); //properties集合对象调用load()方法,将配置参数加载到properties集合中 properties.load(stream); } catch (IOException e) { e.printStackTrace(); } //根据mongodb.properties配置文件中的key,获取value值 host1 = properties.getProperty("host1"); host2 = properties.getProperty("host2"); host3 = properties.getProperty("host3"); port = properties.getProperty("port"); dbname = properties.getProperty("dbname"); source = properties.getProperty("source"); username = properties.getProperty("username"); password = properties.getProperty("password"); } //2.定义一个getMongoClient()方法,用于获取MongoDB副本集的连接对象 public static MongoClient getMongoClient() { //指定用户名、用户认证书库、密码进行身份验证 MongoCredential credential = MongoCredential.createCredential(username, source, password.toCharArray()); //连接mongodb副本集 MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder().applyToClusterSettings( builder ->builder.hosts( Arrays.asList( new ServerAddress("192.168.85.101", 27017), new ServerAddress("192.168.85.102", 27017), new ServerAddress("192.168.85.103", 27017) ))) .credential(credential) .build()); return mongoClient; } //3.定义一个getGridFSConn()方法,用于实现连接指定的MongoDB GridFS中的数据库 public static GridFSBucket getGridFSConn() { MongoClient mongoClient = getMongoClient(); MongoDatabase mongoDatabase = mongoClient.getDatabase(dbname); //获取GridFS中数据库连接 GridFSBucket gridFSBucket = GridFSBuckets.create(mongoDatabase); return gridFSBucket; } }
最新发布
11-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值