在 mybatis 中,返回值类型和参数传递是 mapper 接口中至关重要的两个方面。正确理解和使用它们可以帮助我们高效、准确地进行数据库操作。下面将详细讲解 mybatis 中的返回值类型和参数传递。
返回值类型
mybatis 支持多种返回值类型,帮助开发者根据需求灵活选择合适的类型进行处理。主要包括以下几种:
1. 单一对象返回
当 sql 查询结果为一行数据时,可以使用单一对象作为返回值类型。
示例
假设我们有一个 user 表:
create table user (
id int primary key,
username varchar(255),
email varchar(255),
age int
);对应的 user 实体类:
package com.example.entity;
public class user {
private integer id;
private string username;
private string email;
private integer age;
// getters and setters
}mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
import org.apache.ibatis.annotations.select;
public interface usermapper {
@select("select * from user where id = #{id}")
user selectuserbyid(int id);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbyid" resulttype="com.example.entity.user">
select * from user where id = #{id}
</select>
</mapper>在以上代码中,selectuserbyid 方法返回 user 对象,因为该 sql 语句预期返回单条记录。
2. 集合返回
当 sql 查询结果为多行数据时,可以使用集合类型(如 list、set)作为返回值类型。
示例
假设我们需要查询所有用户:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
import java.util.list;
public interface usermapper {
list<user> selectallusers();
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectallusers" resulttype="com.example.entity.user">
select * from user
</select>
</mapper>在以上代码中,selectallusers 方法返回 list<user>,因为该 sql 语句预期返回多条记录。
3. 原始数据类型返回
当 sql 查询结果为单行单列数据时,可以使用原始数据类型或其包装类作为返回值类型。
示例
假设我们需要获取用户总数:
mapper 接口方法及 xml 配置:
package com.example.mapper;
public interface usermapper {
int getusercount();
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="getusercount" resulttype="int">
select count(*) from user
</select>
</mapper>在以上代码中,getusercount 方法返回 int,因为该 sql 语句预期返回单行单列数据。
4. map 返回
mybatis 允许返回 map 类型的数据,特别适用于动态查询和多表联查。
示例
假设我们有一个需要获取用户名和邮箱的场景:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import java.util.map;
public interface usermapper {
map<string, object> getusermapbyid(int id);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="getusermapbyid" resulttype="map">
select username, email from user where id = #{id}
</select>
</mapper>在以上代码中,getusermapbyid 方法返回 map<string, object>,因为该 sql 语句返回的字段是多个,mybatis 会将列名作为键,列值作为值存入 map 中。
5. 自定义结果映射
除了自动映射结果,mybatis 还支持自定义结果映射,通过 resultmap 进行复杂的映射操作。
示例
假设我们有一个更复杂的用户表结构,涉及多个实体类之间的映射:
create table address (
id int primary key,
user_id int,
street varchar(255),
city varchar(255),
country varchar(255)
);address 实体类:
package com.example.entity;
public class address {
private integer id;
private string street;
private string city;
private string country;
// getters and setters
}我们需要将 user 和 address 进行关联映射:
package com.example.entity;
import java.util.list;
public class user {
private integer id;
private string username;
private string email;
private integer age;
private list<address> addresses;
// getters and setters
}自定义 resultmap 映射:
<mapper namespace="com.example.mapper.usermapper">
<resultmap id="userresultmap" type="com.example.entity.user">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="email" column="email"/>
<result property="age" column="age"/>
<collection property="addresses" oftype="com.example.entity.address">
<id property="id" column="address_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="country" column="country"/>
</collection>
</resultmap>
<select id="selectuserwithaddresses" resultmap="userresultmap">
select u.id, u.username, u.email, u.age,
a.id as address_id, a.street, a.city, a.country
from user u
left join address a on u.id = a.user_id
where u.id = #{id}
</select>
</mapper>在这个示例中,resultmap 用于将查询结果映射到嵌套的 java 对象中,使用 collection 元素表示一个一对多的关系。
参数传递
mybatis 提供了多种方式来传递参数到 sql 语句中。理解参数传递机制可以提高代码的可读性和灵活性。
1. 单个参数
当方法只有一个参数时,可以直接在 sql 语句中使用 #{paramname} 或 #{param1} 来引用该参数。
示例
假设我们需要根据用户名查询用户信息:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
public interface usermapper {
user selectuserbyusername(string username);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbyusername" resulttype="com.example.entity.user">
select * from user where username = #{username}
</select>
</mapper>在以上代码中,方法有一个 username 参数,sql 语句中直接使用 #{username} 进行引用。
2. 多个参数
当方法有多个参数时,mybatis 会默认将这些参数包装成一个 map,键名为 param1、param2 等。
示例
假设我们需要根据用户名和年龄查询用户信息:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
public interface usermapper {
user selectuserbyusernameandage(string username, int age);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbyusernameandage" resulttype="com.example.entity.user">
select * from user where username = #{param1} and age = #{param2}
</select>
</mapper>在以上代码中,方法有两个参数,sql 语句中使用 #{param1} 和 #{param2} 分别引用。
3. 使用 @param 注解
mybatis 提供了 @param 注解用于指定参数名称,以增强代码可读性。
示例
假设我们需要根据用户名和邮箱查询用户信息:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
import org.apache.ibatis.annotations.param;
public interface usermapper {
user selectuserbyusernameandemail(@param("username") string username, @param("email") string email);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbyusernameandemail" resulttype="com.example.entity.user">
select * from user where username = #{username} and email = #{email}
</select>
</mapper>在以上代码中,使用 @param 注解指定参数名称,sql 语句中可以直接使用 #{username} 和 #{email} 引用。
4. 使用 map 参数
可以使用 map 对象传递多个参数,适合动态参数场景。
示例
假设我们需要根据动态条件查询用户信息:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
import java.util.map;
public interface usermapper {
user selectuserbyparams(map<string, object> params);
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbyparams" resulttype="com.example.entity.user">
select * from user
where username = #{username}
<if test="email != null">
and email = #{email}
</if>
<if test="age != null">
and age = #{age}
</if>
</select>
</mapper>在以上代码中,selectuserbyparams 方法的参数是一个 map 对象,sql 语句中可以使用 #{username}、#{email} 和 #{age} 引用。
5. 使用 pojo 作为参数
可以使用 pojo 对象传递参数,将多个字段封装在一个对象中。
示例
假设我们需要根据用户的多个属性查询用户信息:
mapper 接口方法及 xml 配置:
package com.example.mapper;
import com.example.entity.user;
import com.example.entity.usersearchcriteria;
public interface usermapper {
user selectuserbycriteria(usersearchcriteria criteria);
}usersearchcriteria 类:
package com.example.entity;
public class usersearchcriteria {
private string username;
private string email;
private integer age;
// getters and setters
}xml 配置:
<mapper namespace="com.example.mapper.usermapper">
<select id="selectuserbycriteria" resulttype="com.example.entity.user">
select * from user
where username = #{username}
<if test="email != null">
and email = #{email}
</if>
<if test="age != null">
and age = #{age}
</if>
</select>
</mapper>在以上代码中,selectuserbycriteria 方法使用 usersearchcriteria 对象作为参数,sql 语句中可以使用 #{username}、#{email} 和 #{age} 引用。
以上是 mybatis 中关于返回值类型和参数传递的详细讲解。理解这些概念可以帮助你更高效地使用 mybatis 进行数据库操作。
到此这篇关于mybatis 返回值类型和参数传递的文章就介绍到这了,更多相关mybatis 返回值类型和参数传递内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论