mybatis入门(零) :一些常见的问题

发布于:2023-01-21 ⋅ 阅读:(495) ⋅ 点赞:(0)

1.mybatis常见报错如下

报错信息: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class java.lang.String'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'param' in 'class java.lang.String'

说明在String类 中没有获取到 对应的‘param’(param是输入参数名),查找 相关资料发现是因为向mapper.xml文件中SQL语句输入的参数在使用 ${}(拼接)代替#{}(预编译)进行参数输入,需要将输入的参数进行处理 。因此在mapper对应的接口Java中,对应的查询方法输入参数需要进行处理。在方法参数前添加形如 @Param(value = "param") 的方式,"param"最好与参数名一致。

 Map  selectparam(@Param(value = "param") String param);

另:对于预编译 和 字符串 拼接,虽然预编译可以通过将查询条件与“1=1”一同放在一个查询中,相应的条件参数由“?”代替,但是在mybatis中,如果需要将列名作为参数输入到mapper中进行查询,仍然不可避免的需要进行 字符串拼接。

报错信息:

### Error updating database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for priv.practice.orm.UserMapper.addUser

开始以为是传入参数没有值,排查后发现原因是Mapper.xml的文件名该了,找不到Mapper ,修改为  priv.practice.orm.Mapper.addUser  ,session.insert(addSql,addUser);就正常了。

 另附最终测试代码

package priv.practice.orm;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class TestApply {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String source = "mybatis-config.xml";
		
	SqlSessionFactory  factory = new SqlSessionFactoryBuilder().build
		   (Resources.getResourceAsReader(source) );
	SqlSession session=factory.openSession();
	
	String querySql ="priv.practice.orm.Mapper.selectById";
	User user=(User) session.selectOne(querySql, 5);
	user.toString();

	//  add user
//	User addUser = new User();//"me","ffff"
//	addUser.setName("me");addUser.setPwd("ffff");
//	System.out.println("addUser: "+addUser);
//	String addSql ="priv.practice.orm.Mapper.addUser";
//	int count = session.insert(addSql,addUser);
//	session.commit();
//	System.out.println("count: "+count);
	
	//delete User
	 String deleteUser = "priv.practice.orm.Mapper.deleteUser";
	 session.delete(deleteUser,"kk");
	 session.commit();
	session.close();
		
	
	}

}

mapper文件

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="priv.practice.orm.Mapper">
<select id="selectById" resultType="priv.practice.orm.User">
<!-- priv.practice.orm.Mapper.selectById -->
select * from user where id = #{id}
</select>

<insert id="addUser" parameterType="priv.practice.orm.User" >
<!-- priv.practice.orm.Mapper.addUser  -->
insert into user(name,pwd) values (#{name},#{pwd})
</insert>

<!-- deleteUser -->
<delete id="deleteUser" parameterType="String">
<!-- priv.practice.orm.Mapper.deleteUser -->
delete from user where name=#{name}
</delete>
</mapper>

config文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"
                    value="jdbc:mysql://localhost:3306/person" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
     <!-- priv/practie/orm/Mapper.xml -->
        <mapper resource="priv/practice/orm/Mapper.xml" />
    </mappers>
</configuration>  

xml文件位置

 

config.xml 文件  事务方式 为JDBC ,增加和删除数据需要 commit。

 session.commit();
    session.close(); 

本文含有隐藏内容,请 开通VIP 后查看