SQL映射文件

SQL映射文件的几个顶级元素的配置

mapper:映射文件的根节点

cache:配置给定命名空间的缓存

cache-ref:从其他命名空间引用缓存配置

resultMap:用来描述数据库结果集和对象的对应关系

sql:可以重用的SQL块,也可以被其他语句引用

insert:映射插入语句

update:映射更新语句

delete:映射删除语句

select:映射查询语句

<?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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.wdksuper.dao.userDao">
    <!--自动映射-->
    <resultMap id="userselect" type="smbms_user">
        <id property="id" column="id"/>
        <result property="phones" column="phone"/>
    </resultMap>

    <!--id代表当前命名空间下(接口下)的唯一方法名   resultType代表返回值类型-->
    <select id="selectpwd" resultType="smbms_user">
       select * from smbms_user where userCode=#{userCode}
    </select>
    <!--查询所有手动映射-->
    <select id="select" resultMap="userselect">
       select * from smbms_user
    </select>
    <!--多条件模糊查询-->
    <select id="selectqwe" resultType="smbms_user">
       select * from smbms_user where userName like concat('%',#{userName},'%') and address like concat('%',#{address},'%')
    </select>
<update id="updatepwd">
        UPDATE smbms_user set userPassword=#{userPassword} where id=#{id}
    </update>
</mapper>

select(模糊查询语句)

  <select id="selectstu" resuleType="Student" paramterType="String">

    select * from Student where stuName like CONCAT ('%',#{stuName},'%')

  </select>

这是一个id为selectstu的映射查询语句,返回值类型Student,参数类型String

多条件查询

  Map<String,String> stuMap=new HashMap<String,String>();

  stuMap.put("stuname","赵");

  stuMap.put("stuPole","3");

  DAO层

  public List<Student> stuMaplist(Map(String,String) stumap);

  stuMapper.xml

  <select id="selectstu" resultType="Studnet" paramterType="Map">

    select * from Student where stuName like CONCAT ('%',#{stuName},'%')

    and stuRole=#{stuRole}

  </select>

resuleMap完成查询结果展示

  <resultMap type="Studnet" id="stuList">

    <resule property="id" colume="id"/>

    <resule property="stuName" colume="stuName"/>

  </resuleMap>

  id:唯一标识

  type:表示resuleMap映射节国类型

  result:数据库查询的字段名

  property:查询出的结果赋值给实体对象指定的属性

resultType与resultMap

  resultType:直接返回值类型,包括基本数据类型和复杂数据类型

  resultMap:对外部resultMap定义的引用,对应外部的resultMap的id,表示返回结果映射到哪一个resultMap上

原文地址:https://www.cnblogs.com/whtt/p/11633780.html