基本查询,插入,更新,删除,多表查询,mybatis缓存

基本查询

查询数据(单表查询 多表连接 组合查询 分页查询 有参数 没参数 多个参数 返回值是一条 返回值是多条)
    <select id="getInfoById"  parameterType="int"  resultType="UserInfo"   >
        select  *  from userinfo  where id=#{id}
    </select>
  • resultType:
    • 如果查询返回的是一条数据,则直接写返回值类型
    • 如果查询返回的是多条数据,则写集合中的泛型
    <select id="getAll"  resultType="UserInfo"  >
        select *  from  userinfo
    </select>
返回值
  1. 如果没有参数,则省略parameterType属性
  2. 如果有一个参数,是基本数据类型,则SQL语句中获取参数时,用什么名字都能获取到
    但是基于编码规范,建议使用接口中形参的名字
  3. 如果有多个参数,解决方案:
  • 省略parameterType属性,SQL语句中通过参数的索引值获取参数,索引值从0开始 (不建议使用)

  • 使用实体类的类型传入,SQL语句中获取参数时,使用的是实体类中的属性名

  • 使用map类型传入,SQL语句中获取参数时,使用的是map中元素的key

UserInfoMapper.java类
public UserInfo getUsers(Map<String, String>  map);
UserInfoMapper.xml文件
<select id="getUsers"  resultType="UserInfo" >
        select *  from userinfo where username= #{0} and password=#{1}
    </select> 
---------------------------------------------------------------------------------------------------------------
<select id="getUsers" parameterType="UserInfo"   resultType="UserInfo" >
        select *  from userinfo where username=#{username} and password=#{password}
    </select>
    <select id="getUsers" parameterType="map"   resultType="UserInfo" >
        select *  from userinfo where username=#{k_username} and password=#{k_password}
    </select>
Test类
        UserInfo user = new UserInfo();
        user.setUsername("111");
        user.setPassword("111");
        UserInfo u = mapper.getUsers(user);
        System.out.println(u);
---------------------------------------------------------------------------------------------------------------     
        Map<String, String> map = new HashMap<>();
        map.put("k_username", "111");
        map.put("k_password", "111");
        UserInfo u = mapper.getUsers(map);
        System.out.println(u);

组合查询

  • where if
    • 字符串类型判空:username!=null and username!=''
    • int类型判空:id!=0
    • Date类型:date!=null
<select id="getUser" parameterType="UserInfo" resultType="UserInfo"  >
        select *  from  userinfo 
        <where>
            <if test="username!=null and username!='' ">
                and username  like #{username}
            </if>
            <if test="email!=null and email!='' ">
                and email=#{email}
            </if>
        </where>
    </select>

分页查询

    <select id="getPageUser" parameterType="map"    resultType="UserInfo">
        select * from userInfo limit #{k_index},#{k_pagesize}
    </select>
查询入职日期在指定日期之前 &lt;
    <select id="getEmpByHiredate" parameterType="Date" resultType="EmpInfo">
        select * from emp where hiredate &lt; #{hiredate}

    </select>
Test部分(日期转换)
        String strdate="1985-01-01";
        Date utilDate=null;

        try {
            utilDate = new SimpleDateFormat("yyyy-MM-dd").parse(strdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        java.sql.Date   hiredate = new java.sql.Date(utilDate.getTime());
        List<EmpInfo> list1 = mapper.getEmpByHiredate(hiredate);
        for(EmpInfo e:list1){
            System.out.println(e);
        }

插入数据

存储数据
<insert id="insertEmp" parameterType="EmpInfo"    >
        insert into emp(ename,job,mgr,hiredate,sal,comm,deptno)  
        values (#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
    </insert>
存储数据:主键采用最大值+1的方式生成
selectKey标签:
  • keyProperty 属性:查询到的数据赋值给Emp中的哪个属性
  • resultType 属性:查询的返回值类型
  • order 属性:设置查询是在insert之前执行还是之后执行
<insert id="insertEmp" parameterType="EmpInfo" >

        <selectKey keyProperty="empno" resultType="int"  order="BEFORE" >
            select max(empno)+1 from emp
        </selectKey>
        
        insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)  
        values (#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
    </insert>

更新数据

    <update id="updateEmp" parameterType="EmpInfo">
        update emp
        set sal =#{sal},comm=#{comm}
        where empno=#{empno}
    </update>
Test部分 自动提交:session.commit();
    //插入数据
        EmpInfo e= new EmpInfo();
        e.setEname("test012151");
        e.setJob("salesman");
        e.setMgr(7369);
        e.setHiredate(new java.sql.Date(new Date().getTime()));
        e.setSal(2560);
        e.setComm(5693);
        e.setDeptno(10);
        mapper.insertEmp(e);
        session.commit();
        System.out.println("新生成的主键值"+e.getEmpno());*/
        
        //修改数据
        EmpInfo e = new EmpInfo();
        e.setEmpno(7937);
        e.setSal(10000);
        e.setComm(8000);
        mapper.updateEmp(e);
        session.commit();
        

删除数据

根据一个元素删除数据

批量删除

foreach标签:实现循环遍历
  • collection 属性:要遍历的集合类型
  • item 属性:每次遍历得到的元素的名称(随意定义)
  • open 属性:遍历的结果以什么字符开头
  • close 属性:遍历的结果以什么字符结尾
  • separator 属性:遍历得到的元素以什么字符作为分隔符
xml文件
    <delete id="deleteEmps"   >
        delete from  emp  where empno in 

        <foreach collection="array" item="item" open="("  close=")"  separator=","   >
            #{item}
        </foreach>
    </delete>

组合查询

在Bean包中:存在两个实体类(1:有外键 关联2 ;2:无外键)
1:定义2的成员变量
2:定义1的List<1>的成员变量

一对一查询 emp==>dept

association标签:一对一查询 嵌套结果集处理
  • property 属性:嵌套的 Emp 的resultMap封装的结果要赋值给当前实体类Emp的那个属性
  • column 属性:外键列
  • javaType 属性:嵌套的 Emp resultMap的返回值类型
  • resultMap 属性:对应的(Dept) 嵌套的resultMap
xml文件
    <!-- 一对一查询   -->
    <select id="getEmpInfo" parameterType="int" resultMap="eresult"  >
        select e.*, d.deptno ddeptno,d.dname,d.loc
        from emp e,dept d 
        where empno=#{empno} and e.deptno=d.deptno
    </select>
    <resultMap type="Emp" id="eresult">
        <result property="empno" column="empno"   />
        <result property="ename" column="ename"   />
        <result property="job" column="job"   />
        <result property="mgr" column="mgr"   />
        <result property="sal" column="sal"   />
        <result property="hiredate" column="hiredate"   />
        <result property="comm" column="comm"   />
        <result property="deptno" column="deptno"   />

        <association property="dept"  column="deptno"  javaType="Dept"  resultMap="dresult"  />

    </resultMap>

    <resultMap type="Dept" id="dresult">
        <result property="dname" column="dname"   />
        <result property="loc" column="loc"   />
        <result property="deptno" column="ddeptno"   />
    </resultMap>

一对多查询 dept==>emp

collection标签:一对多查询
  • property 属性:构建的集合对象赋值给最终返回值类型中哪个属性
  • ofType 属性:集合中元素的类型
xml文件
    <select id="getDept" parameterType="int"  resultMap="dresult"  >
        select e.*,d.deptno ddeptno,d.dname,d.loc 
        from emp e,dept d 
        where d.deptno=#{deptno} and e.deptno=d.deptno
    </select>
    
    <resultMap type="Dept" id="dresult">
        <result property="deptno"  column="ddeptno"   />
        <result property="dname"  column="dname"   />
        <result property="loc"  column="loc"   />

        <collection property="emps"  ofType="Emp">
        
            <result property="empno" column="empno"   />
            <result property="ename" column="ename"   />
            <result property="job" column="job"   />
            <result property="mgr" column="mgr"   />
            <result property="sal" column="sal"   />
            <result property="hiredate" column="hiredate"   />
            <result property="comm" column="comm"   />
            <result property="deptno" column="deptno"   />
        </collection>
        
    </resultMap>

多对多查询 order==>product==>customer

association标签:一对一查询 嵌套结果集处理
  • property 属性:嵌套的 Emp 的resultMap封装的结果要赋值给当前实体类Emp的那个属性
  • column 属性:外键列
  • javaType 属性:嵌套的 Emp resultMap的返回值类型
  • resultMap 属性:对应的(Dept) 嵌套的resultMap
xml文件
    <select id="getAll"  resultMap="oresult"   >
        select o.*,c.cid ccid,c.cname,c.tel,c.address,p.pid ppid,p.pname,p.price pprice
        from orders o,customer c,product p
        where o.cid=c.cid and o.pid=p.pid
    </select>
    <resultMap type="Order" id="oresult">
    
        <result property="oid"  column="oid"  />
        <result property="pid"  column="pid"  />
        <result property="cid"  column="cid"  />
        <result property="count"  column="count"  />
        <result property="price"  column="price"  />
        
        <association property="p" column="pid" javaType="Product" resultMap="presult" />
        <association property="c" column="cid" javaType="Customer" resultMap="cresult"  />
    </resultMap>
    
    <resultMap type="Product" id="presult">
        <result property="pid"  column="ppid"  />
        <result property="pname"  column="pname"  />
        <result property="price"  column="pprice"  />
    </resultMap>
    
    <resultMap type="Customer" id="cresult">
        <result property="cid"  column="ccid"  />
        <result property="cname"  column="cname"  />
        <result property="tel"  column="tel"  />
        <result property="address"  column="address"  />
    </resultMap>

Sql片段

include标签:用于SQL片段中
  • refid属性:获取sql片段
//Sql片段
<sql id="sid">
select  *  from emp
</sql>

<select id="getEmpByName" parameterType="string" resultType="Emp"  >

<include refid="sid"/>
  where ename like #{ename}
<select>

mybatis缓存:

针对查询类需求,提高程序性能,减轻数据库访问压力,提供的缓存技术

一级缓存:默认开启的

  • 针对同一个SqlSession,如果基于某一个需求查询到一份数据,则mybatis会将查询结果存储在缓存中,接下来如果再一次做同样的查询,则mybatis会直接从缓存中拿数据,而不访问数据库,从而提升查询的性能
  • 手动不开启缓存:flushCache="true"
  • 如果过程中执行了该表的DML操作,则缓存数据自动消失

二级缓存:

  • 不限定SqlSession的缓存的使用
  • 不是默认开启的
  • 需要手动开启(在SqlMapConfig.xml中)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容