需要使用resultMap的两种情况
- 如果类中的字段名和数据库中的字段名不相符是,需要是使用resultMap (id-stuno)
- 如果类中的属性和数据库中的属性不相符时,需要是使用resultMap(String-VarChar2)
配置流程(第一种情况id-stuno)
所需要用到的jar包 mybatis-3.5.1.jar 和 mysql-connector-java-5.1.0-bin.jar
1.配置mybatis-config.xml
创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 资源配置 -->
<properties resource="database.properties"></properties>
<!-- 别名配置 -->
<typeAliases>
<!--批量设置 默认将包中的所有实体类设置别名,类名小写即别名 -->
<package name="com.zy.pojo" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- mapper配置 -->
<mappers>
<mapper resource="com/zy/mapper/StudentMapper.xml" />
</mappers>
</configuration>
2.创建类--表映射
创建Student.java类
package com.zy.pojo;
public class Student {
private Integer id;
private String stuName;
private String stuClass;
private int stuAge;
private boolean stuSex;
private StudentAddress stuAddress;
public Student() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public boolean isStuSex() {
return stuSex;
}
public void setStuSex(boolean stuSex) {
this.stuSex = stuSex;
}
public StudentAddress getStuAddress() {
return stuAddress;
}
public void setStuAddress(StudentAddress stuAddress) {
this.stuAddress = stuAddress;
}
public Student(Integer id, String stuName, String stuClass, int stuAge, boolean stuSex) {
super();
this.id = id;
this.stuName = stuName;
this.stuClass = stuClass;
this.stuAge = stuAge;
this.stuSex = stuSex;
}
public Student(Integer id, String stuName, String stuClass, int stuAge, boolean stuSex, StudentAddress stuAddress) {
super();
this.id = id;
this.stuName = stuName;
this.stuClass = stuClass;
this.stuAge = stuAge;
this.stuSex = stuSex;
this.stuAddress = stuAddress;
}
@Override
public String toString() {
return "Student [id=" + id + ", stuName=" + stuName + ", stuClass=" + stuClass + ", stuAge=" + stuAge
+ ", stuSex=" + stuSex + ", stuAddress=" + stuAddress + "]";
}
}
创建StudentAddress.java类
package com.zy.pojo;
public class StudentAddress {
private String homeAddress;
private String schoolAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
public StudentAddress(String homeAddress, String schoolAddress) {
super();
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
}
public StudentAddress() {
super();
}
@Override
public String toString() {
return "StudentAddress [homeAddress=" + homeAddress + ", schoolAddress=" + schoolAddress + "]";
}
}
3.创建mapper接口
创建StudentMapper.java接口
package com.zy.mapper;
import java.util.List;
import com.zy.pojo.Student;
public interface StudentMapper {
List<Student> queryAllStudent();
}
4.创建mapper映射文件
创建StudentMapper.xml映射
<?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.zy.mapper.StudentMapper">
<!--查询学生全部信息-->
<select id="queryAllStudent" resultMap="studentAddressMap">
select * from Student
</select>
<!-- 这里的类中id和数据库中的字段stuNo不一致 所以使用resultMap -->
<resultMap type="student" id="studentAddressMap">
<!-- 主键用id 非主键用result property代表着实体类中,column代表着数据库中的字段 -->
<id property="id" column="stuNo" />
<result property="stuName" column="stuName" />
<result property="stuClass" column="stuClass" />
<result property="stuAge" column="stuAge" />
<result property="stuSex" column="stuSex" />
<!-- 一对多 -->
<collection property="stuAddress" ofType="com.zy.pojo.StudentAddress">
<result property="homeAddress" column="homeAddress" />
<result property="schoolAddress" column="schoolAddress" />
</collection>
</resultMap>
</mapper>
5.测试类
创建测试类StudentTest.java
package com.zy.test;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.zy.mapper.StudentMapper;
import com.zy.pojo.Student;
import com.zy.pojo.StudentAddress;
public class StudentTest {
public static void main(String[] args) throws IOException {
queryAllStudent() ;
}
// 全部查询(使用了类型转换器)
private static void queryAllStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
List<Student> allStudent = sqlSession.getMapper(StudentMapper.class).queryAllStudent();
for (Student student : allStudent) {
System.out.println(student);
}
sqlSession.close();
}
}
6.运行结果
7.项目结构
9.数据库中的字段
分析一下:
-
类中的属性名id和数据库中的字段stuNo名不一致,需要使用resultMap将它们对应起来
-
查询的类属性是另外一个实体类对象,可以使用resultMap的一对多
第二种情况的可以使用类型转换器更好一点Mybatis之类型转换器的配置