赞
踩
其实我刚开始学的时候记得就踩过这个坑,总是搞不清楚 那个resultMap中的关联层次,以及参数传递的关系,今天研究看了一下其实是很简单只是以前没有太多的实践,所以不明白其中的机制是什么样的。
Association联合select使用值得注意的细节地方:
这是我使用mybatis的Association联合select使用存着问题的地方,
- <association property="module" column="module_id" select="queryModule"> </association>
- <select id="queryModule" resultMap="moduleMap" >
- select * from sys_module where id =#{id}
- </select>
mybatis实际是对XML进行操作,我们所有的方法都直接定义在XML中,写个接口只是为了更好的符合我们3层的思想,如果不写接口,直接通过session也可以直接操作xml中的方法 ,
下面来个实验:
HusbandBean
package com.cy.mybatis.beans;import java.io.Serializable;/** * one to one * @author acer * */public class HusbandBean implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private String name; private WifeBean wife; public
HusbandBean() { super(); } public HusbandBean(Integer id, String name, WifeBean wife) { super(); this.id = id; this.name = name; this.wife = wife; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName()
{ return name; } public void setName(String name) { this.name = name; } public WifeBean getWife() { return wife; } public void setWife(WifeBean wife) { this.wife = wife; } @Override public String toString() { return "Husband [id=" + id + ", name=" + name +
", wife=" + wife + "]"; } }WifeBean.java- package com.cy.mybatis.beans;
-
- import java.io.Serializable;
- /**
- * one to one
- * @author acer
- *
- */
- public class WifeBean implements Serializable{
-
- private static final long serialVersionUID = 1L;
- private Integer id;
- private String name;
- private HusbandBean husband;
- public WifeBean() {
- super();
- }
- public WifeBean(Integer id, String name, HusbandBean husband) {
- super();
- this.id = id;
- this.name = name;
- this.husband = husband;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public HusbandBean getHusband() {
- return husband;
- }
- public void setHusband(HusbandBean husband) {
- this.husband = husband;
- }
- @Override
- public String toString() {
- return "Wife [id=" + id + ", name=" + name + ", husband=" + husband
- + "]";
- }
- }
3)接下来建立两个接口,HusbandMapper,WifeMapper.
HusbandMapper
- package com.cy.mybatis.mapper;
-
- import com.cy.mybatis.beans.HusbandBean;
-
- public interface HusbandMapper {
- /**
- * 根据id查询丈夫信息
- * @param id
- * @return
- * @throws Exception
- */
- public HusbandBean selectHusbandById (int id) throws Exception;
-
- /**
- * 根据id查询丈夫与妻子信息
- * @param id
- * @return
- * @throws Exception
- */
- public HusbandBean selectHusbandAndWife(int id) throws Exception;
-
- }
4)定义HusbandMapper.xml文件- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.cy.mybatis.mapper.HusbandMapper">
-
- <resultMap type="HusbandBean" id="husbandAndWife">
- <id property="id" column="id" javaType="java.lang.Integer"/>
- <result property="name" column="name" javaType="java.lang.String"/>
-
- <!-- association – 一个复杂的类型关联;许多结果将包成这种类型
- 嵌入结果映射 – 结果映射自身的关联,或者参考一个
- column="id" 这里的id指的是在t_wife表来的主键id
- 这个查询妻子,所以在妻子mapper里有个方法 -->
- <association property="wife" column="id" javaType="WifeBean" select="com.cy.mybatis.mapper.WifeMapper.selectWifeByHusbandId" ></association>
- </resultMap>
-
- <!-- resultType 返回类型 从这条语句中返回的期望类型的类的完全限定名或别名
- 。-->
- <select id="selectHusbandById" resultType="HusbandBean">
- select * from t_husband where id=#{id}
- </select>
-
- <!-- resultMap 命名引用外部的 resultMap。返回的是一个集合。-->
- <select id="selectHusbandAndWife" resultMap="husbandAndWife">
- select * from t_husband where id=#{id}
- </select>
-
-
-
-
- </mapper>
在WifeMapper.xml里有个方法- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
- <mapper namespace="com.cy.mybatis.mapper.WifeMapper">
- <select id="selectWifeByHusbandId" resultType="WifeBean">
- select * from t_wife where fk_husband_id = #{id}
-
- </select>
- </mapper>
5)写个实现- package com.cy.mybatis.service;
-
- import org.apache.ibatis.session.SqlSession;
-
- import com.cy.mybatis.beans.HusbandBean;
- import com.cy.mybatis.mapper.HusbandMapper;
- import com.cy.mybatis.tools.DBTools;
-
-
-
-
- public class OneToOneService {
-
- public static void main(String[] args) {
- selectHusbandAndWife();
-
- }
-
-
- private static void selectHusbandAndWife() {
- SqlSession session = DBTools.getSession();
- HusbandMapper hm = session.getMapper(HusbandMapper.class);
- try {
- HusbandBean husband = hm.selectHusbandAndWife(1);
- System.out.println(husband);
- session.commit();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
注意:那个工具类还是前一章那样写的,就相当与在昨天的基础上建立的。
二、一对多关系
还是通过例子来解释说明。(一把锁对应多把钥匙)。
KeyBean.java- package com.cy.mybatis.beans;
-
- import java.io.Serializable;
- /**
- * manyTOone
- *
- *
- */
- public class KeyBean implements Serializable {
-
-
- private static final long serialVersionUID = 3712545874604618746L;
-
- private Integer id;
- private String key;
-
- private LockBean lock;
-
- public KeyBean() {
- super();
- }
-
- public KeyBean(Integer id, String key, LockBean lock) {
- super();
- this.id = id;
- this.key = key;
- this.lock = lock;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getKey() {
- return key;
- }
-
- public void setKey(String key) {
- this.key = key;
- }
-
- public LockBean getLock() {
- return lock;
- }
-
- public void setLock(LockBean lock) {
- this.lock = lock;
- }
-
- @Override
- public String toString() {
- return "KeyBean [id=" + id + ", key=" + key + ", lock=" + lock + "]";
- }
-
- }
- package com.cy.mybatis.beans;
-
- import java.io.Serializable;
- import java.util.List;
- /**
- * oneTOmany
- *
- *
- */
- public class LockBean implements Serializable{
-
- private static final long serialVersionUID = 7092410462131162665L;
-
- private Integer id;
- private String lock;
-
- private List<KeyBean> keys;
-
- public LockBean() {
- super();
- }
-
- public LockBean(Integer id, String lock, List<KeyBean> keys) {
- super();
- this.id = id;
- this.lock = lock;
- this.keys = keys;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getLock() {
- return lock;
- }
-
- public void setLock(String lock) {
- this.lock = lock;
- }
-
- public List<KeyBean> getKeys() {
- return keys;
- }
-
- public void setKeys(List<KeyBean> keys) {
- this.keys = keys;
- }
-
- @Override
- public String toString() {
- return "LockBean [id=" + id + ", keys=" + keys + ", lock=" + lock + "]";
- }
-
- }
2.3) 建立接口
KeyMapper.java
- package com.cy.mybatis.mapper;
-
- import java.util.List;
-
- import org.apache.ibatis.annotations.Param;
-
- import com.cy.mybatis.beans.KeyBean;
-
- public interface KeyMapper {
- /**
- * 批量添加钥匙
- * @return
- * 提倡 这样使用 @Param("keys")
- */
- public int batchSaveKeys(@Param("keys")List<KeyBean> keys);
- }
LockMapper.java- package com.cy.mybatis.mapper;
-
- import org.apache.ibatis.annotations.Param;
-
- import com.cy.mybatis.beans.LockBean;
-
- public interface LockMapper {
- /**
- * 添加锁
- * @param lock
- * @return
- */
- public int saveLock(@Param("lock")LockBean lock);
-
- /**
- * 根据ID查询锁的资料
- * @param id
- * @return
- */
- public LockBean findLockById(int id);
-
- /**
- * 根据ID查询锁与钥匙的资料
- * one2many
- * @param id
- * @return
- */
- public LockBean findLockAndKeys(int id);
-
- }
2.4) 建立xml文件
KeyMapper.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.cy.mybatis.mapper.KeyMapper">
-
- <resultMap id="keyMap" type="KeyBean">
- <id property="id" column="id" javaType="java.lang.Integer"/>
- <result property="key" column="key_name" javaType="java.lang.String"/>
- </resultMap>
-
-
- <!--collection 为用于遍历的元素(必选),支持数组、List、Set -->
- <!-- item 表示集合中每一个元素进行迭代时的别名. -->
- <!--separator表示在每次进行迭代之间以什么符号作为分隔 符. -->
- <insert id="batchSaveKeys">
- insert into t_key values
- <foreach collection="keys" item="key" separator=",">
- (null,#{key.key},#{key.lock.id})
- </foreach>
- </insert>
-
- <select id="findKeysByLockId" resultMap="keyMap">
- select * from t_key where fk_lock_id = #{id}
- </select>
-
- </mapper>
LockMapper.xml- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.cy.mybatis.mapper.LockMapper">
- <!--自定义返回类型 -->
- <resultMap id="lockMap" type="LockBean">
- <id property="id" column="id" javaType="java.lang.Integer"/>
- <result property="lock" column="lock_name" javaType="java.lang.String"/>
- </resultMap>
-
- <!--自定义返回类型 -->
- <resultMap id="lockAndKeysMap" type="LockBean">
- <id property="id" column="id" javaType="java.lang.Integer"/>
- <result property="lock" column="lock_name" javaType="java.lang.String"/>
-
- <collection property="keys" column="id" select="com.cy.mybatis.mapper.KeyMapper.findKeysByLockId"></collection>
- </resultMap>
-
- <insert id="saveLock">
- insert into t_lock values (null,#{lock.lock})
- </insert>
-
- <select id="findLockById" resultMap="lockMap">
- select * from t_lock where id= #{id}
- </select>
-
- <select id="findLockAndKeys" resultMap="lockAndKeysMap">
- select * from t_lock where id= #{id}
- </select>
-
- </mapper>
2.5 ) 实现- package com.cy.mybatis.service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.ibatis.session.SqlSession;
-
- import com.cy.mybatis.beans.KeyBean;
- import com.cy.mybatis.beans.LockBean;
- import com.cy.mybatis.mapper.KeyMapper;
- import com.cy.mybatis.mapper.LockMapper;
- import com.cy.mybatis.tools.DBTools;
-
- public class OneToManyService {
- public static void main(String[] args) {
- // saveLock();
- // batchSaveKeys();
- findLockAndKeys();
- }
-
- private static void findLockAndKeys() {
-
- SqlSession session = DBTools.getSession();
- LockMapper lm = session.getMapper(LockMapper.class);
- LockBean lock = lm.findLockAndKeys(1);
- System.out.println(lock);
- }
-
- private static void batchSaveKeys() {
-
- SqlSession session = DBTools.getSession();
- LockMapper lm = session.getMapper(LockMapper.class);
- KeyMapper km = session.getMapper(KeyMapper.class);
-
- LockBean lock = lm.findLockById(1);
- List<KeyBean> keys = new ArrayList<KeyBean>();
- for(int i = 0; i < 5; i++){
- KeyBean key = new KeyBean(null, "钥匙"+i, lock);
- keys.add(key);
- }
- km.batchSaveKeys(keys);
- session.commit();
- }
-
- private static void saveLock() {
- SqlSession session = DBTools.getSession();
- LockMapper lm = session.getMapper(LockMapper.class);
- LockBean lock = new LockBean(null, "锁1", null);
- lm.saveLock(lock);
- session.commit();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。