MyBatis注解开发
MyBatis常用注解
将映射文件替换为注解:
public interface UserMapper {
@Insert("insert into user value (...)")
public void save(User user);
@Update("")
public List<User> findAll();
}
其他delete,select等注解类似
之后将之前核心配置文件中的加载映射文件代码换成:
<mappers>
<!-- 指定接口所在包-->
<package name="com.xxx.Mapper"/>
</mappers>
复杂查询:
一对一:
方法一
@Insert("insert into user value (...)")
@Results({
@Result(column = "oid",property = "id")
@Result(column = "oid",property = "id")
@Result(column = "oid",property = "id")
})
public void save(User user);
方法二
@Select("select * from orders ")
@Results({
@Result(column = "oid",property = "id"),
@Result(column = "oid",property = "id"),
@Result(column = "oid",property = "id"),
@Result(
property = "user",//要封装的属性名称
column = "uid",//根据那个字段来查询另外一个表数据
javaType = User.class,//要封装的实体类型
//select属性代表那个接口方法获得数据,findById是我们写好的一个带select注解的接口方法用来查询id相同的
one=@One(select = "com.xxx.mapper.UserMapper.findById")
)
})
public void save(User user);
一对多:
@Select("select * from user ")
@Results({
@Result(id = true, column = "id", property = "id"),
@Result(column = "oid", property = "id"),
@Result(column = "oid", property = "id"),
@Result(property = "orderList",
column = "id",
javaType = List.class,
many = @Many(select = "com.xxx.mapper.findById")
)
})
public void save(User user);
多对多:
@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.xxx.mapper.findByUid")
)
})
public void save(User user);
@Select("select * from sys_user_role ur,sys_role r where ur.roleId=r.id And ur.userId=#{uid")
public void findByUid(User user);
标签:--,column,Result,MyBatis,注解,property,id,select,user
From: https://www.cnblogs.com/rainaftersummert/p/17626671.html