[英]myBatis. ResultMap and properties
I am trying to use myBatis in my project.我正在尝试在我的项目中使用 myBatis。 For the "Select" method, I used result map
对于“选择”方法,我使用了结果图
<?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="resultMap">
<resultMap id="userMap" type="db.entities.User">
<result column="id" property="id"/>
<result column="login" property="login"/>
<result column="password" property="password"/>
<result column="service_profile" property="serviceProfile.id"/>
<result column="driver_profile" property="driverProfile.id"/>
<result column="premium_expiring_time" property="premiumExpiringDate"/>
<result column="registration_date" property="registrationDate"/>
<result column="last_visit_date" property="lastVisitDate"/>
<result column="authorization_key" property="authorizationKey"/>
<result column="last_altitude" property="lastGeoAltitude"/>
<result column="last_longitude" property="lastGeoLongitude"/>
</resultMap>
</mapper>
And it works, when I get from my function's argument instance of my class它有效,当我从我的类的函数参数实例中获取时
@ResultMap("resultMap.userMap")
@Select("SELECT * FROM users WHERE login = #{login} AND password = #{password}")
fun getUser(user: User): User?
But I think, that it is bad idea, because first I need to create User().但我认为,这是个坏主意,因为首先我需要创建 User()。 When I try to use "login" and "password" in function's arguments, I take an exception:
当我尝试在函数的参数中使用“登录”和“密码”时,我有一个例外:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'login' not found. Available parameters are [arg1, arg0, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'login' not found. Available parameters are [arg1, arg0, param1, param2]
How can I use incoming arguments without creating instance of my class User?如何在不创建类 User 的情况下使用传入参数?
You need to 1) add @Param
to each parameter or 2) add Kotlin compiler option -java-parameters
.您需要 1) 将
@Param
添加到每个参数或 2) 添加 Kotlin 编译器选项-java-parameters
。
1) would look as follows: 1) 将如下所示:
@ResultMap("resultMap.userMap")
@Select("SELECT * FROM users WHERE login = #{login} AND password = #{password}")
fun getUser(
@Param("login") login: String,
@Param("password") password: String): User?
2) requires MyBatis 3.4.1+ and Kotlin 1.1+. 2) 需要 MyBatis 3.4.1+ 和 Kotlin 1.1+。
MyBatis' parameter name resolution is somewhat complicated mainly because of historical reasons. MyBatis 的参数名解析有些复杂,主要是历史原因。
See this answer if you are interested.如果您有兴趣,请参阅此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.