一 resultMap标签之一对一映射
1.准备阶段
User.java文件
public class User {
private Integer user_id;
private String account;
private String password;
private String user_name;
private Integer status;
private Date login_time;
private String ip;
private Integer fk_role_id;
//关联对象
private Role role;
//省略getter和setter方法
}
Role.java 文件
public class Role {
private Integer role_id;
private String role_name;
private String role_key;
private Integer status;
//省略getter和setter方法
}
2.传统方式
- UserMapper.xml映射文件
user_id,account,password,user_name,status,login_time,ip,fk_role_id
- UserDao接口和UserDaoImpl实现类
UserDao.java接口代码
public interface UserDao {
User getUserByPK(int user_id);
}
UserDaoImpl.java实现类
public class UserDaoImpl implements UserDao {
@Override
public User getUserByPK( 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映射文件
role_id,role_name,role_key,status
- RoleDao.java文件和RoleDaoImpl.java实现类
RoleDao.java接口
public interface RoleDao {
Role getRoleByPK(int role_id);
}
RoleDaoImpl.java实现类
public class RoleDaoImpl implements RoleDao {
@Override
public Role getRoleByPK( int role_id ) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSqlSession();
return sqlSession.selectOne(Role.class.getName()+".get", role_id);
} finally {
MyBatisUtils.closeSqlSession(sqlSession);
}
}
}
- Java代码测试
@Test
public void 获取用户信息(){
UserDao userDao = new UserDaoImpl();//接口回调
User user = userDao.getUserByPK(-999);
//获取对应外键信息
if(user.getFk_role_id()!=null){
RoleDao roleDao = new RoleDaoImpl();
//查询角色对应的信息
Role role = roleDao.getRoleByPK(user.getFk_role_id());
//建立关系
user.setRole(role);
}
System.out.println(user);
}
-
图解说明
3.resultMap标签之select方式
其实就是替换了
//获取对应外键信息
if(user.getFk_role_id()!=null){
RoleDao roleDao = new RoleDaoImpl();
//查询角色对应的信息
Role role = roleDao.getRoleByPK(user.getFk_role_id());
//建立关系
user.setRole(role);
}
这段代码
- 映射文件
user_id,account,password,user_name,status,login_time,ip,fk_role_id
- Java测试代码
@Test
public void SELECT方式(){
UserDao userDao = new UserDaoImpl();//接口回调
User user = userDao.getUserByPK(-999);
System.out.println(user);
}
-
图解说明
4.resultMap标签之resultMap属性方式
- UserDao接口和UserDaoImpl实现类
UserDao.java代码
public interface UserDao {
User getUserLeftJoin(int user_id);
}
UserDaoImpl.java代码
public class UserDaoImpl implements UserDao {
@Override
public User getUserLeftJoin( int user_id ) {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSqlSession();
return sqlSession.selectOne(User.class.getName()+".getUserLeftJoin", user_id);
} finally {
MyBatisUtils.closeSqlSession(sqlSession);
}
}
}
- 映射文件
${alias}.user_id,${alias}.account,${alias}.password,
${alias}.user_name,${alias}.status,${alias}.login_time,
${alias}.ip,${alias}.fk_role_id
- Java测试代码
@Test
public void 连接查询_结果集处理(){
UserDao userDao = new UserDaoImpl();//接口回调
User user = userDao.getUserLeftJoin(-999);
System.out.println(user);
}
-
图解说明
二 多对一映射测试
- UserDao接口和UserDaoImpl实现类
UserDao.java代码
public interface UserDao {
List list01();
List list02();
}
UserDaoImpl.java代码
public class UserDaoImpl implements UserDao {
@Override
public List list01() {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSqlSession();
return sqlSession.selectList(User.class.getName()+".list01");
} finally {
MyBatisUtils.closeSqlSession(sqlSession);
}
}
@Override
public List list02() {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtils.getSqlSession();
return sqlSession.selectList(User.class.getName()+".list02");
} finally {
MyBatisUtils.closeSqlSession(sqlSession);
}
}
}
- 映射文件
-
图解说明