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="cn.bdqn.dao.UserMapper"> <resultMap type="User" id="seachUserResult"> <result property="id" column="id"/> <result property="userCode" column="userCode" /> <result property="userName" column="userName" /> <result property="roleId" column="roleId" />
//property=“属性名” javaType=“属性名的类型” ----(一对一)
<association property="role" javaType="Role" resultMap="roleMap"/> resultMap> <resultMap type="Role" id="roleMap"> <result property="id" column="id"/> <result property="roleCode" column="roleCode"/> <result property="roleName" column="roleName"/> resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult"> select u.*,r.roleCode as roleCode,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id} select> <resultMap type="User" id="userMap"> <id property="id" column="userId"/>
//property是属性名 ofType是List《Address》的类对象
<collection property="addressList" ofType="Address"> <id property="id" column="a_id"/> <result property="postCode" column="postCode"/> <result property="addressContent" column="addressContent"/> collection> resultMap> <select id="getAddressListByUserId" parameterType="User" resultMap="userMap"> select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id} select> <select id="count" resultType="int"> select count(1) from user select> <insert id="add" parameterType="User"> insert into user (userCode,userName,userPassword) values (#{userCode},#{userName},#{userPassword}) insert> <update id="update" parameterType="User"> update user set userCode=#{userCode},userName=#{userName}, userPassword=#{userPassword} where id=#{id} update> <delete id="delete" parameterType="User"> delete from user where id=#{id} delete> <select id="getUserList" resultType="User"> select * from user select> mapper>
接口 UserMapper
public interface UserMapper { public int count(); public void add(User user); public void update(User user); public void delete(User user); public ListgetUserList(); //根据roleId获取用户列表 public List getUserListByRoleId(Role role); //获取指定用户的地址列表(user表-address表:1对多关系) public User getAddressListByUserId(User user); }
接口 RoleMapper
public interface RoleMapper { public void add(Role role); public void update(Role role); public void delete(Role role); public ListgetRoleList(); }
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="cn.bdqn.dao.RoleMapper"> <select id="getRoleList" resultType="Role"> select * from role select> <insert id="add" parameterType="Role"> insert into role (roleCode,roleName) values (#{roleCode},#{roleName}) insert> <update id="update" parameterType="Role"> update role set roleCode=#{roleCode},roleName=#{roleName} where id=#{id} update> <delete id="delete" parameterType="Role"> delete from role where id=#{id} delete> mapper>
User.java
public class User { private Integer id; private String userName; private String userCode; private String userPassword; private Integer roleId; //private String roleName; //collection private List addressList; //association private Role role; public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public List getAddressList() { return addressList; } public void setAddressList(List addressList) { this.addressList = addressList; } public Integer getRoleId() { return roleId; } public void setRoleId(Integer roleId) { this.roleId = roleId; } /** public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } */ public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } }
address.java
public class Address { private Integer id; private Integer postCode; private String addressContent; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPostCode() { return postCode; } public void setPostCode(Integer postCode) { this.postCode = postCode; } public String getAddressContent() { return addressContent; } public void setAddressContent(String addressContent) { this.addressContent = addressContent; } }