码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis的注解开发

时间:2021-05-24 10:31:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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 }
User.java
技术图片
 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 }
TUser.java
技术图片
 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 }
Account.java

  提示: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();

 

mybatis的注解开发

标签:err   HERE   从表   alt   account   serial   nbsp   sed   new   

原文地址:https://www.cnblogs.com/gjx1212/p/14771575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!