I used Mybatis 3.2.5 in a simple project. The structure of th project is :

The content of mybatis-config.xml is
<?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://127.0.0.1:3306/blog_db?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="letian/mybatis/mapper/UserMapper.xml"/>
</mappers>
</configuration>
The table user in blog_db has 2 records.
mysql> select * from user;
+----+--------+----------------+----------+
| id | name | email | password |
+----+--------+----------------+----------+
| 1 | letian | [email protected] | 123 |
| 2 | xiaosi | [email protected] | 123 |
+----+--------+----------------+----------+
2 rows in set (0.00 sec)
The source code of letian.mybatis.bean.User is
package letian.mybatis.bean;
public class User {
private int id;
private String name;
private String email;
private String password;
// ......
// getters and setters
// ......
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
The source code of letian.mybatis.dao.UserMapper is
package letian.mybatis.dao;
import letian.mybatis.bean.User;
public interface UserMapper {
User findById(int id);
}
The source code of letian/mybatis/mapper/UserMapper.xml is
<?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="letian.mybatis.dao.UserMapper">
<select id="findById" parameterType="HashMap" resultType="letian.mybatis.bean.User">
select
* from blog_db.user where id=#{id}
</select>
</mapper>
The source code of Main.java is
import java.io.IOException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import letian.mybatis.bean.User;
import letian.mybatis.dao.UserMapper;
public class Main {
public static void main(String[] args) throws IOException {
SqlSessionFactory sessionFactory;
sessionFactory = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsReader("mybatis-config.xml"));
SqlSession sqlSession = sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById(1);
System.out.println(user);
}
}
Main.java could be executed correctly. If the value of parameterType in <select id="findById">...</select> in letian/mybatis/mapper/UserMapper.xml is set to int or Integer, Main.java could be executed correctly as well.
However, if the content of letian/mybatis/mapper/UserMapper.xml is changed to
<?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="letian.mybatis.dao.UserMapper">
<select id="findById" parameterType="HashMap" resultType="letian.mybatis.bean.User">
select * from
blog_db.user where
<choose>
<when test="id < 0">
id = 0-#{id}
</when>
<otherwise>
id = #{id}
</otherwise>
</choose>
</select>
</mapper>
Main.java cannot be executed and I got the error as follows:
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 'id' in 'class java.lang.Integer'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.Integer'
......
If the value of parameterType in <select id="findById">...</select> in letian/mybatis/mapper/UserMapper.xml is set to int or Integer, Main.java cannot be executed as well.
Though this problem was solved in a different way, I think it's an unreasonable implementation for MyBatis.
Are you sure that parameterType="int" (or "java.lang.Integer") doesn't work?
"Integer" isn't a type or an alias, so that won't work.
Recommend use Integer instead of int,if you can.
And @harawata is true,plz check your param. :)
parameterType is optional and it is usually better to let MyBatis detect it.@Param annotation, you need to use the implicit name _parameter to reference it in the OGNL expression or ${}.@Condor70 @legend0702 I am sure. When I use dynamic SQL, parameterType="int", or "java.lang.Integer", or "Integer", or "HashMap", it doesn't work. I know how to solve it, but just think it is strange that Mybatis can not work in the situation I mentioned above.
Java does not allow us to obtain the parameter name at runtime, unfortunately.
Please search 'java parameter name' for more detailed explanation.
So, as I explained, you may have to 1) modify the xml mapper as follows:
<when test="_parameter < 0">
id = 0-#{id}
</when>
Or 2) add @Param("id") to the method parameter.
User findById(@Param("id")int id);
5楼说的对 顶起
i got this too,and i added annotation @Param and problem solved.
Most helpful comment
Java does not allow us to obtain the parameter name at runtime, unfortunately.
Please search 'java parameter name' for more detailed explanation.
So, as I explained, you may have to 1) modify the xml mapper as follows:
Or 2) add
@Param("id")to the method parameter.