Mybatis--表关联one-to-one
来源:互联网 发布:西北师大知行学院贴吧 编辑:程序博客网 时间:2024/04/30 10:55
【1】两个表结构,其中t_class表拥有外键 t_id,对应t_teacher表主键,t_id
表结构如下:
【2】对应的model
Classes:
public class Classes { private int id; private String name; private Teacher teacher; ...}
Teacher:
public class Teacher { private int id; private String name; ...}
【3】classMapper.xml配置
<mapper namespace="com.web.mapper.classMapper"><!-- ************the first way--嵌套结果************ --> <select id="getClass" parameterType="int" resultMap="classResultMap"> select * from t_class c,t_teacher t where c.t_id = t.t_id and c.c_id = #{id} </select> <!-- 可以解决model属性名与数据表中column列名不一致问题 jdbcType一定要大写 --> <resultMap type="Classes" id="classResultMap"> <id property="id" column="c_id" javaType="int" jdbcType="INTEGER"/> <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/> <!-- one to one or one to many ,the many side !! here,use association not collection--> <association property="teacher" column="t_id" javaType="Teacher" > <!--here,the column='t_id' is not necessary !--> <!-- javaType=Teacher||teacher type=Classes||classes the two all right !!! --> <id property="id" column="t_id" javaType="int" jdbcType="INTEGER"/> <result property="name" column="t_name" javaType="string" jdbcType="VARCHAR"/> </association> </resultMap> <!-- ********the two way--return resultMap********** --> <select id="getClassById" parameterType="int" resultMap="classResultMap2"> select * from t_class c where c.c_id = #{id} </select> <resultMap type="java.util.Map" id="classResultMap2"> <result property="id" column="c_id" javaType="int" jdbcType="INTEGER"/> <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/> </resultMap> <!-- *********the three way--嵌套查询*************** --> <select id="getClass3" parameterType="int" resultMap="classResultMap3"> select * from t_class c where c.c_id = #{id} </select> <!-- resultType should use column alias !!! --> <select id="getTeacher" parameterType="int" resultType="Teacher"> select t_id id,t_name name from t_teacher where t_id = #{id} </select> <resultMap type="Classes" id="classResultMap3"> <result property="id" column="c_id" javaType="int" jdbcType="INTEGER"/> <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/> <association property="teacher" javaType="Teacher" select="getTeacher" column="t_id"> <!-- here,the javaType is not necessary !--> </association> </resultMap></mapper>
【4】Test
- test—getClass:
@Test public void testSelect(){ /*set auto commit ,which equals to the above*/ SqlSession session = MybatisUtils.getFactory().openSession(true); String statement = "com.web.mapper.classMapper.getClass"; /*return the effect rows*/ Classes c = session.selectOne(statement, 1); System.out.println("result.."+c); Teacher teacher = c.getTeacher(); System.out.println("get from class..."+teacher); }
result as follows :
- test-getClassById:
测试返回为map,controller中手动封装为Classes对象–这里没有附带teacher
@Test public void testSelect2(){ /*set auto commit ,which equals to the above*/ SqlSession session = MybatisUtils.getFactory().openSession(true); String statement = "com.web.mapper.classMapper.getClassById"; /*return the effect rows*/ HashMap<String, Object> classMap = session.selectOne(statement, 1); Classes classes = new Classes(classMap.get("name")+""); System.out.println("result.."+classes+','+classes.getClass()); }
result as follows :
- test-getClass3 :
嵌套查询
@Test public void testSelect3(){ /*set auto commit ,which equals to the above*/ SqlSession session = MybatisUtils.getFactory().openSession(true); String statement = "com.web.mapper.classMapper.getClass3"; /*return the effect rows*/ Classes classes = session.selectOne(statement, 1); System.out.println("result.."+classes+','+classes.getClass()); }
result as follows :
result..Classes [id=1, name=计算机, teacher=Teacher [id=1, name=李明], list=null],class com.web.model.Classes
【5】标签说明
association : 用于一对一的关联查询 ;property : 对象属性名称 ;javaType : 对象属性类型 ;column : 所对应的外键字段名称 ;select : 使用另一个查询封装的结果 ; parameterType : 参数类型 ;resultType : 返回的结果类型 ;jdbcType : 对应的jdbc字段类型,请注意要大写 !另外与使用数据库字段类型一致!!
0 0
- Mybatis--表关联one-to-one
- Mybatis - 表关联one-to-many
- hibernate one-to-one 表关联解决方案
- Hibernate One-To-One 关联
- Hibernate One-To-One 关联
- Mybatis 多表关联查询(1) one-to-one关系
- one-to-one外键双向关联之建表
- 学习笔记-----------表之间的关联(one-to-one many-to-one many-to-many)
- Hibernate one to one 关联关系映射
- hibernate one-to-one外键关联
- hibernate---->一对一关联映射 (one-to-one)
- one-to-one双向关联映射
- one-to-one双向关联之加载
- Hibernate:one-to-one 主外键关联
- Hibernate 关联映射one-to-one
- Mybatis 多表关联查询(2) one-to-many关系
- hibernate one-to-many many-to-one多了一个关联表
- Hibernate one-to-many many-to-one 双向关联
- python根据给定的年月日以数字形式打印日期
- 常用win+r运行命令总结
- 第十四周项目1-验证算法(1)-折半查找算法
- Spring配置项<context:annotation-config/>解释说明
- 第十三周项目1-Prim算法的验证
- Mybatis--表关联one-to-one
- 用顺序表实现的简易通讯录(第一版)
- Android自定义控件-彩虹条进度条
- Distributed Representations of Words and Phrasesand their Compositionality
- 同时使用parceler/butterknife/dagger2出现DuplicateFileException的解决方法
- [LeetCode]198. House Robber
- 消息游标
- 第十四周 折半查找算法的验证
- python爬取网页中javascript动态添加的内容(一)