MyBatis注解开发

1.mybatis 的常用注解说明

**@Insert:**实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result 一起使用,封装多个结果集

@ResultMap:实现引用@Results 定义的封装

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

@SelectProvider: 实现动态 SQL 映射

@CacheNamespace:实现注解二级缓存的使用

2.单表CRUD

user表:id、username、birthday、sex、address

user类

1
2
3
4
5
6
7
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
}

userDao接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public interface UserDao {
/*查询所有用户*/
@Select("select * from user")
List<User> findAll();

@Select("select * from user where id = #{id}")
User findById(Integer id);

@Insert("insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})")
void saveUser(User user);

@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id = #{id}")
void updateUser(User user);

@Delete("delete from user where id = #{id}")
void deleteById(Integer id);

@Select("select * from user where username like #{name}")
List<User>findUserByName(String name);

@Select("select count(*) from user")
int findTotal();
}

3.复杂关系映射的注解说明

  1. @Results 注解:代替的是标签resultMap

​ 该注解中可以使用单个@Result 注解,也可以使用@Result 集合

​ @Results({@Result(),@Result()})或@Results(@Result())

  1. @Resutl 注解:代替了id标签和result标签

    @Result 中 属性介绍:

    ​ id 是否是主键字段

    ​ column:sql查询数据库后指定的的结果列名

    ​ property 实体类的属性名

    ​ one 需要使用的@One 注解(@Result(one=@One)()))

    ​ many 需要使用的@Many 注解(@Result(many=@many)()))

  2. @One 注解(一对一):代替了assocation标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。

    @One 注解属性介绍:

    ​ select 指定用来多表查询的 sqlmapper:全限定类名.方法名

    ​ fetchType :指定加载方式 lazy(延迟加载)、DEFAULT、EAGER(立即加载)

    使用格式:

    ​ @Result(column=” “,property=””,one=@One(select=””))

  3. @Many 注解(多对一):代替了Collection标签,是多表查询的关键,在注解中用来指定查询返回的对象集合。

    注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义;

    使用格式:@Result(property=””,column=””,many=@Many(select=””))

3.1列名和sql查询结果的列名不一致的情况下,使用resultMap进行关联

user表:id、username、birthday、sex、address

user1类

1
2
3
4
5
6
7
public class user1 {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;
}

userDao接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*查询所有用户*/
@Select("select * from user")
//id是本结果集的唯一标识,其他接口方法根据此id引用结果集
//value是Result注解数组
@Results(id = "userMap",value = {
//id标识是否为主键,column标识sql查询后的结果列名,property标识实体类对应的属性名
@Result(id = true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday")
})
List<user1> findAll1();

@Select("select * from user where id = #{id}")
@ResultMap("userMap")//指定结果集,写定义好的Results的id
user1 findById1(Integer id);

3.2一对一关联及延迟加载

需求:加载账户信息时并且加载该账户的用户信息,根据情况可实现延迟加载。(注解方式实现)

添加 User 实体类及 Account 实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
}
public class Account {
private Integer id;
private Integer uid;
private Double money;
/*多对一:一个账户只能属于一个用户,从表方应该包含一个主表方的对象引用*/
private User user;
}

添加账户accountDao的持久层接口并使用注解配置

1
2
3
4
5
6
7
8
9
10
11
public interface AccountDao {
/*查询所有用户*/
@Select("select * from account")
@Results(id = "AccountMap",value = {
@Result(id = true,column = "id",proper ty = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(column = "uid",property = "user",one = @One(select = "cn.hxx.Dao.UserDao.findById",fetchType = FetchType.EAGER))
})
List<Account> findAll();
}

添加用户userDao的的持久层接口并使用注解配置

1
2
3
4
public interface UserDao {
@Select("select * from user where id = #{id}")
User findById(Integer id);
}

3.3使用注解实现一对多复杂关系映射

添加 User1 实体类及 Account 实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
public class user1 {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;
private List<Account> accounts;
}
public class Account1 {
private Integer id;
private Integer uid;
private Double money;
}

添加账户account1Dao的持久层接口并使用注解配置

1
2
3
4
public interface Account1Dao {
@Select("select * from account where uid = #{uid}")
List<Account1> findByUid(Integer id);
}

添加用户user1Dao的的持久层接口并使用注解配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface User1Dao {
/*查询所有用户,并加载出每个用户的所有账户信息*/
@Select("select * from user")
@Results(id = "userAccountsMap",value = {
@Result(id = true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday"),
//查询用户所拥有的账户时,需要根据当前用户的id作为AccountDao.findByUid的参数来查询
@Result(column = "id",property = "accounts",many = @Many(select = "cn.hxx.Dao.Account1Dao.findByUid",fetchType = FetchType.LAZY))
})
List<user1> findAllUserAccounts();
}

4.缓存注解配置

一级缓存默认开启不需要配置,二级缓存需要配置

1.开启全局配置

1
2
3
4
5
<!-- 开启延迟加载的支持 -->
<settings>
<!--全局性地开启或关闭所有映射器配置文件中已配置的任何缓存-->
<setting name="cacheEnabled" value="true"></setting>
</settings>

2.使用注解**@CacheNamespace**

1
2
3
4
@CacheNamespace(blocking = true)
public interface User1Dao {
.....
}