当前位置:   article > 正文

Mybatis学习笔记:(二) java中的实体(对象、List、Set)关系怎样与数据库关联映射_mybatis set key

mybatis set key

其实我刚开始学的时候记得就踩过这个坑,总是搞不清楚 那个resultMap中的关联层次,以及参数传递的关系,今天研究看了一下其实是很简单只是以前没有太多的实践,所以不明白其中的机制是什么样的。


Association联合select使用值得注意的细节地方:

这是我使用mybatis的Association联合select使用存着问题的地方,

  1. <association property="module" column="module_id" select="queryModule"> </association>
  2. <select id="queryModule" resultMap="moduleMap" >
  3. select * from sys_module where id =#{id}
  4. </select>

其实类似: select * from sys_module where id =resultSet.getInt("module_id");

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

 XML中只要有方法,就可以使用,而调用的方式就是:namespace+方法名;
 例外使用resultType时,一定要保证,你属性名与字段名相同;
 如果不相同,就使用resultMap 。

下面来个实验:

一、一对一关系

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
  1. package com.cy.mybatis.beans;
  2. import java.io.Serializable;
  3. /**
  4. * one to one
  5. * @author acer
  6. *
  7. */
  8. public class WifeBean implements Serializable{
  9. private static final long serialVersionUID = 1L;
  10. private Integer id;
  11. private String name;
  12. private HusbandBean husband;
  13. public WifeBean() {
  14. super();
  15. }
  16. public WifeBean(Integer id, String name, HusbandBean husband) {
  17. super();
  18. this.id = id;
  19. this.name = name;
  20. this.husband = husband;
  21. }
  22. public Integer getId() {
  23. return id;
  24. }
  25. public void setId(Integer id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public HusbandBean getHusband() {
  35. return husband;
  36. }
  37. public void setHusband(HusbandBean husband) {
  38. this.husband = husband;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Wife [id=" + id + ", name=" + name + ", husband=" + husband
  43. + "]";
  44. }
  45. }

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

HusbandMapper

  1. package com.cy.mybatis.mapper;
  2. import com.cy.mybatis.beans.HusbandBean;
  3. public interface HusbandMapper {
  4. /**
  5. * 根据id查询丈夫信息
  6. * @param id
  7. * @return
  8. * @throws Exception
  9. */
  10. public HusbandBean selectHusbandById (int id) throws Exception;
  11. /**
  12. * 根据id查询丈夫与妻子信息
  13. * @param id
  14. * @return
  15. * @throws Exception
  16. */
  17. public HusbandBean selectHusbandAndWife(int id) throws Exception;
  18. }

 4)定义HusbandMapper.xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cy.mybatis.mapper.HusbandMapper">
  4. <resultMap type="HusbandBean" id="husbandAndWife">
  5. <id property="id" column="id" javaType="java.lang.Integer"/>
  6. <result property="name" column="name" javaType="java.lang.String"/>
  7. <!-- association – 一个复杂的类型关联;许多结果将包成这种类型
  8. 嵌入结果映射 – 结果映射自身的关联,或者参考一个
  9. column="id" 这里的id指的是在t_wife表来的主键id
  10. 这个查询妻子,所以在妻子mapper里有个方法 -->
  11. <association property="wife" column="id" javaType="WifeBean" select="com.cy.mybatis.mapper.WifeMapper.selectWifeByHusbandId" ></association>
  12. </resultMap>
  13. <!-- resultType 返回类型 从这条语句中返回的期望类型的类的完全限定名或别名
  14. 。-->
  15. <select id="selectHusbandById" resultType="HusbandBean">
  16. select * from t_husband where id=#{id}
  17. </select>
  18. <!-- resultMap 命名引用外部的 resultMap。返回的是一个集合。-->
  19. <select id="selectHusbandAndWife" resultMap="husbandAndWife">
  20. select * from t_husband where id=#{id}
  21. </select>
  22. </mapper>

  在WifeMapper.xml里有个方法
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cy.mybatis.mapper.WifeMapper">
  4. <select id="selectWifeByHusbandId" resultType="WifeBean">
  5. select * from t_wife where fk_husband_id = #{id}
  6. </select>
  7. </mapper>

5)写个实现
  1. package com.cy.mybatis.service;
  2. import org.apache.ibatis.session.SqlSession;
  3. import com.cy.mybatis.beans.HusbandBean;
  4. import com.cy.mybatis.mapper.HusbandMapper;
  5. import com.cy.mybatis.tools.DBTools;
  6. public class OneToOneService {
  7. public static void main(String[] args) {
  8. selectHusbandAndWife();
  9. }
  10. private static void selectHusbandAndWife() {
  11. SqlSession session = DBTools.getSession();
  12. HusbandMapper hm = session.getMapper(HusbandMapper.class);
  13. try {
  14. HusbandBean husband = hm.selectHusbandAndWife(1);
  15. System.out.println(husband);
  16. session.commit();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }

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

二、一对多关系

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

 

KeyBean.java
  1. package com.cy.mybatis.beans;
  2. import java.io.Serializable;
  3. /**
  4. * manyTOone
  5. *
  6. *
  7. */
  8. public class KeyBean implements Serializable {
  9. private static final long serialVersionUID = 3712545874604618746L;
  10. private Integer id;
  11. private String key;
  12. private LockBean lock;
  13. public KeyBean() {
  14. super();
  15. }
  16. public KeyBean(Integer id, String key, LockBean lock) {
  17. super();
  18. this.id = id;
  19. this.key = key;
  20. this.lock = lock;
  21. }
  22. public Integer getId() {
  23. return id;
  24. }
  25. public void setId(Integer id) {
  26. this.id = id;
  27. }
  28. public String getKey() {
  29. return key;
  30. }
  31. public void setKey(String key) {
  32. this.key = key;
  33. }
  34. public LockBean getLock() {
  35. return lock;
  36. }
  37. public void setLock(LockBean lock) {
  38. this.lock = lock;
  39. }
  40. @Override
  41. public String toString() {
  42. return "KeyBean [id=" + id + ", key=" + key + ", lock=" + lock + "]";
  43. }
  44. }
  1. package com.cy.mybatis.beans;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. /**
  5. * oneTOmany
  6. *
  7. *
  8. */
  9. public class LockBean implements Serializable{
  10. private static final long serialVersionUID = 7092410462131162665L;
  11. private Integer id;
  12. private String lock;
  13. private List<KeyBean> keys;
  14. public LockBean() {
  15. super();
  16. }
  17. public LockBean(Integer id, String lock, List<KeyBean> keys) {
  18. super();
  19. this.id = id;
  20. this.lock = lock;
  21. this.keys = keys;
  22. }
  23. public Integer getId() {
  24. return id;
  25. }
  26. public void setId(Integer id) {
  27. this.id = id;
  28. }
  29. public String getLock() {
  30. return lock;
  31. }
  32. public void setLock(String lock) {
  33. this.lock = lock;
  34. }
  35. public List<KeyBean> getKeys() {
  36. return keys;
  37. }
  38. public void setKeys(List<KeyBean> keys) {
  39. this.keys = keys;
  40. }
  41. @Override
  42. public String toString() {
  43. return "LockBean [id=" + id + ", keys=" + keys + ", lock=" + lock + "]";
  44. }
  45. }

2.3) 建立接口

KeyMapper.java

  1. package com.cy.mybatis.mapper;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Param;
  4. import com.cy.mybatis.beans.KeyBean;
  5. public interface KeyMapper {
  6. /**
  7. * 批量添加钥匙
  8. * @return
  9. * 提倡 这样使用 @Param("keys")
  10. */
  11. public int batchSaveKeys(@Param("keys")List<KeyBean> keys);
  12. }

LockMapper.java
  1. package com.cy.mybatis.mapper;
  2. import org.apache.ibatis.annotations.Param;
  3. import com.cy.mybatis.beans.LockBean;
  4. public interface LockMapper {
  5. /**
  6. * 添加锁
  7. * @param lock
  8. * @return
  9. */
  10. public int saveLock(@Param("lock")LockBean lock);
  11. /**
  12. * 根据ID查询锁的资料
  13. * @param id
  14. * @return
  15. */
  16. public LockBean findLockById(int id);
  17. /**
  18. * 根据ID查询锁与钥匙的资料
  19. * one2many
  20. * @param id
  21. * @return
  22. */
  23. public LockBean findLockAndKeys(int id);
  24. }

2.4) 建立xml文件

KeyMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cy.mybatis.mapper.KeyMapper">
  4. <resultMap id="keyMap" type="KeyBean">
  5. <id property="id" column="id" javaType="java.lang.Integer"/>
  6. <result property="key" column="key_name" javaType="java.lang.String"/>
  7. </resultMap>
  8. <!--collection 为用于遍历的元素(必选),支持数组、List、Set -->
  9. <!-- item 表示集合中每一个元素进行迭代时的别名. -->
  10. <!--separator表示在每次进行迭代之间以什么符号作为分隔 符. -->
  11. <insert id="batchSaveKeys">
  12. insert into t_key values
  13. <foreach collection="keys" item="key" separator=",">
  14. (null,#{key.key},#{key.lock.id})
  15. </foreach>
  16. </insert>
  17. <select id="findKeysByLockId" resultMap="keyMap">
  18. select * from t_key where fk_lock_id = #{id}
  19. </select>
  20. </mapper>

LockMapper.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.cy.mybatis.mapper.LockMapper">
  4. <!--自定义返回类型 -->
  5. <resultMap id="lockMap" type="LockBean">
  6. <id property="id" column="id" javaType="java.lang.Integer"/>
  7. <result property="lock" column="lock_name" javaType="java.lang.String"/>
  8. </resultMap>
  9. <!--自定义返回类型 -->
  10. <resultMap id="lockAndKeysMap" type="LockBean">
  11. <id property="id" column="id" javaType="java.lang.Integer"/>
  12. <result property="lock" column="lock_name" javaType="java.lang.String"/>
  13. <collection property="keys" column="id" select="com.cy.mybatis.mapper.KeyMapper.findKeysByLockId"></collection>
  14. </resultMap>
  15. <insert id="saveLock">
  16. insert into t_lock values (null,#{lock.lock})
  17. </insert>
  18. <select id="findLockById" resultMap="lockMap">
  19. select * from t_lock where id= #{id}
  20. </select>
  21. <select id="findLockAndKeys" resultMap="lockAndKeysMap">
  22. select * from t_lock where id= #{id}
  23. </select>
  24. </mapper>

2.5 ) 实现
  1. package com.cy.mybatis.service;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.ibatis.session.SqlSession;
  5. import com.cy.mybatis.beans.KeyBean;
  6. import com.cy.mybatis.beans.LockBean;
  7. import com.cy.mybatis.mapper.KeyMapper;
  8. import com.cy.mybatis.mapper.LockMapper;
  9. import com.cy.mybatis.tools.DBTools;
  10. public class OneToManyService {
  11. public static void main(String[] args) {
  12. // saveLock();
  13. // batchSaveKeys();
  14. findLockAndKeys();
  15. }
  16. private static void findLockAndKeys() {
  17. SqlSession session = DBTools.getSession();
  18. LockMapper lm = session.getMapper(LockMapper.class);
  19. LockBean lock = lm.findLockAndKeys(1);
  20. System.out.println(lock);
  21. }
  22. private static void batchSaveKeys() {
  23. SqlSession session = DBTools.getSession();
  24. LockMapper lm = session.getMapper(LockMapper.class);
  25. KeyMapper km = session.getMapper(KeyMapper.class);
  26. LockBean lock = lm.findLockById(1);
  27. List<KeyBean> keys = new ArrayList<KeyBean>();
  28. for(int i = 0; i < 5; i++){
  29. KeyBean key = new KeyBean(null, "钥匙"+i, lock);
  30. keys.add(key);
  31. }
  32. km.batchSaveKeys(keys);
  33. session.commit();
  34. }
  35. private static void saveLock() {
  36. SqlSession session = DBTools.getSession();
  37. LockMapper lm = session.getMapper(LockMapper.class);
  38. LockBean lock = new LockBean(null, "锁1", null);
  39. lm.saveLock(lock);
  40. session.commit();
  41. }
  42. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/625678
推荐阅读
相关标签
  

闽ICP备14008679号