Mybatis(问题一)开发过程中常见的问题

问题一:在XML配置文件中语句的书写与对象中定义的不一致,导致编译报错

1、对象实体类

package com.flx.dto;

public class Message {
	private int id;
	private String username;
	private String password;
	private String sex;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Message [id=" + id + ", username=" + username + ", password="
				+ password + "]";
	}

}

2、XML配置文件中的SQL语句

<?xml version="1.0" encoding="UTF-8"?>
<!--

       Copyright 2009-2016 the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="Message">

<resultMap type="com.flx.dto.Message" id="MessageResult">
	<id column="id" jdbcType="INTEGER" property="id"/>
	<result column="username" jdbcType="VARCHAR" property="username"/>
	<result column="password" jdbcType="VARCHAR" property="password"/>
</resultMap>

<!-- 
select 元素有很多种配置
id:命名空间中唯一标识,也可以被用来引用这条语句
parameterType:将会传进这条语句中的参数类的完全限制名或者别名
resultMap:引用外部的resultMap
resultType:从这条语句中但会期望的完全限制名或者别名,和resultMap不能同时使用

ONGL:是一种特殊的表达式,它可以支持JAVA语法
  "":需要进行在XMl中需要进行转义
mybatis:
	#{sex}:是在mybatis中处理的,在读取到#好时候会替换掉
 -->
<select id="getMessageList" parameterType="com.flx.dto.Message" resultMap="MessageResult">
  select id,username,password from messages where 1 = 1
  <if test="sex != null and !"".equals(sex.trim())">
  	and sex = #{sex}
  </if>
   <if test="username != null and !"".equals(username.trim())">
  	and username = #{username}
  </if>
  <if test="password != null and !"".equals(password.trim())">
  	and passwprd = #{passwprd}
  </if>
</select>

<!-- <insert id="" parameterType="">
	insert into messages 
</insert> -->

</mapper>

3、报错截图

配置文件错误

运行报错:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'passwprd' in 'class com.flx.dto.Message'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'passwprd' in 'class com.flx.dto.Message'
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
	at com.flx.dao.MessageDAO.getMessageList(MessageDAO.java:27)
	at com.flx.dao.MessageDAO.main(MessageDAO.java:65)
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'passwprd' in 'class com.flx.dto.Message'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:419)
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219)
	at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
	... 3 more


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值