mybatis如何解决实体类中字段和数据表字段不对应引发的问题

2022-08-02 10:27:40

当实体类中设置的字段和数据库表中的字段名称不一致时,会引发问题

实体类代码如下:

package com.cn.vo;
/**
 * 学生实体类
 * */
public class StudentVO {
	private int stuId;        //对应表字段名为stu_id
	private String stuName;   //对应表字段名为stu_name
	private String sex;       //对应表字段名为sex
	
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "StudentVO [stuId=" + stuId + ", stuName=" + stuName + ", sex="
				+ sex + "]";
	}
	
	
}
上面实体类和表字段名不对应


2、实体类映射文件如下:

<?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="com.cn.mapper.StudentMapper">
 		<!-- 1.根据id查询学生信息 -->
 		<select id="selectStudentById" parameterType="int" resultType="StudentVO">
 			select  t.*  from t_student t where t.stu_id=#{stuId} 
 		</select>
 </mapper>


3、测试代码如下:
package com.cn.test1;

import org.apache.ibatis.session.SqlSession;

import com.cn.mapper.StudentMapper;
import com.cn.utils.MybatisUtil;
import com.cn.vo.StudentVO;

public class FindUserById {
	public static void main(String[] args) {
		//1.获取sqlSession 
		SqlSession session=MybatisUtil.getSqlSession();
		//2.获取mapper接口的代理对象
		StudentMapper stuMapper=session.getMapper(StudentMapper.class);
		//3.sessio操作执行查询
		StudentVO stuVO=stuMapper.selectStudentById(2);
		//4.打印结果
		System.out.println(stuVO.toString());
	}
}
测试结果如下:



数据库中记录



很明显:查询结果不对。正是因为数据库表字段和实体类字段不一致引发的问题。那么映射文件该如何配置呢?

配置方式1:

<?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="com.cn.mapper.StudentMapper">
 		<!-- 1.根据id查询学生信息 -->
 		<select id="selectStudentById" parameterType="int" resultType="StudentVO">
 			select t.stu_Id as stuId,
 				   t.stu_name as stuName,
 				   t.sex as sex
 			from t_student t where t.stu_id=#{stuId} 
 		</select>
</mapper>


这样配置后查询的结果就是正确的,但是如果字段太多,每次都这样设置别名会很繁琐,而且工作量太大


配置方式2:通过<resultMap>映射实体类属性名和表的字段名一一对应的关系


<?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="com.cn.mapper.StudentMapper">
 		<!-- 
 			resultMap:将实体类字段属性名和表的字段名进行对应 
 		-->
 		<resultMap id="studentResultMap" type="com.cn.vo.StudentVO" >
 			<!-- 用id属性映射主键字段 -->
 			<id property="stuId" column="stu_id" />
 			<!-- 用result属性来映射非主键字段 -->
 			<result property="stuName" column="stu_name"/>
 			<result property="sex" column="sex" />
 		</resultMap>
 		
 		<!-- 1.根据id查询学生信息 -->
 		<select id="selectStudentById" parameterType="int"  resultMap="studentResultMap">
 			select t.*
 			from t_student t where t.stu_id=#{stuId} 
 		</select>
</mapper>
  • 作者:猿累人生
  • 原文链接:https://blog.csdn.net/sinat_23490433/article/details/78434050
    更新时间:2022-08-02 10:27:40