当实体类中的属性与数据库中的字段名不匹配时,结果不会将数据存入实体类中
解决方法一:
select * from t_user
将原来的SQL语句改成(起别名):
select id,username,password as pwd,gender,regist_time from t_user
<resultMap id="userMap" type="User">
<result column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="pwd"/>
<result column="gender" property="gender"/>
<result column="regist_time" property="regist_time"/>
resultMap>
<select id="selectUsers" resultMap="userMap">
select * from t_user
select>
运行结果:
注意:对于数据库其他字段和实体类中的属性相一致时,我们可以不必进行一一映射,只需要将对应的字段和属性映射即可
<resultMap id="userMap" type="User">
<result column="password" property="pwd"/>
resultMap>
<select id="selectUsers" resultMap="userMap">
select * from t_user
select>
使用java实现sql语句
select s.id,s.`name`,t.`name` as 授课老师 from `student` s,`teacher` t where s.tid=t.id
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO teacher(`id`, `name`) VALUES (1, '陈老师');
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');
@Data
public class Student {
//学生的id
private int id;
//学生的名字
private String name;
//多个学生可以是同一个老师,即多对一
private Teacher teacher;
}
Teacher类:
@Data
public class Teacher {
//老师id
private int id;
//老师的名字
private String name;
}
public interface StudentMapper {
//查询学生对应的所有老师
List<Student> queryStudents();
}
按查询嵌套处理
<mapper namespace="dao.StudentMapper">
<resultMap id="students" type="Student">
<association property="teacher" javaType="Teacher" column="tid" select="getTeacher"/>
resultMap>
<select id="queryStudents" resultMap="students" >
select * from student
select>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{tid}
select>
mapper>
<mappers>
<mapper class="dao.StudentMapper"/>
mappers>
public class Test {
@org.junit.Test
public void test(){
//通过封装好的工具类获取SqlSession会话
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//通过接口类型class获取接口对象实例(动态代理)
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//执行接口中的方法
List<Student> students = mapper.queryStudents();
for (Student student : students) {
System.out.println(student);
}
sqlSession.commit();
//关闭SqlSession
sqlSession.close();
}
}
Mapper.xml配置文件中sql语句:
<resultMap id="students" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
association>
resultMap>
<select id="queryStudents" resultMap="students" >
select s.`id` sid,s.`name` sname,t.`name` tname from `student` s,`teacher` t where s.tid=t.id
select>
结果:
@Data
public class Student {
private int id;
private String name;
private int tid;
}
Teacher类
@Data
public class Teacher {
private int id;
private String name;
//一个老师对应多个学生
private List<Student> students;
}
public interface TeacherMapper {
//通过老师的id查询所有的学生
Teacher getTeacherById(@Param("id")int id);
}
按结果嵌套处理
3. Mapper.xml配置文件配置
<mapper namespace="dao.TeacherMapper">
<select id="getTeacherById" resultMap="TeacherToStudents" parameterType="int">
select s.id sid,s.`name` sname,s.tid tid,t.`name` tname
from student s,teacher t where s.tid=t.id and t.id=#{id}
select>
<resultMap id="TeacherToStudents" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" javaType="ArrayList" ofType="Student" >
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
collection>
resultMap>
mapper>
<mappers>
<mapper class="dao.TeacherMapper"/>
mappers>
public class Test {
@org.junit.Test
public void test(){
//通过封装好的工具类获取SqlSession会话
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//通过接口类型class获取接口对象实例(动态代理)
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
//执行接口中的方法
Teacher teacher = mapper.getTeacherById(1);
System.out.println(teacher);
sqlSession.commit();
//关闭SqlSession
sqlSession.close();
}
}
Mapper.xml配置文件配置:
<mapper namespace="dao.TeacherMapper">
<select id="getTeacherById" resultMap="TeacherToStudents" parameterType="_int">
select id,name
from teacher where id=#{id}
select>
<resultMap id="TeacherToStudents" type="Teacher">
<result property="id" column="id"/>
<result property="name" column="name"/>
<collection property="students" javaType="ArrayList" ofType="Student" select="queryStudentsByUid" column="id" >
collection>
resultMap>
<select id="queryStudentsByUid" resultType="Student">
select id,name,tid from student where tid=#{id}
select>
mapper>
注意collection 中的