@Insert @Update @Delete @Select @Result @Results @One @Many
public interface UserMapper { @Insert("insert into user values(#{id},#{username},#{password},#{birthday})") public void save(User user); @Update(" update user set username=#{username}, password=#{password} where id=#{id}") public void update(User user); @Delete("delete from user where id=#{id}") public void delete(int id); @Select("select * from user where id=#{id}") public User findById(int id); @Select("select * from user") public List<User> findAll(); }
加载映射关系, 指定接口所在包
<mappers> <package name="com.bearpx.spring.mybatis.dao"></package> </mappers>
注解 | 说明 |
@Results |
代替<resultMap>标签,可以使用单个@Result注解,也可以使用@Result集合。 使用格式: @Results({@Result(), @Result()}) 或 @Results(@Result()) |
@Result |
代替<id>和<result>标签。 @Result属性介绍: column: 数据库的列名 property: 需要装配的属性名 one: 需要使用的@One注解(@Result(one=@One)()) many: 需要使用的@Many注解(@Result (many=@many)()) |
@One(一对一) |
代替<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。 @One注解属性介绍: select: 指定用来多表查询的sqlmapper 使用格式: @Result(column="", property="", one=@One(select="")) |
@Many(多对一) |
代替<collection>标签,是多表查询的关键,在注解中用来指定子查询返回对象集合。 使用格式: @Result(column="", property="", many=@Many(select="")) |
@Select("select *,o.id oid from orders o, user u where o.uid = u.id") @Results({ @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result(column = "uid",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) public List<Order> findAll();
@Select("select * from orders") @Results({ @Result(column = "id",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result( property = "user", // 要封装的属性名称 column = "uid", // 根据哪个字段去查询user表的数据 javaType = User.class, // 要封装的实体类型 one = @One(select = "com.bearpx.spring.mybatis.dao.UserMapper.findById") // 查询哪个接口的方法获取数据 ) }) public List<Order> findAll2();
@Select("select * from user") @Results({ @Result(id=true, column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password") , @Result( property = "orderList", column = "id", javaType = List.class, many = @Many(select="com.bearpx.spring.mybatis.dao.OrderMapper.findByUid") ) }) public List<User> findUserAndOrderAll();
OrderMapper接口
@Select("select * from orders where uid=#{uid}") public List<Order> findByUid(int uid);
@Select("select * from user") @Results({ @Result(id=true, column = "id",property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password") , @Result( property = "roleList", column = "id", javaType = List.class, many = @Many(select="com.bearpx.spring.mybatis.dao.RoleMapper.findByUid") ) }) public List<User> findUserAndRoleAll();
public interface RoleMapper { @Select("select * from sys_user_role ur,sys_role r where ur.roleId = r.id and ur.userId=#{uid}") public List<Role> findByUid(int uid); }
原文:https://www.cnblogs.com/kingdomer/p/14032749.html