mybatis多对多配置
数据库配置相关配置
user表:
role表:
中间表:user_role
xml配置文件和mapper、javabean
role.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanlin.mapper.RoleMapper">
<resultMap id="findRoleMap" type="com.hanlin.bean.Role">
<id property="id" column="id"/>
<result property="roleName" column="rolename"/>
<result property="note" column="note"/>
<!--一对多关系配置:一个角色可以对应多个用户-->
<collection property="userList" column="id" ofType="com.hanlin.bean.User"
select="com.hanlin.mapper.UserMapper.getRoleIdUser">
</collection>
</resultMap>
<!--根据roleid获取角色-->
<select id="getRoleById" resultMap="findRoleMap" parameterType="int">
SELECT
ro.id,ro.rolename,ro.note
FROM
role ro
WHERE ro.id=#{id}
</select>
<!--根据用户查询角色-->
<select id="getUserByIdRole" resultMap="findRoleMap" parameterType="int">
SELECT
ro.rolename,ro.note
FROM
user_role r
LEFT JOIN role ro ON r.roleid = ro.id
WHERE
r.userid = #{userid}
</select>
</mapper>
user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanlin.mapper.UserMapper">
<resultMap id="UserMap" type="com.hanlin.bean.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="address" column="address"/>
<!--一对多配置:一个用户可以对用多应角色-->
<collection property="roleList" column="id" ofType="com.hanlin.bean.Role"
select="com.hanlin.mapper.RoleMapper.getUserByIdRole"/>
</resultMap>
<!--根据角色查询用户-->
<select id="getRoleIdUser" parameterType="int" resultMap="UserMap">
SELECT
u.`name`,u.address
FROM
user_role r
LEFT JOIN `user` u ON r.userid = u.id
where r.roleid=#{roleId}
</select>
<!--根据userid获取用户-->
<select id="getUserById" parameterType="int" resultMap="UserMap">
SELECT
*
FROM
user
where id=#{userid}
</select>
</mapper>
UserMapper
package com.hanlin.mapper;
import com.hanlin.bean.User;
import org.apache.ibatis.annotations.Param;
/**
* 用户数据查询接口
* @author:hanlin.yuan
* @date:2019/2/25
*/
public interface UserMapper {
/**
* 根据角色查询用户
* @param roleId
* @return
*/
User getRoleIdUser(@Param("roleId") int roleId);
/**
* 根据userID获取用户
* @param userid
* @return
*/
User getUserById(@Param("userid") int userid);
}
RoleMapper
package com.hanlin.mapper;
import com.hanlin.bean.Role;
import org.apache.ibatis.annotations.Param;
/**
* 角色数据查询接口
* @author:hanlin.yuan
* @date:2019/2/25
*/
public interface RoleMapper {
/**
* 根据id获取角色
* @param id
* @return
*/
Role getRoleById(@Param("id")int id);
/**
* 根据用户id获取用户角色
* @param userid
* @return
*/
Role getUserByIdRole(@Param("userid")int userid);
}
Role
package com.hanlin.bean;
import java.util.List;
/**
* 角色
* @author:hanlin.yuan
* @date:2019/2/25
*/
public class Role {
private int id;
private String roleName;
private String note;
private List<User> userList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
User
package com.hanlin.bean;
import java.util.List;
/**
* 用户
* @author:hanlin.yuan
* @date:2019/2/25
*/
public class User {
private int id;
private String name;
private String address;
private List<Role> roleList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Role> getRoleList() {
return roleList;
}
public void setRoleList(List<Role> roleList) {
this.roleList = roleList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试类:
package com.hanlin.test;
import com.hanlin.bean.Role;
import com.hanlin.bean.User;
import com.hanlin.mapper.RoleMapper;
import com.hanlin.mapper.UserMapper;
import com.hanlin.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
/**
* 用户测试
* @author:hanlin.yuan
* @date:2019/2/25
*/
public class UserTest {
public static void main(String[] args) {
SqlSession session = SqlSessionFactoryUtil.getSqlSession();
RoleMapper mapper = session.getMapper(RoleMapper.class);
Role role1 = mapper.getRoleById(2);
System.out.println("角色名称:"+role1.getRoleName());
List<User> list = role1.getUserList();
for (User user:list){
System.out.println("user名称:"+user.getName());
}
System.out.println("********************************************");
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.getUserById(7);
System.out.println("用户名称:"+user.getName());
List<Role> roles = user.getRoleList();
for (Role r:roles){
System.out.println("角色:"+r.getRoleName());
}
}
}
控制台输出:
注意事项
我们从role.xml文件中可以看出,我们在配置一对多的时候,多的方其实是可以用“select"属性来查询的,我们上面的例子就是调用的user.xml里的getRoleIdUser:根据角色来查询用户,而column="id"其中的id就是指的角色的id,这行代码的意思就是把角色的id“带到”user.xml里的getRoleIdUser方法里当做参数来查询
项目资源
项目源码:链接:https://pan.baidu.com/s/1NW3RBjznsOjZuzd7u4Uw1A
提取码:pz8l