myBatis查询

来源:互联网 发布:苏联解体启示知乎 编辑:程序博客网 时间:2024/06/06 10:23

通过入门实例我们知道了mybatis的简单使用,这篇主要讲解mybatis的查询

1.java类(与数据库表像对应)

package com.tgb.domain;public class Teacher {private int id;private String name;......}package com.tgb.domain;import java.util.List;public class Classes {private int id;private String name;private Teacher teacher;.......}
2.,mybatis.java工具类

package com.tgb.test;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatis {public static SqlSessionFactory getSqlSessionFactory(){String resource = "conf.xml";InputStream is = TestUser.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);return sessionFactory;}public static SqlSession getsqlSqlSession(){return MyBatis.getSqlSessionFactory().openSession();}        // true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务       // false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务public static SqlSession getsqlSqlSession(boolean isAutoComment){return MyBatis.getSqlSessionFactory().openSession(isAutoComment);}}
3.ClassesMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的--><mapper namespace="com.tgb.mapping.ClassMapper">  <select id="getClass" parameterType="int" resultMap="ClassResultMap">  select * from class as c,teacher as t where c.teacher_id = t.t_id and c.c_id=#{id}  </select>  <resultMap type="com.tgb.domain.Classes" id="ClassResultMap">  <id property="id" column="c_id"/>  <result property="name" column="c_name"/>  <association property="teacher" javaType="com.tgb.domain.Teacher">  <id property="id" column="t_id"/>  <result property="name" column="t_name"/>   </association>   </resultMap>    <select id="getClass22" parameterType="int" resultMap="ClassResultMap22"> select * from class where c_id=#{id} </select> <resultMap type="com.tgb.domain.Classes" id="ClassResultMap22">     <id property="id" column="c_id"/>  <result property="name" column="c_name"/>  <association property="teacher" column="teacher_id" select="getTeacher"/>  </resultMap> <select id="getTeacher" parameterType="int" resultType="com.tgb.domain.Teacher"> select t_id id,t_name name from teacher where t_id=#{id} </select></mapper>
MyBatis中使用association标签来解决一对一的关联查询,association标签可用的属性如下:
      property:对象属性的名称
      javaType:对象属性的类型
      column:所对应的外键字段名称
      select:使用另一个查询封装的结果

4.test1_1.java单元测试

package com.tgb.selectmore;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import com.tgb.domain.Classes;import com.tgb.test.MyBatis;public class test1_1 {@Testpublic void testgetClass(){SqlSession session = MyBatis.getsqlSqlSession(true);String statemet ="com.tgb.mapping.ClassMapper.getClass";Classes  classes= session.selectOne(statemet, 2);session.close();System.out.println(classes);}@Testpublic void testGetClass(){SqlSession session = MyBatis.getsqlSqlSession(true);String statemet ="com.tgb.mapping.ClassMapper.getClass22";Classes  classes= session.selectOne(statemet, 1);session.close();System.out.println(classes);//classes +班级类class_a,id为1老师为1老师名称为teacher1}



1 0