<?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"> <!-- namespace的名字需要跟接口的类名一致 --> <mapper namespace="cn.bdqn.dao.UserMapper"> <!-- 1、resultMap属性:type为java实体类;id为此resultMap的标识 2、resultMap的子元素: id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能. result – 映射到JavaBean 的某个“简单类型”属性,String,int等. association – 映射到JavaBean 的某个“复杂类型”属性,其他JavaBean类. collection –复杂类型集合 (演示示例2) --> <!--根据roleId获取用户列表: 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 --> <!-- <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"/> <result property="roleName" column="roleName"/> </resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult"> select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id} </select> --> <!-- 根据roleId获取用户列表 association start--> <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" /> <!-- <association property="role" javaType="Role" > <result property="id" column="id"/> <result property="roleCode" column="roleCode"/> <result property="roleName" column="roleName"/> </association> --> <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> <!-- association end--> <!-- 获取指定用户的地址列表(user表-address表:1对多关系) collection start--> <resultMap type="User" id="userMap"> <id property="id" column="userId"/> <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> <!-- collection end --> <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 List<User> getUserList(); //根据roleId获取用户列表 public List<User> 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 List<Role> getRoleList(); }
<?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<Address> addressList; //association private Role role; public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public List<Address> getAddressList() { return addressList; } public void setAddressList(List<Address> 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; } }
时间: 2024-09-30 18:58:43