1.常用注解
@Insert 新增
@Update 更新
@Delete 删除
@Select 查询
@Result 结果集
@Results 封装多个结果集
@One 一对一结果集
@Many 一对多结果集
- 注解代替Mapper.xml文件
-
mapper.UserMapper
public interface UserMapper { /** * 基本操作 */ @Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save(User user); @Update("update user set username=#{username},password=#{password} where id=#{id}") public void update(User user); @Delete("delete from user where id=#{id}") public void delete(int id); @Select("select * from user where id=#{id}") public User findById(int id); @Select("select * from user") public List<User> findAll(); /** * 一对多 */ @Select("select * from user") @Results({ @Result(id=true ,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "orderList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid") ) }) public List<User> findUserAndOrderAll(); @Select("SELECT * FROM USER") @Results({ @Result(id = true,column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "roleList", column = "id", javaType = List.class, many = @Many(select = "com.itheima.mapper.RoleMapper.findByUid") ) }) public List<User> findUserAndRoleAll(); }
-
mapper.OrderMapper
public interface OrderMapper { @Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid); /** * 一对一 方式二.两张表分开查 */ @Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result( property = "user", //要封装的属性名称 column = "uid", //根据那个字段去查询user表的数据 javaType = User.class, //要封装的实体类型 //select属性 代表查询那个接口的方法获得数据 one = @One(select = "com.itheima.mapper.UserMapper.findById") ) }) public List<Order> findAll(); /** * 一对一 方式一.两张表一起查 */ /*@Select("select *,o.id oid from orders o,user u where o.uid=u.id") @Results({ @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result(column = "uid",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) public List<Order> findAll();*/ }
-
mapper.RoleMapper
public interface RoleMapper { @Select("SELECT * FROM sys_user_role ur,sys_role r WHERE ur.roleId=r.id AND ur.userId=#{uid}") public List<Role> findByUid(int uid); }
-
加载映射关系
<mappers> <package name="com.miaokela.mapper"></package> </mappers>
-
附录(模型类):
domain.User
public class User { private int id; private String username; private String password; private Date birthday; //当前用户具备哪些角色 private List<Role> roleList; public List<Role> getRoleList() { return roleList; } public void setRoleList(List<Role> roleList) { this.roleList = roleList; } /*//描述的是当前用户具有的订单 private List<Order> orderList; public List<Order> getOrderList() { return orderList; } public void setOrderList(List<Order> orderList) { this.orderList = orderList; }*/ public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", birthday=" + birthday + ", roleList=" + roleList + '}'; } }
domain.Order
public class Order { private int id; private Date ordertime; private double total; //当前订单属于哪一个用户 private User user; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getOrdertime() { return ordertime; } public void setOrdertime(Date ordertime) { this.ordertime = ordertime; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Order{" + "id=" + id + ", ordertime=" + ordertime + ", total=" + total + ", user=" + user + '}'; } }
domain.Role
public class Role { private int id; private String roleName; private String roleDesc; 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 getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } @Override public String toString() { return "Role{" + "id=" + id + ", roleName='" + roleName + '\'' + ", roleDesc='" + roleDesc + '\'' + '}'; } }
-