Sql映射文件

     MyBatis真正的力量是在映射语句中。这里是奇迹发生的地方。对于所有的力量,SQL映射的XML文件是相当的简单。当然如果你将它们和对等功能的JDBC代码来比较,你会发现映射文件节省了大约95%的代      码量。MyBatis的构建就是聚焦于SQL的,使其远离于普通的方式。

SQL映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): 

>mapper:映射文件的根元素节点,只有一个属性namespace命名空间,用于区分不同的mapper,全局唯一 ,namespace绑定的DAO接口全名称,即面向接口编程。这里的mapper就相当于接口的实现类。

    

  • cache - 配置给定命名空间的缓存。
  • cache-ref – 从其他命名空间引用缓存配置。
  • resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载你的对象。
  • parameterMap – 已经被废弃了!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除。这里不会记录。
  • sql – 可以重用的SQL块,也可以被其他语句引用。
  • insert – 映射插入语句
  • update – 映射更新语句
  • delete – 映射删除语句
  • select – 映射查询语句

一:使用select完成但条件查询

      使用工具idea和mysql数据库

     创建实体类

public class student {

    private int stuId;

    private String  stuName;

    private grade getGrade;

    private  int stuAge;

    public grade getGetGrade() {
        return getGrade;
    }

    public void setGetGrade(grade getGrade) {
        this.getGrade = getGrade;
    }

    public int getStuAge() {
        return stuAge;
    }
   public student(int id,String name){

   }
   public student(){}
    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
}

使用select完成条件查询

    一:首先配置mapper使用resultType

<!--模糊查询   使用resultType返回结果集-->    
    <select id="getAllStudentByLike" parameterType="String" resultType="stu">
        select * from student where stuName like CONCAT('%',#{stuName},'%')

</select>

 

测试类

 public  void Test() throws IOException {

        studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
        List<student> list = dao.getAllStudentByLike("z");
        for (student item:list) {
            System.out.println("----------"+item.getStuName());
        }
}

 

 

另外parameterType支持的复杂类型除了javaBean之外,还包括Map类型

即修改Mapper

 <!--模糊查询-->
    <select id="getAllStudentByLike" parameterType="Map" resultType="stu">
        select * from student where stuName like CONCAT('%',#{stuName},'%')
    </select>

 

然后再测试类里创建一个 HashMap集合直接作为方法参数即可

studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
        Map<String,String> userMap = new HashMap<String, String>();
        userMap.put("stuName","z");
        List<student> list = dao.getAllStudentByLike(userMap);
        for (student item:list) {
            System.out.println("----------"+item.getStuName());
        }

 

 

不过map集合的key值必须和类中的字段名相同。

 

二:使用resultMap完成两表查询

       比如学生表里关联班级表的主键id,如果使用resultType只能展示其id但在实际中往往关注的是班级名称,所有需要使用resultMap映射自定义结果。

       

<resultMap id="studentMap" type="entity.student">
<id property="stuId" column="stuId"></id> <result property="stuName" column="stuName"></result> <result property="gradeName" column="gradeName"> </resultMap>

//sql语句
select * from student,grade 

 

   

resultType直接表示 返回 类型 ,包括基础类型和复杂数据类型

resultMap则是对外部resultMap的引用,对应resultMap的id 表示返回结果映射到 哪一个resultMap。:他的应用场景是:数据库字段信息与对象属性不一致或者需要做复杂的联合查询以便自由控制映射结果 。

 

另外在 MyBatis的select元素中,resultType和resultMap本质上是一样的,都是Map数据结构。但是 二者不能同时 存在。

三:使用resultMap的自动映射级别

     MyBatis中分为三个映射级别

     >NONE:禁止自动匹配

     >PARTIAL:(默认):自动匹配所有属性有内部嵌套(association,collection)的除外

     >FULL:自动匹配所有

  在大配置里设置autoMappingBehavior

 <settings>
       
        <!--设置resultMap的自动映射级别为Full(自动匹配所有)-->
        <setting name="autoMappingBehavior" value="FULL" />   <!--FULL要大写··-->
     
    </settings>

 

设置autoMappingBehavior的值为FULL时就不需要配置resultMap下的节点,他会根据数据库自动匹配

四:使用update完成修改

     

 <update id="update">
        update student set stuName=#{0} where stuId=#{1}
    </update>

 

这里使用占位符比较简单的一种作为参数,在测试类中就直接填参就行了

 

五:使用映射复杂类型的属性association

     前面的result只能映射到javaBean的某个“简单类型”属性,基础数据类型和包装类等/

但要映射复杂类型的属性时需要用到assocoation    复杂类xing:即一个javaBean里有另一个javaBean,但是association仅处理一对一的关联关系

    private int stuId;

    private String  stuName;

    private grade getGrade;

    private  int stuAge;

。。。。。省略封装

 

 <resultMap id="studentMap" type="entity.student">
       <!-- <id property="stuId" column="stuId"></id>
        <result property="stuName" column="stuName"></result>-->
        <!--关联另一个 属性-->
        <association property="getGrade" javaType="grade">
       <!-- <id property="gradeId" javaType="Integer" column="gradeId"></id>
       <result property="gradeName" javaType="String" column="gradeName"></result>-->
        </association>
    </resultMap>
<!--这里使用了自动匹配-->

 

<select id="getAllStudent" resultMap="studentMap">
         SELECT * FROM student,grade WHERE student.stuGrade=grade.gradeId
    </select>

 

测试类里直接调用即可

六:前面说到association仅处理一对一的管理关系

    如果要处理一对多的关系,则需要使用collection,它与 association元素差不多,但它映射的属性是一个集合列表,即javaBean内部嵌套一个复杂数据类型属性。

javaBean

  private int gradeId;

    private String gradeName;

    private List<student> gatStudent;

 

 

 <resultMap id="gradeMap" type="grade">
        <!--<id property="gradeId" column="gradeId"></id>
        <result property="gradeName" column="gradeName"></result>-->
        <collection property="gatStudent" ofType="stu">
             <!-- <id property="stuId" column="stuId"></id>
              <result property="stuName" column="stuName"></result>-->
        </collection>
    </resultMap>

 


 <!--查询对应年级的student-->
    <select id="getAll" resultMap="gradeMap">
       select * from student,grade where stuGrade = gradeId and gradeId=1
    </select>

 

 

完:

 

posted on 2017-07-03 17:29  啷个哩个啷  阅读(2338)  评论(0编辑  收藏  举报