/**
* 属性和表的列名不相同时的处理方法
* 1.sql中给列重新命名:
* select tid id, tname name from teacher t where tid=#{id}
* 2.在teacherMapper.xml中使用resultMap标签
* <select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
* select * from teacher where tid=#{id}
* </select>
* <resultMap type="Teacher" id="getTeacherMap">
* <id property="id" column="tid"/>
* <result property="name" column="tname"/>
* </resultMap>
* @author 魅力_小生
*
*/
mysql表:Teacher[tid, tname], Java 实体类:Teacher[id, name]
建表语句:
create table
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL auto_increment,
`tname` varchar(20) default NULL,
PRIMARY KEY (`tid`)
) 添加2条数据:
1 老师A
2 老师B
Teacher实体类:
public class Teacher {
private int id;
private String name; public Teacher() {
super();
}
public Teacher(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
} }
一、第一种处理方法:sql中给列重新命名
select tid id, tname name from teacher t where tid=#{id}
1.1 写实体类关联的MapperXML:
<mapper namespace="com.mlxs.mybatis.test3.teacherMapper">
<!--
* 属性和表的列名不相同时的处理方法
* 1.sql中给列重新命名:
* select tid id, tname name from teacher t where tid=#{id}
-->
<select id="getTeacher" parameterType="int" resultType="Teacher">
select tid id, tname name from teacher t where tid=#{id}
</select>
</mapper>
1.2 写java测试类
/**
* 属性和表的列名不相同时的2种处理方法
* 1.sql中给列重新命名:
* select tid id, tname name from teacher t where tid=#{id}
* @author 魅力_小生
*
*/
@Test
public void getTeacher(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
String statement = "com.mlxs.mybatis.test3.teacherMapper.getTeacher";
Teacher teacher = session.selectOne(statement, "1");
System.out.println("teacher--->"+teacher);
session.close();
}
1.3 结果
teacher--->Teacher [id=1, name=老师A]
二、第二种处理方法:使用resultMap标签
2.1 写实体类关联的MapperXML:
<mapper namespace="com.mlxs.mybatis.test3.teacherMapper"> <!--
属性和表的列名不相同时的处理方法:
2.在teacherMapper.xml中使用resultMap标签
-->
<select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
select * from teacher where tid=#{id}
</select>
<resultMap type="Teacher" id="getTeacherMap">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
</resultMap>
</mapper>
2.2 Java测试类
/**
* 属性和表的列名不相同时的2种处理方法
* 2.在teacherMapper.xml中使用resultMap标签
* <select id="getTeacher2" parameterType="int" resultMap="getTeacherMap">
* select * from teacher where tid=#{id}
* </select>
* <resultMap type="Teacher" id="getTeacherMap">
* <id property="id" column="tid"/>
* <result property="name" column="tname"/>
* </resultMap>
* @author 魅力_小生
*
*/
@Test
public void getTeacher2(){
//创建session,设置事务为true
SqlSession session = MyBatisUtil.getSessionFactory().openSession(true);
String statement = "com.mlxs.mybatis.test3.teacherMapper.getTeacher2";
Teacher teacher = session.selectOne(statement, "1");
System.out.println("teacher--->"+teacher);
session.close();
}
2.3 结果
teacher--->Teacher [id=1, name=老师A]