8., ordinary road – advanced mapping, one to one and many to one

One of the resultMap tags is one-to-one mapping

1. preparation stage

User.java file

Public class User Integer private String {private user_id; account private; String password; private String user_name; private Integer status; private Date login_time; private String IP; private Integer fk_role_id; / / role / / Role private related object; omit the getter and setter methods}

Role.java file

Public class Role Integer private String {private role_id; role_name private; String role_key; private Integer status; / / getter and setter} ellipsis

2. traditional way

  • UserMapper.xml mapping file
< mapper namespace= > < com.shxt.model.User; resultMap type= "com.shxt.model.User" id= "BaseResultMapper" > < ID; column= "user_id" property= "user_id" /> < result; column= "account" property= "account" /> < result; column= "password" property= "password" /> < result column= "user_name" property= "user_name" /> < result; column= "status" property= "status" /> < result; column= "login_time" property= "login_time" /> < result; column= "IP" property= "IP" /> < result; column= "fk_role_id" property= "fk_role_id" /> < /resultMap> < SQL id= "sys_user_columns" > user_id, account, password, user_name, status, login_time, IP, fk_role_id, < /sql& Gt; < select id= "load" parameterType= "_int" resultMap= "BaseResultMapper" > SELECT < include refid= "sys_user_columns" /> FROM sys_user WHERE user_id=#{user_id}; < /select> < /mapper>
  • The UserDao interface and the UserDaoImpl implementation class

UserDao.java interface code

Public, interface, UserDao, {User, getUserByPK (int, user_id)}

UserDaoImpl.java implementation class

Public class UserDaoImpl implements UserDao public User getUserByPK {@Override (int user_id) {SqlSession sqlSession = null; try = {sqlSession (MyBatisUtils.getSqlSession); return sqlSession.selectOne ((User.class.getName) + ".Load", user_id);} finally {MyBatisUtils.closeSqlSession}}} (sqlSession);
  • RoleMapper.xml mapping file
< mapper namespace= > < com.shxt.model.Role; resultMap type= "com.shxt.model.Role" id= "BaseResultMapper" > < ID; column= "role_id" property= "role_id" /> < result; column= "role_name" property= "role_name" < result / > column= "role_key" property= "role_key" /> < result column= "status" property= "status" /> < /resultMap> < SQL id= "sys_role_columns" > role_id, role_name, role_key, status, < /sql> < select id= "get" parameterType= "_int" resultMap= "BaseResultMapper" > SELECT < include refid= "sys_role_columns" /> FROM sys_role WHERE role_id=#{role_id} < /select> < /mapper>
  • The RoleDao.java file and the RoleDaoImpl.java implementation class

RoleDao.java interface

Public, interface, RoleDao, {Role, getRoleByPK (int, role_id)}

RoleDaoImpl.java implementation class

Public class RoleDaoImpl implements RoleDao public Role getRoleByPK {@Override (int role_id) {SqlSession sqlSession = null; try = {sqlSession (MyBatisUtils.getSqlSession); return sqlSession.selectOne ((Role.class.getName) + ".Get", role_id);} finally {MyBatisUtils.closeSqlSession}}} (sqlSession);
  • Java code testing
@Test public void (UserDao) to obtain user information {userDao = new (UserDaoImpl); / / user = userDao.getUserByPK User callback interface (-999); / / get the corresponding foreign key information if (user.getFk_role_id) =null (!) {RoleDao roleDao = new (RoleDaoImpl); / / query role information corresponding to the Role = roleDao.getRoleByPK (user.getFk_role_id (role)); / / establish the relationship between user.setRole (role); System.out.println (user);}}
  • Illustration illustrates the one to one traditional way of
    8., ordinary road - advanced mapping, one to one and many to one

Select mode of 3.resultMap tag

It’s actually a replacement

To obtain the corresponding foreign key information (if / user.getFk_role_id) (! =null) {RoleDao roleDao = new (RoleDaoImpl); / / query role information corresponding to the Role role = roleDao.getRoleByPK (user.getFk_role_id ()); / / establish the relationship between user.setRole (role);}

This piece of code

  • Mapping file
< resultMap type= "com.shxt.model.User" id= "BaseResultMapper" > < ID; column= "user_id" property= "user_id" /> < result; column= "account" property= "account" /> < result; column= "password" property= "password" /> < result; column= "user_name" property= "user_name" /> < result; column= "status" property= "status" /> < result; column= "login_time" property= "login_time" /> < result; column= "IP" property= "IP" /> < result; column= "fk_role_id" property= "fk_role_id" /> < /resultMap> < resultMap type= "com.shxt.model.User" id= "SimpleResultMapper" extends= "BaseResultMapper" > < association! - used to map related object --> < Assoc Iation property= "role" javaType= "com.shxt.model.Role" column= "fk_role_id" select= "com.shxt.model.Role.get" /> < /resultMap> < SQL id= "sys_user_columns" > user_id, account, password, user_name, status, login_time, IP, fk_role_id, < /sql> <! -- the use of another ID resultMap= SimpleResultMapper --> < select "id=" load "parameterType=" _int "resultMap=" SimpleResultMapper "> SELECT < include refid=" sys_user_columns "/> FROM sys_user WHERE user_id=#{user_id}; < /select>
  • Java test code
@Test public void SELECT (userDao) {UserDao = new (UserDaoImpl); / / user = userDao.getUserByPK User callback interface (-999); System.out.println (user);}
  • The diagram illustrates that the
    8., ordinary road - advanced mapping, one to one and many to one
    select method illustrates the inheritance of the
    8., ordinary road - advanced mapping, one to one and many to one
    result set

ResultMap attribute mode of 4.resultMap tag

  • UserDao interface and UserDaoImpl implementation class
    UserDao.java code
Public, interface, UserDao, {User, getUserLeftJoin (int, user_id)}

UserDaoImpl.java code

Public class UserDaoImpl implements UserDao public User getUserLeftJoin {@Override (int user_id) {SqlSession sqlSession = null; try = {sqlSession (MyBatisUtils.getSqlSession); return sqlSession.selectOne ((User.class.getName) + ".GetUserLeftJoin", user_id);} finally {MyBatisUtils.closeSqlSession}}} (sqlSession);
  • Mapping file
< mapper namespace= > < com.shxt.model.User; SQL id= "sys_user_columns_alias" > ${alias}.user_id, ${alias}.account, ${alias}.password, ${alias}.user_name, ${alias}.status, ${alias}.login_time, ${alias}.ip, ${alias}.Fk_role_id < /sql> < resultMap type= "com.shxt.model.User" id= "BaseResultMapper" > < ID; column= "user_id" property= "user_id" /> < result column= "account" property= "account" /> < result; column= "password" property= "password" /> < result; column= "user_name" property= "user_name" /> < result; column= "status" property= "status" /> < result; column= "login_time" property= "login_time" /> < result; column= ip" Property= "IP" /> < result; column= "fk_role_id" property= "fk_role_id" /> < /resultMap> < resultMap type= "com.shxt.model.User" id= "JoinResultMapper" extends= "BaseResultMapper" > < Association; property= "role" javaType= "com.shxt.model.Role" resultMap= "com.shxt.model.Role.BaseResultMapper" > < < ID! Column= "role_id" property= "role_id" /> < result; column= "role_name" property= "role_name" /> < result; column= "role_key" property= "role_key" /> < result; column= "status" property= "status" /> --> < /association> < /resultMap> < select id= getUserLeftJoin parameterType=" _int "resultMap=" JoinResultMapper "> SELECT < include refid=" sys_user_columns_alias "> < property; name=" alias "value=" U "/> < /include> <,! -- how to find those key fields of --> < include; refid= com.shxt.model.Role.sys_role_columns_alias > < property; name=" alias "value=" R "/> < /include> FROM sys_user u LEFT JOIN sys_role R ON u.fk_role_id WHERE u.user_id =#{user_id} = r.role_id < /select>
  • Java test code
@Test public void _ query result set processing (UserDao) {userDao = new (UserDaoImpl); / / user = userDao.getUserLeftJoin User callback interface (-999); System.out.println (user);}
  • Illustration explains
    8., ordinary road - advanced mapping, one to one and many to one
    recommendation

More than 2 to one mapping test

  • UserDao interface and UserDaoImpl implementation class
    UserDao.java code
Public, interface, UserDao, {List< User> list01 (); List< User> list02 ();}

UserDaoImpl.java code

Public class UserDaoImpl implements UserDao @Override public {List< User> list01 (SqlSession) {sqlSession = null; try = {sqlSession (MyBatisUtils.getSqlSession); return sqlSession.selectList (User.class.getName) + (".List01");} finally {MyBatisUtils.closeSqlSession}} (sqlSession); @Override public List< User> list02 (sqlSession = SqlSession) {null; try {sqlSession = MyBatisUtils.getSqlSession (return); sqlSession.selectList (User.class.getName) + (".List02");} finally {MyBatisUtils.closeSqlSession}}} (sqlSession);
  • Mapping file
< select id= "list01" resultMap= "SimpleResultMapper" > SELECT < include refid= "sys_user_columns" /> FROM sys_user < /select> < select id= "list02" resultMap= "JoinResultMapper" > SELECT < include refid = "sys_user_columns_alias" > < property; name= "alias" value= "U" /> < /include> < include, refid= "com.shxt.model.Role.sys_role_columns_alias" > < property; name= "alias" value= "R" /> < FROM / include> sys_user u LEFT JOIN sys_role R ON u.fk_role_id = r.role_id < /select>
  • Illustration illustrates
    8., ordinary road - advanced mapping, one to one and many to one
    multiple to one test