Mybatis当中xml映射文件resultType返回类型

2022-06-26 12:49:55

这篇文章主要讲述Mybatis当中xml映射文件resultType返回类型,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

1、返回Map

一般用于报表功能,不想写实体类的时候使用。

public Map<String, Object>getEmpByIdReturnMap(Integer id);
<select id="getEmpByIdReturnMap" resultType="map">
	select* from tbl_employee where id=#{id}</select>

2、返回List

一般用于根据名称模糊查询列表等功能

public List<Employee>getEmpsByLastNameLike(String lastName);
<select id="getEmpsByLastNameLike" resultType="com.gzl.mybatis.bean.Employee">
	select* from tbl_employee where last_name like #{lastName}</select>

3、返回复杂Map

多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key

@MapKey("id")
public Map<Integer, Employee>getEmpByLastNameLikeReturnMap(String lastName);
<select id="getEmpByLastNameLikeReturnMap" resultType="com.gzl.mybatis.bean.Employee">
	select* from student where last_name like #{lastName}</select>

4、返回pojo

一般用于根据主键id查对象

public EmployeegetEmpById(Integer id);
<select id="getEmpById" resultType="com.gzl.mybatis.bean.Employee">
	select* from tbl_employee where id= #{id}</select>

5、返回Boolean

以修改语句为例:成功自动返回true

public booleanupdateEmp(Employee employee);
<update id="updateEmp">
	update tbl_employee 
	set last_name=#{lastName},email=#{email},gender=#{gender}
	where id=#{id}</update>

6、返回Long

以新增返回主键为例,mysql案例

public LongaddEmp(Employee employee);
<insert id="addEmp" parameterType="com.gzl.mybatis.bean.Employee"
	useGeneratedKeys="true" keyProperty="id" databaseId="mysql">
	insert intostudemt(last_name,email,gender)values(#{lastName},#{email},#{gender})</insert>

7、返回void

这里以删除为例

public voiddeleteEmpById(Integer id);
<delete id="deleteEmpById">
	delete from tbl_employee where id=#{id}</delete>

总结

resultType:如果返回的是一个集合,要写集合中元素的类型

  • 作者:怪咖软妹@
  • 原文链接:https://blog.csdn.net/weixin_43888891/article/details/109589985
    更新时间:2022-06-26 12:49:55