ResultMap&&鉴别器&&别名
ResultMap:字段和属性名的对应关系
修改数据库列名称
alter table student1 rename column stuno to sno;
alter table student1 rename column stuname to sname;
alter table student1 rename column stuage to sage;
alter table student1 rename column graname to gname;
字段名->属性名
src\org\myy\mapper\studentMapper.xml
<!--如果字段名和属性名不一致,需要使用resultMap指定对应关系--> <select id="queryStudentByStunoWithResultMap" parameterType="int" resultMap="studentResultMap"> select sno,sname,sage,gname from student1 where sno=#{sno} </select> <resultMap id="studentResultMap" type="student"> <!--主键--> <id column="sno" property="stuNo"/> <!--普通字段--> <result column="sname" property="stuName"/> <result column="sage" property="stuAge"/> <result column="gname" property="graName"/> </resultMap>
src\org\myy\mapper\StudentMapper.java
//stuNo stuName stuAge Student queryStudentByStunoWithResultMap(int sno);
src\org\myy\test\Test.java
//Connection - SqlSession操作Mybatis //conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader->sqlSession //可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle"); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper=session.getMapper(StudentMapper.class); Student student=studentMapper.queryStudentByStunoWithResultMap(1); System.out.println(student); session.close();
在resultMap中还可以使用鉴别器:对相同sql中不同字段值进行判断,从而进行不同得处理。
src\org\myy\mapper\studentMapper.xml
<select id="queryStudentsWithResultMap" resultMap="studentResultMap"> select sno,sname,nikename,sage,gname from student1 </select> <resultMap id="studentResultMap" type="student"> <!--主键--> <id column="sno" property="stuNo"/> <!--普通字段--> <!--<result column="sname" property="stuName"/>--> <result column="sage" property="stuAge"/> <result column="gname" property="graName"/> <!--鉴别器:对查询结果进行分支处理,如果是a年级,则真名,如果b年级,显示昵称--> <discriminator javaType="String" column="gname"> <case value="a" resultType="student"> <result column="sname" property="stuName"/> </case> <case value="b" resultType="student"> <result column="nikename" property="stuName"/> </case> </discriminator> </resultMap>
src\org\myy\mapper\StudentMapper.java
List<Student> queryStudentsWithResultMap();
src\org\myy\test\Test.java
//Connection - SqlSession操作Mybatis //conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); //reader->sqlSession //可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"devOracle"); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper=session.getMapper(StudentMapper.class); List<Student> students=studentMapper.queryStudentsWithResultMap(); System.out.println(students); session.close();
别名
src\conf.xml
<typeAliases>
<!--单个别名(别名忽略大小写) -->
<!-- <typeAlias type="org.myy.entity.Student" alias="student"/> -->
<!--批量定义别名(别名忽略大小写) 以下会自动将该包中的所有类批量定义别名 :别名就是类名(不带包名,忽略大小写) -->
<package name="org.myy.entity"/>
</typeAliases>
如果在批量设置别名时,出现了冲突。可以使用@Alias("student")区分
src\org\myy\entity\Student.java
@Alias("student1") public class Student {
be simple, be happy.