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
id="studentResultMap" type="student">
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
src\org\myy\mapper\StudentMapper.java
ListqueryStudentsWithResultMap();
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); Liststudents=studentMapper.queryStudentsWithResultMap(); System.out.println(students); session.close();
别名
src\conf.xml
<package name="org.myy.entity"/>
如果在批量设置别名时,出现了冲突。可以使用@Alias("student")区分
src\org\myy\entity\Student.java
@Alias("student1") public class Student {