mybatis 一对一 一对多 多对多

 一对一

             account                                                                          users

                    

1 . account 继承users

accountMapper.xml 中配置 resultMap ,映射user中的属性

<resultMap id="accounts" type="account">
    <id property="a_id" column="aid"></id>
    <result property="aname" column="aname"></result>
    <result property="money" column="money"></result>
    <result property="uid" column="uid"></result>
    <!--映射user-->
   
<result property="uid" column="id"></result>
    <result property="username" column="name"></result>
    <result property="age" column="age"></result>
    <result property="birthday" column="birthday"></result>
</resultMap>
<!--查询所有-->
<select id="FindAll" resultMap="accounts">
   select * from account a,users u where a.aid = u.id
</select>

2 . account 中包含user属性

用association 关联一个对象

association 关联对象的映射, 只能关联一个对象

            property: 映射属性名

            javaType:   加载对象的java数据类型

<resultMap id="accounts" type="account">
    <id property="a_id" column="aid"></id>
    <result property="aname" column="aname"></result>
    <result property="money" column="money"></result>
    <result property="uid" column="uid"></result>
    <!--映射user-->
   
<association property="user" javaType="user">
        <id property="uid" column="id"></id>
        <result property="username" column="name"></result>
        <result property="age" column="age"></result>
        <result property="birthday" column="birthday"></result>
    </association>
</resultMap>

<!--查询所有-->
<select id="FindAll" resultMap="accounts">
   select from account a,users u where a.aid = u.id
</select>

3 . 使用user引用user中的属性名

<resultMap id="accounts" type="account">
<id property="a_id" column="aid"></id>
<result property="aname" column="aname"></result>
<result property="money" column="money"></result>
<result property="uid" column="uid"></result>
    <!---user对象的映射: 使用user来引用user中的属性名-->
   
<result property="user.uid" column="id"></result>
    <result property="user.username" column="name"></result>
    <result property="user.age" column="age"></result>
    <result property="user.birthday" column="birthday"></result>
</resultMap>

<!--查询所有-->
<select id="FindAll" resultMap="accounts">
   select from account a,users u where a.aid = u.id
</select>

一对多

user表和account表

一个用户有多个账户:在用户中定义一个账户集合

private List<Account> accountList = new ArrayList<Account>();

 

collection:关联多个对象

            accountList: 映射的属性名

            ofType:关联的类型

<mapper namespace="com.wqy.mapper.UserMapper">
    <resultMap id="users" type="user">
        <id column="id" property="uid"></id>
        <result column="name" property="username"></result>
        <result column="age" property="age"></result>
        <result column="birthday" property="birthday"></result>
        <!--映射account-->
       
<collection property="accountList" ofType="account">
            <id property="a_id" column="aid"></id>
            <result property="aname" column="aname"></result>
            <result property="money" column="money"></result>
            <result property="uid" column="uid"></result>
        </collection>
    </resultMap>
    <!--查询所有-->
   
<select id="findAll" resultMap="users">
        select * from users u ,account a where u.id = a.uid
    </select>

多对多

用户表:users                                                                   角色表role                                    中间表 users_role

                                    

即两个一对多

在用户对象中添加属性:

private List<Role> roleList = new ArrayList<Role>();

 

在角色对象中添加属性:

private List<User> userList = new ArrayList<User>();

 

注解

@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态 SQL映射

 

@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集

/**

     * 查询全部

     * @return

     * @Results:映射结果集:多列数据

     * @Result:映射单列的数据

     *          id:是否为主键:true:为主键          false:不是主键

     *    如果列名与属性名不一致,必须配置

     *    如果列名与属性名一致,则可以不配置

     */


@Results({
        @Result(id = true,property = "uid",column = "id"),
        @Result(property = "username",column = "name")
})
@Select("select * from users")
List<User> findAll();

@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=""))

Account中有User属性
private User user;

 

public interface AccountMapper {

@Results({
@Result(id=true,property = "a_id",column = "aid"),
@Result(property = "user",column = "uid",one=@One(select = "com.wqy.mapper.UserMapper.findUserByid",fetchType = FetchType.LAZY))
})
@Select("select * from account ")
List<Account> FindAll();
}

uid为account表中的外键字段,根据uid去查用户

 

public interface UserMapper {


@Results({
@Result(id = true,property = "uid",column = "id"),
@Result(property = "username",column = "name"),
})
@Select("select * from users where id = #{id}")
User findUserByid(Integer id);
}

 

@Many  注解(多对一)

User 中的属性

private List<Account> accountList = new ArrayList<Account>();

代替了<Collection> 标签, 是是多表查询的关键,在注解中用来指定子查询返回对象集合。

注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义;

使用格式:

public interface UserMapper {

@Results({
@Result(id = true,property = "uid",column = "id"),
@Result(property = "username",column = "name"),
@Result(property = "accountList",column = "uid",javaType = List.class,
many = @Many(select = "com.wqy.mapper.AccountMapper.FindAllByUid"))
})
@Select("select * from users ")
List<User> findAll();
}

 

 

public interface AccountMapper {

@Results({
@Result(id=true,property = "a_id",column = "aid")
})
@Select("select * from account ")
List<Account> FindAllByUid(Integer id);
}

 

 @SelectKey

/**
 * selectKey:查询主键值
 *  属性:keyColumn:表中的列名
 *   keyProperty:pojo的属性名
 *   before: 在添加之前查询,还是在之后查询,true:在之前查询,false:在之后查询
 *  resultType:返回值类型
 *  statement:  sql语句
 */

@Results({
        @Result(id = true,property = "uid",column = "id"),
        @Result(property = "username",column = "name")
})
@SelectKey(keyProperty = "uid",keyColumn = "id",before = false,
        resultType = Integer.class,statement = {"select last_insert_id()"})
@Insert("insert into users values (null,name,age,birthday)")
void save(User user);

 

 

 

/**

     * 查询全部

     * @return

     * @Results:映射结果集:多列数据

     * @Result:映射单列的数据

     *          id:是否为主键:true:为主键 false:不是主键

     *  如果列名与属性名不一致,必须配置

     *  如果列名与属性名一致,则可以不配置

     *  @Result:如果映射的一对一

     *      javaType:属性对应的类型是什么

     *      property:属性名

     *      column:表的列名

     *      one: 对应一条记录

     *      @One(select = "com.itheima.dao.UserDao.findByUid"):去查询一个对象返回

     *          属性:fetchType:默认为立即加载

     *               可以配置为:FetchType.LAZY: 延迟加载,只有在使用的时候才会加载该对象

     */

 


动态SQL:

public class UserProvider {
public String findByConditionSql(String name, Integer age) {
StringBuilder sb = new StringBuilder();
sb.append("select * from users where 1=1 ");
if (name != null && name != "") {
sb.append(" and name = \"%\"#{param1}\"%\" ");
}
if (age != null) {
sb.append(" and age > #{param2}");
}

return sb.toString();
}
}

 

//    @Select("select * from users where name like \"%\"#{param1}\"%\" and age > #{param2}" )
   
@SelectProvider(type = UserProvider.class,method = "findByConditionSql")
    List<User> findBycondition(String name,Integer age);














posted on 2018-10-28 22:10  Cyan_W  阅读(762)  评论(0编辑  收藏  举报

导航