实体类属性和数据库表中列名不一致的时候,使用@Results()注解。
@Select("select * from user")
@Results(id="userMap",
value = {
@Result(id=true,column = "id",property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address"),
@Result(column = "birthday", property = "birthday")
})
复杂关系映射的注解说明
@Results注解代替了标签<resultMap>
该注解中可以使用单个@Result注解,也可以使用@Result集合
@Results({@Result(),@Result()})或@Results(@Result())
id: 表示唯一标志,比如userMap。这样下面的查询也可以用到。使用@ResultMap("userMap"),就不需要在每个持久层接口上写一堆映射信息了。
@Result注解 代替了<id>标签和<result>标签
@Result中属性:
id:主键字段
column:数据库的列名
property:实体类属性名
one:需要使用@One注解(@Result(one=@One)())
many:需要使用@Many注解(@Result(many=@Many)())
@One 注解(一对一)
代替了<assocation>标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性:
select:指定用来多表查询的 sqlmapper
fetchType:会覆盖全局的配置参数 lazyLoadingEnabled。。
使用格式:
@Result(column="uid",property="user",one=@One(select="com.mantishell.dao.IUserDao.findById",fetchType=FetchType.EAGER))
@Many 注解(多对一)
代替了<Collection>标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义;
使用格式:
@Result(property="accounts",column="id",many=@Many(select="com.mantishell.dao.IAccountDao.findAccountByUId",fetchType=FetchType.LAZY))
一对一映射
package com.mantishell.dao;
import com.mantishell.domain.Account;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
public interface IAccountDao {
/**
* 查询所有账户,并且获取每个账户所属的用户信息
* @return
*/
@Select("select * from account")
@Results(id="accountMap",value = {
@Result(id=true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(property = "user",column = "uid",one=@One(select="com.mantishell.dao.IUserDao.findById",fetchType= FetchType.EAGER))
})
List<Account> findAll();
/**
* 根据用户id查询账户信息
* @param userId
* @return
*/
@Select("select * from account where uid = #{userId}")
List<Account> findAccountByUid(Integer userId);
}
一对多:
package com.mantishell.dao;
import com.mantishell.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
@CacheNamespace(blocking = true)
public interface IUserDao {
/**
* 查询所有用户
* @return
*/
@Select("select * from user")
@Results(id="userMap",value={
@Result(id=true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday"),
@Result(property = "accounts",column = "id",
many = @Many(select = "com.mantishell.dao.IAccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
})
List<User> findAll();
/**
* 根据id查询用户
* @param userId
* @return
*/
@Select("select * from user where id=#{id} ")
@ResultMap("userMap")
User findById(Integer userId);
/**
* 根据用户名称模糊查询
* @param username
* @return
*/
@Select("select * from user where username like #{username} ")
@ResultMap("userMap")
List<User> findUserByName(String username);
}