MyBatis的注解开发(一)
Mybatis 的注解说明
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态 SQL 映射
public interface IUserDao {
// 根据姓名模糊查询
@Select("select * from user where username like #{username}")
List<User> findUserByUserName(String name);
// 根据id查用户
@Select("select * from user where id = #{id}")
User findUserById(Integer id);
// 查询所有User
@Select("select * from user")
List<User> findUser();
// 添加用户
@Insert("insert into user values(null,#{username},#{birthday},#{sex},#{address})")
int insert(User user);
// 修改
@Update("update user set username = #{username} ,birthday = #{birthday} , sex = #{sex} ,address = #{address} where id = #{id}")
int update(User user);
// 删除
@Delete("delete from user where id = #{id}")
int delete(int i);
}
MyBatis的注解开发(二)
使用注解实现复杂关系映射开发
复杂关系映射的注解说明
@Results 注解
代替的是标签<resultMap>
该注解中可以使用单个@Result 注解,也可以使用@Result 集合
@Results({@Result(), @Result() })或@Results(@Result())
@Resutl 注解
代替了 <id>标签和<result>标签
@Result 中 属性介绍:
column 数据库的列名
Property 需要装配的属性名
one 需要使用的@One 注解(@Result(one=@One)()))
many 需要使用的@Many 注解(@Result(many=@many)()))
@One 注解(一对一)
代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:
select 指定用来多表查询的 sqlmapper
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。 。
使用格式:
@Result(column=" ",property="",one=@One(select=""))
@Many 注解(多对一)
代替了<Collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般
为 ArrayList) 但是注解中可以不定义;
使用格式:
@Result(property="",column="",many=@Many(select=""))
使用注解实现一对一复杂关系映射及延迟加载
// Account实体类
public class Account {
private Integer id;
private Integer uid;
private Double money;
private User user;
}
/*
和XML方式对比:
*/
// IAccountDao接口
public interface IAccountDao {
// <select id="findAccount" resultMap="accountMap">
// SELECT * from account
// </select>
// 只查询账户信息的DAO接口 使用注解实现 并按需实现懒加载
@Select("select * from account")
// 查询账户信息 还要加载该账户所属的用户信息
@Results({ // <resultMap id="accountMap" type="account"> Results相当于resultMap
@Result(id = true, property = "id", column = "id"), // <id column="id" property="id"/>
@Result(property = "uid", column = "uid"), // <result column="uid" property="uid"/>
@Result(property = "money", column = "money"), // <result column="money" property="money"/>
// <association property="user"
// javaType="user"
// select="fun.chenqi.ssm.dao.IUserDao.findUserById"
// column="uid"
// fetchType="lazy"
// />
// property: account实体类里面 private User user;
// column : 填写传递给 select 映射的参数
// select: 这是来自UserDao接口的findUserById()方法的调用,用于查询指定账户下的User信息
// fetchType = FetchType.LAZY 实现懒加载
// @One:实现一对一结果集封装 相当于association
@Result(property = "user", column = "uid", javaType = User.class,
one = @One(
select = "fun.chenqi.ssm.dao.IUserDao.findUserById",
fetchType = FetchType.LAZY
)
)
}
)
List<Account> findAccount();
}
// IUserDao接口
public interface IUserDao {
// 根据id查用户
@Select("select * from user where id = #{id}")
User findUserById(Integer id);
}
使用注解实现一对多复杂关系映射及延迟加载
// User实体类
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List<Account> accounts;
// ......
}
// IUserDao接口
public interface IUserDao {
// <select id="findUser" resultMap="userMap">
// SELECT * from user;
// </select>
// 查询所有User
@Select("select * from user")
@Results({ // <resultMap id="userMap" type="user">
@Result(id = true, column = "id", property = "id"), // <id column="id" property="id"/>
@Result(property = "username", column = "username"), // <result column="username" property="username"/>
@Result(column = "birthday", property = "birthday"), // <result column="birthday" property="birthday"/>
@Result(column = "sex", property = "sex"), // <result column="sex" property="sex"/>
@Result(column = "address", property = "address"), // <result column="address" property="address"/>
// <collection
// property="accounts" accounts是User类List集合的属性名
// ofType="account" 在注解里面写JavaType 代表accounts集合的类型
// fetchType="lazy" 懒加载
// select="fun.chenqi.ssm.dao.IAccountDao.findAccountByUid" many相当于collection ,select代表IAccountDao接口的哪个方法
// column="id"> 表示要传递的参数
// </collection>
@Result(column = "id", javaType = List.class, property = "accounts",
many = @Many(
select = "fun.chenqi.ssm.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY
)
)
})
List<User> findUser();
}
// IAccountDao接口
public interface IAccountDao {
// 根据uid查用户信息
@Select("select * from account where uid = #{id}")
List<Account> findAccountByUid(Integer uid);
}