MyBatis学习笔记(二) 关联关系

  • 时间:
  • 浏览:1

package com.cy.mybatis.mapper;

select * from t_wife where fk_husband_id = #{id}

private Integer id;

import java.util.ArrayList;

* manyTOone

import com.cy.mybatis.beans.KeyBean;

<insert id="saveLock">

<result property="name" column="name" javaType="java.lang.String"/>

session.commit();

随后不相同,就使用resultMap 。

this.id = id;

this.lock = lock;

insert into t_lock values (null,#{lock.lock}) 

this.name = name;

还是通过例子来解释说明。(一个多 妻子对应一个多 丈夫)。

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

* @param id

public LockBean findLockAndKeys(int id);

/**

你你这个查询妻子,也那末了妻子mapper里有个方式 -->

}

private String key;

</resultMap>

public void setLock(LockBean lock) {

public Integer getId() {

<!-- association – 一个多 繁杂的类型关联;你这个结果将包成你你这个类型

public List<KeyBean> getKeys() {

* 批量加进去去钥匙

this.lock = lock;

import com.cy.mybatis.tools.DBTools;

insert into t_husband values (null,'hello');

</resultMap>

this.keys = keys;

return lock;

*/

import java.util.List;

e.printStackTrace();

this.name = name;

<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

}

*

public void setName(String name) {

注意:

import com.cy.mybatis.beans.HusbandBean;

<mapper namespace="com.cy.mybatis.mapper.HusbandMapper">

*/

km.batchSaveKeys(keys);

this.key = key;

this.id = id;

LockMapper lm = session.getMapper(LockMapper.class);

import org.apache.ibatis.annotations.Param;

import java.io.Serializable;

* @return

}

</mapper>

public interface LockMapper {

</resultMap>

}

<!-- item 表示集合中每一个多 元素进行迭代时的别名. -->

*/

*/

@Override

public WifeBean getWife() {

@Override

* @param id

public class WifeBean implements Serializable{

}

}

return wife;

public HusbandBean getHusband() {

private static void saveLock() {

private static final long serialVersionUID = 7092410462131162665L;

fk_lock_id int 

<mapper namespace="com.cy.mybatis.mapper.LockMapper">

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

</resultMap>

return name;

<!-- resultType 返回类型 从这条的话中返回的期望类型的类的完整篇 限定名或别名

public LockBean() {

lock_name varchar(20)

public String toString() {

public void setId(Integer id) {

}

/**

return "Husband [id=" + id + ", name=" + name + ", wife=" + wife + "]";

session.commit();

<mapper namespace="com.cy.mybatis.mapper.KeyMapper">

private Integer id;

this.husband = husband;

}

}

* 根据id查询丈夫与妻子信息

*/

1)数据库信息

create table t_lock(

import java.util.List;

二、一对多关系

}

}

 <!--自定义返回类型 -->

<result property="lock" column="lock_name" javaType="java.lang.String"/>

public String getKey() {

<select id="selectWifeByHusbandId" resultType="WifeBean">

public String getName() {

public WifeBean() {

public String getLock() {

return keys;

<resultMap id="keyMap" type="KeyBean">

super();

public String toString() {

<!--自定义返回类型 -->

return id;

LockMapper.xml

this.key = key;

super();

2.5 ) 实现

* @throws Exception

public int batchSaveKeys(@Param("keys")List<KeyBean> keys);

<foreach collection="keys" item="key" separator=",">

public HusbandBean selectHusbandById (int id) throws Exception;

public class OneToManyService {

return lock;

);

}

结果显示:

* @param lock

husband_name varchar(20)

private String name;

select * from t_lock where id= #{id}

}

private String name;

import java.io.Serializable;

虽然在数据库里可不不可不可以 不可不可以 一方配置的外键,随后你你这个一对一是双向的关系。

public void setKey(String key) {

* 根据ID查询锁与钥匙的资料

/**

LockBean lock = new LockBean(null, "锁1", null);

selectHusbandAndWife();

return husband;

private static void batchSaveKeys() {

*/

* @return

this.id = id;

public String getName() {

private static void selectHusbandAndWife() {

fk_husband_id int

一、一对一关系

LockMapper.java

column="id" 这里的id指的是在t_wife表来的主键id 

this.husband = husband;

// saveLock();

}

import com.cy.mybatis.beans.LockBean;

}

private static final long serialVersionUID = 1L;

}

/**

}

public HusbandBean() {

* @throws Exception

package com.cy.mybatis.mapper;

SqlSession session = DBTools.getSession();

* @param id

private List<KeyBean> keys;

<id property="id" column="id" javaType="java.lang.Integer"/>

public void setKeys(List<KeyBean> keys) {

KeyMapper.xml

private String lock;

select * from t_lock where id= #{id}

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

public void setHusband(HusbandBean husband) {

return "KeyBean [id=" + id + ", key=" + key + ", lock=" + lock + "]";

key_name varchar(20),

}

lm.saveLock(lock);

super();

private static final long serialVersionUID = 1L;

}

List<KeyBean> keys = new ArrayList<KeyBean>();

KeyBean key = new KeyBean(null, "钥匙"+i, lock);

insert into t_key values 

}

*/

<select id="findLockById" resultMap="lockMap">

private static final long serialVersionUID = 3712545874804618746L;

select * from t_husband where id=#{id}

}

SqlSession session = DBTools.getSession();

import com.cy.mybatis.beans.KeyBean;

public Integer getId() {

this.name = name;

public void setWife(WifeBean wife) {

</select

System.out.println(lock);

SqlSession session = DBTools.getSession();

}

public HusbandBean(Integer id, String name, WifeBean wife) {

/**

this.lock = lock;

}

import org.apache.ibatis.session.SqlSession;

public WifeBean(Integer id, String name, HusbandBean husband) {

}

public void setId(Integer id) {

public KeyBean(Integer id, String key, LockBean lock) {

// batchSaveKeys();

super();

* 根据ID查询锁的资料

public LockBean findLockById(int id);

</insert>

this.id = id;

}

* one to one

id int primary key auto_increment,

this.keys = keys;

public static void main(String[] args) {

}

</select

public void setId(Integer id) {

* @return

package com.cy.mybatis.beans;

<resultMap type="HusbandBean" id="husbandAndWife">

<select id="selectHusbandAndWife" resultMap="husbandAndWife">

+ "]";

import java.util.List;

public class OneToOneService {

* @param id

this.wife = wife;

public void setLock(String lock) {

* one2many

<!--collection 为用于遍历的元素(必选),支持数组、List、Set -->

package com.cy.mybatis.beans;

super();

* 提倡 从前 使用 @Param("keys")

4)定义HusbandMapper.xml文件

public HusbandBean selectHusbandAndWife(int id) throws Exception;

(null,#{key.key},#{key.lock.id})

3)接下来建立一个多 接口,HusbandMapper,WifeMapper.

<select id="findLockAndKeys" resultMap="lockAndKeysMap">

}

<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

例外使用resultType时,一定要保证,你属性名与字段名相同;

<id property="id" column="id" javaType="java.lang.Integer"/>

}

}

import com.cy.mybatis.tools.DBTools;

*

5)写个实现

在WifeMapper.xml里有个方式

2)对应的JavaBean代码

import com.cy.mybatis.mapper.KeyMapper;

* @return

@Override

return "LockBean [id=" + id + ", keys=" + keys + ", lock=" + lock + "]";

* @author acer

public class KeyBean implements Serializable {

keys.add(key);

package com.cy.mybatis.beans;

HusbandMapper hm = session.getMapper(HusbandMapper.class);

HusbandBean.java

}

findLockAndKeys();

</select>

this.lock = lock;

LockMapper lm = session.getMapper(LockMapper.class);

this.id = id;

</mapper>

}

* @return

注意:那个工具类还是前一章那样写的,就相当与在昨天的基础上建立的。

this.id = id;

。-->

return id;

public KeyBean() {

this.id = id;

return key;

KeyMapper.java

import com.cy.mybatis.beans.LockBean;

id int primary key auto_increment,

select * from t_husband where id=#{id}

public String toString() {

还是通过例子来解释说明。(一把锁对应多把钥匙)。

}

this.wife = wife;

import java.io.Serializable;

<select id="selectHusbandById" resultType="HusbandBean">

}

);

<insert id="batchSaveKeys">

<result property="lock" column="lock_name" javaType="java.lang.String"/>

}

KeyBean.java

2.1)数据库信息 这里那末加进去去数据了,亲们 用批量加进去去数据

public interface KeyMapper {

}

<result property="key" column="key_name" javaType="java.lang.String"/>

}

public static void main(String[] args) {

import org.apache.ibatis.session.SqlSession;

嵌入结果映射 – 结果映射自身的关联,随后参考一个多

super();

package com.cy.mybatis.service;

import com.cy.mybatis.beans.HusbandBean;

LockMapper lm = session.getMapper(LockMapper.class);

LockBean.java

public Integer getId() {

* oneTOmany

<!-- resultMap 命名引用内外部的 resultMap。返回的是一个多 集合。-->

}

public interface HusbandMapper {

private LockBean lock;

public int saveLock(@Param("lock")LockBean lock);

public class LockBean implements Serializable{

HusbandMapper

* 加进去去锁

select * from t_key where fk_lock_id = #{id}

}

private HusbandBean husband;

*

}

public void setName(String name) {

return id;

*/

/**

}

public LockBean getLock() {

}

<id property="id" column="id" javaType="java.lang.Integer"/>

}

create table t_wife(

private WifeBean wife;

2.3) 建立接口

for(int i = 0; i < 5; i++){

this.name = name;

WifeBean.java

package com.cy.mybatis.mapper;

super();

<mapper namespace="com.cy.mybatis.mapper.WifeMapper">

<select id="findKeysByLockId" resultMap="keyMap">

<collection property="keys" column="id" select="com.cy.mybatis.mapper.KeyMapper.findKeysByLockId"></collection>

}

<resultMap id="lockAndKeysMap" type="LockBean">

);

<!--separator表示在每次进行迭代之间以哪些地方符号作为分隔 符. -->

}

public LockBean(Integer id, String lock, List<KeyBean> keys) {

insert into t_wife values(null,'kitty',1)

}

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

XML中随后我有方式 ,就可不不可不可以 不可不可以 使用,而调用的方式 可是:namespace+方式 名;

* one to one

*/

return id;

LockBean lock = lm.findLockById(1);

this.id = id;

</insert>

public String toString() {

super();

* 根据id查询丈夫信息

import java.io.Serializable;

@Override

/**

原文发布时间为:2018-08-80本文来自云栖社区合作者方式 伙伴“全栈开发者社区”,了解相关信息可不不可不可以 不可不可以 关注“全栈开发者社区”。

wife_name varchar(20),

public void setId(Integer id) {

id int primary key auto_increment,

*/

<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

System.out.println(husband);

LockBean lock = lm.findLockAndKeys(1);

}

*

return "Wife [id=" + id + ", name=" + name + ", husband=" + husband

<resultMap id="lockMap" type="LockBean">

private static void findLockAndKeys() {

import org.apache.ibatis.annotations.Param;

2.2) 实体类

return name;

create table t_husband(

</mapper>

/**

/**

<association property="wife" column="id" javaType="WifeBean" select="com.cy.mybatis.mapper.WifeMapper.selectWifeByHusbandId" ></association>

}

</mapper>

try {

SqlSession session = DBTools.getSession();

KeyMapper km = session.getMapper(KeyMapper.class);

mybatis实际是对XML进行操作,亲们 所有的方式 都直接定义在XML中,写个接口可是为了更好的符合亲们 3层的思想,随后不写接口,直接通过session也可不不可不可以 不可不可以 直接操作xml中的方式 ,

);

</foreach>

2.4) 建立xml文件

package com.cy.mybatis.service;

</select>

}

<id property="id" column="id" javaType="java.lang.Integer"/>

public class HusbandBean implements Serializable{

} catch (Exception e) {

<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

</select>

package com.cy.mybatis.beans;

private Integer id;

private Integer id;

id int primary key auto_increment,

HusbandBean husband = hm.selectHusbandAndWife(1);

</select>

/**

}

* @return

}

* @author acer

}

import com.cy.mybatis.mapper.HusbandMapper;

import com.cy.mybatis.mapper.LockMapper;

public Integer getId() {

create table t_key(

session.commit();