Annotation development has become more and more popular in recent years, and Mybatis can also use annotation development, so that we can reduce the writing of Mapper mapping files.
Mybatis common notes
@Insert: Implement new
@Update: Implement update
@Delete: Implement delete
@Select: Implement query
@Result: Implement result set encapsulation
@Results: Can be used with @Result to encapsulate multiple result sets
@ResultMap: Implement the encapsulation defined by reference @Results
@One: Implement one-to-one result set encapsulation
@Many: Implement one-to-many result set encapsulation
@SelectProvider: Implement dynamic SQL mapping
@CacheNamespace: Implement the use of annotated secondary cache
Note: The attribute name of User here is not consistent with the column name of the database table.
Use annotations to develop the persistence layer interface
publicinterfaceIUserDao{/**
* 查询所有用户
* @return
*/@Select("select * from user")@Results(id="userMap",
value={@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User>findAll();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/@Select("select * from user where id = #{uid} ")@ResultMap("userMap")
User findById(Integer userId);/**
* 保存操作
* @param user
* @return
*/@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address})")intsaveUser(User user);/**
* 更新操作
* @param user
* @return
*/@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")intupdateUser(User user);/**
* 删除用户
* @param userId
* @return
*/@Delete("delete from user where id = #{uid} ")intdeleteUser(Integer userId);/**
* 查询使用聚合函数
* @return
*/@Select("select count(*) from user ")intfindTotal();/**
* 模糊查询
* @param name
* @return
*/@Select("select * from user where username like #{username} ")
List<User>findByName(String name);}
Write SqlMapConfig configuration file
<!-- 配置映射信息 --><mappers><!-- 配置 dao 接口的位置,它有两种方式
第一种:使用 mapper 标签配置 class 属性
第二种:使用 package 标签,直接指定 dao 接口所在的包
--><packagename="cn.myp666.dao"/></mappers>
Using annotations to implement complex relationship mapping development
Before implementing complex relationship mapping, we can <resultMap>achieve it through configuration in the mapping file. When developing with annotations, we need to use @Results annotation, @Result annotation, @One annotation, @Many annotation.
Annotation explanation of complex relationship mapping
@Results 注解
Instead of labels<resultMap>
You can use a single @Result annotation in this annotation, or you can use the @Result collection
Instead of <Collection>labels, it is the key to multi-table queries and is used in annotations to specify that the subquery returns a collection of objects.
Note: Aggregate elements are used to handle the "one-to-many" relationship. You need to specify the attribute of the mapped Java entity class, the javaType of the attribute (generally ArrayList), but it can not be defined in the annotation;
Add user's persistence layer interface and use annotation configuration
publicinterfaceIUserDao{/**
* 查询所有用户
* @return
*/@Select("select * from user")@Results(id="userMap",
value={@Result(id=true,column="id",property="userId"),@Result(column="username",property="userName"),@Result(column="sex",property="userSex"),@Result(column="address",property="userAddress"),@Result(column="birthday",property="userBirthday")})
List<User>findAll();/**
* 根据 id 查询一个用户
* @param userId
* @return
*/@Select("select * from user where id = #{uid} ")@ResultMap("userMap")
User findById(Integer userId);}
One-to-many complex relationship mapping using annotations
Write user persistence layer interface and use annotation configuration