首页 > 其他 > 详细

mybatis关联关系中的对一关联

时间:2017-01-19 23:14:20      阅读:510      评论:0      收藏:0      [点我收藏+]

1、Peson.java简述:(省略getter setter方法)

    

public class Person {

private PersonInfo personInfo; //对一关联person 对一关联personInfo,

private Integer id;

private String name;

private Integer age;

private String remark;

    ...

}

2、PersonInfo.java简述(省略setter getter)

    

public class PersonInfo {

private Integer id;

private String station;

private Date joinDate;

    ...

}

3、映射文件personInfoMapper.xml

    

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 配置命名空间,区别名称 -->

<mapper namespace="cn.itcast.mapper.PersonInfoMapper">

<!-- 中间,对象的属性和结果集的字段之间的对应关系 -->

<resultMap type="cn.itcast.domain.Person" id="personInfoRM">

<!-- 主键映射 -->

<id property="id" column="ID"/>

<!-- 普通字段 property指实体的属性;column结果集的字段名称 -->

<result property="name" column="USERNAME"/>

<result property="age" column="AGE"/>

<result property="remark" column="REMARK"/>

</resultMap>

<!-- 对象关联 -->

<!-- (1)使用继承方式,通过association标签配置对一关联关系  -->

<resultMap type="cn.itcast.domain.Person" id="personPersonInfoRM" extends="personInfoRM">

<association property="personInfo" javaType="cn.itcast.domain.PersonInfo">

<id property="id" column="INFOID"/>

<result property="station" column="STATION"/>

<result property="joinDate" column="JOIN_DATE"/>

</association>

</resultMap>

<!-- 查询,人员和人员扩展信息 -->

<select id="findPersonInfo" parameterType="map" resultMap="personPersonInfoRM">

        <!--左外链接查询-->

SELECT 

person.*,person_info.id AS                                   infoid,person_info.join_date,person_info.station

FROM 

person LEFT JOIN person_info ON person.id=person_info.id

<where>

<if test="name!=null">username=#{name}</if>

</where>

</select>

</mapper>

4、测试方法testFindPersonInfo.java

    

package test;


import java.io.IOException;

import java.io.InputStream;

import java.io.Reader;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Before;

import org.junit.Test;


import cn.itcast.domain.Person;


public class PersonInfoTest {

private SqlSessionFactory sqlSessionFactory;

//获取sqlSession

@Before

public void get() throws IOException{

String resource = "sqlMapConfig.xml";

InputStream inputStream = Resources.getResourceAsStream(resource);

sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

}

/**

* 根据Person与PersonInfo之间的对一映射查询person信息

*/

@Test

public void testFindPersonInfo(){

SqlSession session = sqlSessionFactory.openSession();

//如何访问mapper中的方法呢?  规则:命名空间+.+id

Map map = new HashMap();

List<Person> persons =                 session.selectList("cn.itcast.mapper.PersonInfoMapper.findPersonInfo", map);

for (Person person : persons) {

System.out.println(person);

}

}

}

<--------------------------------新手上路,有错误地方还望指正----------------------->

本文出自 “12285883” 博客,请务必保留此出处http://12295883.blog.51cto.com/12285883/1893253

mybatis关联关系中的对一关联

原文:http://12295883.blog.51cto.com/12285883/1893253

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!