Mybatis注解开发

1、Mybatis的常用注解

这几年来注解开发越来越流行,在 Spring 中我们也使用过注解。而在中 Mybatis 也可以使用注解开发方式,在只是注解用来替代mapper.xml映射文件,这样我们就可以减少编写 Mapper 映射文件了。一些常用注解:

注解说明
@Insert实现新增
@Delete实现删除
@Update实现更新
@Select实现查询
@Result实现结果集封装
@Results可以与@Result 一起使用,封装多个结果集
@ResultMap实现引用@Results 定义的封装
@One实现一对一结果集封装
@Many实现一对多结果集封装
@SelectProvider实现动态 SQL 映射
@CacheNamespace实现注解二级缓存的使用

2、Mybatis 注解实现基本CRUD

在之前的开发中,我们都是在映射文件中编写映射语句,然后通过映射文件和 Dao 层接口的几个规范完成 Dao 层的实现。现在我们可以通过注替代映射文件,那么现在我们可以直接在 Dao 层接口上使用注解编写映射语句。 具体的实现:

public interface UserDao {

    @Select("select * from user where id=#{id}")
    User findByUserId(int id) throws IOException;

    @Select("select  * from user  where id=#{id} and name = #{name}")
    User findByUserIdAndName(User user) throws IOException;

    @Update("update user set name=#{name},age=#{age} where id=#{id}")
    void updateUser(User user);

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

    @Insert("insert into user(name,age)values(#{name},#{age})")
    void saveUser(User user);

这样一个使用注解实现基本的 CRUD 就完成了,而且在使用过程中除了配置文件中引入映射文件的部分发生改变,其他部分一且照旧。

3、MyBatis 注解实现复杂映射

实现复杂关系映射之前我们可以在映射文件中通过配置<resultMap>元素来实现,在使用注解开发时我们需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解

注解说明
@Results用于替代映射文件中的<resultMap>元素,该注解是配合@Result 注解一起使用的
@Resutl用于替代映射文件中的<id>元素或<result>元素
@One用于替代映射文件中的<assocation>元素,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@Many用于替代映射文件的<collection>元素,是是多表查询的关键,在注解中用来指定子查询返回对象集合。

在这四个注解中,@Resutl用于@Resutls中,@One@Many又作用于@Resutl中。

@Resutl中的属性:

属性说明
id是否是主键字段,
column数据库的列名
property需要装配的属性名
one需要使用的@One 注解(@Result(one=@One)()))
many需要使用的@Many 注解(@Result(many=@many)()))

一对一查询:

public interface UserMapper {
    @Select("select * from orders")
    @Results({
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "ordertime",column = "ordertime"),
            @Result(property = "total",column = "total"),
            @Result(property = "user",column = "uid",
                    javaType = User.class,
                    one = @One(select = "com.itlong.mapper.UserMapper.findById"))
    })
    List<Order> findOrderAll();

    @Select("select * from user where id=#{id}")
    User findByUserId(int id) throws IOException;
}

上述代码的含义:当调用接口的 findOrderAll方法时,执行方法上面的映射语句,然后将查询到结果通过结果集返回,然后结果集调用findByUserId方法,findByUserId方法将查新到的用户信息返回给结果集,然后结果集将结果映射给 Order 对象。

一对多查询:

public interface UserMapper {
    @Select("select * from orders wwhere uid=#{uid}")
    List<Order> findByuId(int uid);

    @Select("select * from user where id=#{id}")
    @Results({
              @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "password",column = "password"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "orderList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.itlong.mapper.UserMapper .findByuid"))
    })
    User findUserAll(int id) throws IOException;
}

上述代码的含义:当调用接口的 findUserAll方法时,执行方法上面的映射语句,然后将查询到结果通过结果集返回,然后结果集调用findByuId方法,findByuId方法将查新到的订单信息返回给结果集,然后结果集将结果映射给 User 对象。

因为我们取消了映射文件,所以我们需要修改配置文件中关于引用映射文件的代码:

<mappers>
    <package name="com.itlong.dao"/>
</mappers>

上述两个案例中的涉及的 User 类和 Order 类:

public class Order {

    private int id;
    private Date ordertime;
    private double total;

    //代表当前订单从属于哪一个客户
    private User user;
}

public class User {
    
    private int id;
    private String username;
    private String password;
    private Date birthday;
    //代表当前用户具备哪些订单
    private List<Order> orderList;
}

4、MyBatis 注解实现缓存

在映射文件中,我们是通过<cache>元素开启该映射文件的二级缓存。而在使用注解的过程中,我们省略了映射文件,所以只能通过CacheNamespace注解开启二级缓存:

@CacheNamespace(blocking = true)
public interface UserMapper {
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值