标签:err HERE 从表 alt account serial nbsp sed new
准备的model:
1 public class User implements Serializable{ 2 private Integer id; 3 private String name; 4 5 //保存一对多查询结果 6 private List<Account> accounts; 7 public List<Account> getAccounts() { 8 return accounts; 9 } 10 public void setAccounts(List<Account> accounts) { 11 this.accounts = accounts; 12 } 13 public Integer getId() { 14 return id; 15 } 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 @Override 26 public String toString() { 27 return "User [id=" + id + ", name=" + name + "]"; 28 } 29 30 31 32 33 }
1 public class TUser implements Serializable{ 2 @Override 3 public String toString() { 4 return "TUser [userId=" + userId + ", userName=" + userName + "]"; 5 } 6 public int getUserId() { 7 return userId; 8 } 9 public void setUserId(int userId) { 10 this.userId = userId; 11 } 12 public String getUserName() { 13 return userName; 14 } 15 public void setUserName(String userName) { 16 this.userName = userName; 17 } 18 int userId; 19 String userName; 20 }
1 public class Account implements Serializable{ 2 private Integer id; 3 private Integer uid; 4 private double money; 5 6 //从表包含一个主表实体 7 private User user; 8 9 public Integer getId() { 10 return id; 11 } 12 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 17 public Integer getUid() { 18 return uid; 19 } 20 21 @Override 22 public String toString() { 23 return "Account [id=" + id + ", uid=" + uid + ", money=" + money + ", user=" + user+ "]"; 24 } 25 26 public void setUid(Integer uid) { 27 this.uid = uid; 28 } 29 30 public double getMoney() { 31 return money; 32 } 33 34 public void setMoney(double money) { 35 this.money = money; 36 } 37 38 public User getUser() { 39 return user; 40 } 41 42 public void setUser(User user) { 43 this.user = user; 44 } 45 }
提示:TUser.java是user.java的别名版。
使用步骤核心:
创建对应dao的interface,并在对应的方法上添加sql语句对应的注解。
在方法上添加注解,可以根据方法的返回值参数等自动识别,实现想要的效果。
1,单表操作(写在对应的interface中的)
@Select,@Insert,@Update,@Delete注解对应CRUD操作
1 //针对crud一共有4个注解 2 @Select("select * from user") 3 public List<User> findAll(); 4 @Insert("insert into user(name) values(#{name})") 5 void saveUser(User user);//#{}里面的变量是对应的形参的属性名 6 7 @Update("update user set name=#{name} where id=#{id} ") 8 void updateUser(User user); 9 10 @Delete("delete from user where id=#{id}") 11 void deleteUser(Integer id); 12 13 @Select("select * from user where id=#{id}") 14 User findById(Integer id); 15
别名的使用
//数据库和对应的表不能对应上 @Select("select * from user") @Results(id="userMap",value = { @Result(id=true,column = "id",property = "userId"), @Result(column = "name",property = "userName"), }) List<TUser> findTUser(); @Select("select * from user where id=#{id}") @ResultMap(value = {"userMap"}) TUser findByIdTUser(Integer id);
2,多表操作
(1)多对一,一对一
fetchType属性:立即加载|懒加载
//多对一查询 @Select("select a.id,a.money ,a.user_id uid from my_account a ") @Results(id="userAccount",value ={ @Result(id=true,column = "id",property = "id"), @Result(column = "uid",property = "uid"), @Result(column = "money",property = "money"), @Result(column = "uid",property = "user",one=@One(select = "com.dao.IUserDao.findById",fetchType = FetchType.EAGER)), }) List<Account> findAccount(); //根据uid在account中查询账户 @Select("select a.id ,a.money,a.user_id uid from my_account a where user_id=#{id}") List<Account> findAccountsByUid(Integer uid);
(2)一对多
//一对多查询 @Select("select * from user") @Results(id="UserAccountMap",value = { @Result(id=true,column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "id",property = "accounts",many = @Many(select = "com.dao.IUserDao.findAccountsByUid", fetchType = FetchType.LAZY )) } ) List<User> findOneToMany();
标签:err HERE 从表 alt account serial nbsp sed new
原文地址:https://www.cnblogs.com/gjx1212/p/14771575.html