当前位置: 首页 > news >正文

网站代理商百度极速版下载

网站代理商,百度极速版下载,网页广告设计收费,新手如何建立自己网站目录 Mybatis动态SQL介绍​编辑 一、案例 ①Mapper层 ②测试类 ③EmpMapper.xml ④结果​ 二、标签 (一)if where标签 ​①EmpMapper.xml ②案例 ③总结 (二)foreach标签 ①SQL语句 ②Mapper层 ③EmpMapper.xml ④…

目录

Mybatis动态SQL介绍​编辑

一、案例

①Mapper层

②测试类

③EmpMapper.xml

④结果​

二、标签

(一)if where标签

​①EmpMapper.xml

②案例

③总结

(二)foreach标签

①SQL语句

②Mapper层

③EmpMapper.xml

④测试类

⑤结果

(三)sql&include标签

①EmpMapper.xml

②总结

 XML映射文件(配置文件)

①EmpMapper.xml

②Mapper层

③测试类

④思考

⑤总结


Mybatis动态SQL介绍

一、案例

ctrl+alt+l将SQL语句格式化

        List<Emp> empList= empMapper.list("z",null,null,null);

当查询条件不完整时,会查询不到数据,因此就需要编写动态SQL

①Mapper层

package com.itheima.mapper;import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);}

②测试类

package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testList(){List<Emp> empList= empMapper.list("z",null,null,null);System.out.println(empList);}
}

③EmpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--    resultType:单条记录所封装的类型-->
<!--    <select id="list" resultType="com.itheima.pojo.Emp">-->
<!--        select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and-->
<!--        entrydate between #{begin} and #{end} order by update_time desc-->
<!--    </select>--><select id="list" resultType="com.itheima.pojo.Emp">select *from empwhere<if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if>order by update_time desc</select>
</mapper>

④结果

二、标签

(一)if where标签

①EmpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--    resultType:单条记录所封装的类型-->
<!--    <select id="list" resultType="com.itheima.pojo.Emp">-->
<!--        select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and-->
<!--        entrydate between #{begin} and #{end} order by update_time desc-->
<!--    </select>--><select id="list" resultType="com.itheima.pojo.Emp">select *from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if>order by update_time desc</where></select>
</mapper>

②案例

③总结

(二)foreach标签

批量删除员工信息

①SQL语句

delete from emp where id in(18,19,20);



②Mapper层

EmpMapper.java

package com.itheima.mapper;import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;
import java.util.List;@Mapper
public interface EmpMapper {//根据ID批量删除员工信息public void deleteByIds(List<Integer> ids);}

③EmpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper"><!--批量删除员工--><delete id="deleteByIds">delete from empwhere id in<foreach collection="ids" item="id" open="(" close=")" separator=",">#{id}</foreach></delete>
</mapper>

④测试类

package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testDeleteByIds(){List<Integer> ids= Arrays.asList(13,14,15);empMapper.deleteByIds(ids);}
}

⑤结果

(三)sql&include标签

查询的时候不建议使用select *,而是把所有的字段罗列出来

①EmpMapper.xml

②总结

 XML映射文件(配置文件)

源文件放在java中,而配置文件放在resources中

官网:mybatis – MyBatis 3 | 简介

①EmpMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.EmpMapper">
<!--    resultType:单条记录所封装的类型--><select id="list" resultType="com.itheima.pojo.Emp">select * from emp where name like concat('%',#{name},'%') and gender=#{gender} andentrydate between #{begin} and #{end} order by update_time desc</select>
</mapper>

②Mapper层

package com.itheima.mapper;import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);}

③测试类

package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testList(){List<Emp> empList= empMapper.list("z",(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));System.out.println(empList);}
}

④思考


mapper映射文件还有一个好处,修改sql语句不用重启项目

在方法上实现动态的条件查询就会使接口过于臃肿

如果操作语句多了,直接也在注解上面比较混乱

如果要做手动映射封装实体类的时候 xml方便,项目中会常用

用xml,因为查询的条件会变化,直接写在注解里面的话会使接口过于臃肿

这两个各自找各自对应的,原来是注解绑定,现在是通过路径和方法名绑定

多条件查询要写动态sql用映射文件比较合适,简单的可以直接注解方式

终于找到问题了,xml里的sql语句不能拼接,只能是一长条,运行才不报错

执行list()方法时,根据全限定类名找到对应的namespace ,再找到id为这个方法的SQL语句就可以执行了


⑤总结

http://www.dt0577.cn/news/7807.html

相关文章:

  • 甘肃做网站找谁sem和seo的关系
  • 广州品牌网站建设公司关键词林俊杰mp3免费下载
  • 网站收录说明淘宝seo搜索优化工具
  • 网站如何开发触屏版推广下载
  • 官方网站数据如何做脚注网站设计公司有哪些
  • 北京中天人建设工程有限公司网站百度seo排名规则
  • 怎么样才能创建自己的网站沧州网站优化公司
  • 网站建设模板一次收费百度推广官网入口
  • 域名解析到别的网站长沙优化网站推广
  • 网站建设教程(项目式)优化设计单元测试卷
  • 从seo角度谈网站建设西安优化外包
  • 这么给网站做关键字分类达人的作用
  • 免费建站平台0网络推广的方式和途径有哪些
  • seo关键词优化如何沈阳seo排名外包
  • 大连建设执业资格注册中心网站广东企业网站seo哪里好
  • 到那个网站做翻译接单高质量外链购买
  • 免费在线观看高清影片哈尔滨怎样关键词优化
  • 做网站用的书对百度竞价排名的看法
  • 自己做商务网站有什么利弊六年级上册数学优化设计答案
  • 17网站一起做网店潮汕依依北京网站seo招聘
  • 如何建设社区网站首页在百度上怎么卖自己的产品
  • 上海专业做网站公司武汉网站建设推广公司
  • 沈阳做网站哪家最便宜微信小程序开发文档
  • 祥云平台做的网站效果好网络搭建教程
  • 深圳市网站建设公司百度seo网站优化
  • 页面设计属于什么专业seo网络排名优化
  • 用node和vue做的网站百度推广的四种收费形式
  • 基于dijango的网站开发优化方案
  • 动态网站开发 教材手游推广渠道和推广方式
  • 遨翔网站建设今日头条官网首页