网站建设服务合同需要哪些资料广告网站模板
目录
一、准备工作
二、常用配置
三、尝试
四、增删改查
1、增加
2、删除
3、修改
4、查询
五、XML的映射方法
一、准备工作
实施前的准备工作:
-  
准备数据库表
 -  
创建一个新的springboot工程,选择引入对应的起步依赖(mybatis、mysql驱动、lombok)
 -  
application.properties中引入数据库连接信息
 -  
创建对应的实体类 Emp(实体类属性采用驼峰命名)
 -  
准备Mapper接口 EmpMapper
 
SQL文件:emp的sql文件

二、常用配置
#指定mybatis输出日志的位置, 输出控制台 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl# 查询的时候mybatis驼峰命名法 mybatis.configuration.map-underscore-to-camel-case=true
三、尝试
在Mybatis中提供的参数占位符有两种:${...} 、#{...}
-  
#{...}
-  
执行SQL时,会将#{…}替换为?,生成预编译SQL,会自动设置参数值
 -  
使用时机:参数传递,都使用#{…}
 
 -  
 -  
${...}
-  
拼接SQL。直接将参数拼接在SQL语句中,存在SQL注入问题
 -  
使用时机:如果对表名、列表进行动态设置时使用
 
 -  
 
注意事项:在项目开发中,建议使用#{...},生成预编译SQL,防止SQL注入安全。
四、增删改查
1、增加
 // 新增@Options(useGeneratedKeys = true,keyProperty = "id")  // 返回主键@Insert("insert into emp(username, name, gender, image, job, entrydate, " +"dept_id, create_time, update_time) " +"values (#{userName}, #{name}, #{gender}, #{image}," +" #{job}, #{entryDate}, #{deptId}, #{createTime}, #{updateTime})")int insert(Emp emp); 
测试
 // 新增@Testpublic void empAdd(){//创建员工对象Emp emp = new Emp();emp.setUserName("小明");emp.setName("小将");emp.setImage("sadasdasd.jpg");emp.setGender((short)1);emp.setJob(1);emp.setEntryDate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDate.now());emp.setUpdateTime(LocalDate.now());emp.setDeptId(1);empMapper.insert(emp);} 
2、删除
  // 删除@Delete("delete from emp where id = #{id}")int delete(int id);
 
test
 // 删除测试@Testpublic void empDelete() {var s =  empMapper.delete(17);System.out.printf("删除:%s\n",s);} 
3、修改
  // 修改@Update("update emp set username = #{userName}, name = #{name}, gender = 3 where id = 18;")void update(Emp emp); 
Test
  // 修改@Testpublic  void update(){Emp emp = new Emp();emp.setName("大卫");emp.setUserName("daadasd");emp.setGender(2);empMapper.update(emp);} 
4、查询
  // 查询@Select("select * from emp " +"where name like concat('%',#{name},'%') " +"and gender = #{gender} " +"and entrydate between #{begin} and #{end} " +"order by update_time desc")List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end); 
Test
 // 查询@Testpublic void search(){List<Emp> emp =   empMapper.list("汤姆", (short) 1,LocalDate.of(2000,8,15),LocalDate.of(2023,8,5));System.out.println(emp);} 
五、XML的映射方法
-  
<sql>:定义可重用的SQL片段 -  
<include>:通过属性refid,指定包含的SQL片段 -  
<if>-  
用于判断条件是否成立,如果条件为true,则拼接SQL
 -  
形式:
<if test="name != null"> … </if>
 
 -  
 -  
<where>-  
where元素只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR
 
 -  
 -  
<set>-  
动态地在行首插入 SET 关键字,并会删掉额外的逗号。(用在update语句中)
 
 -  
 -  
<foreach>遍历deleteByIds方法中传递的参数ids集合<foreach collection="集合名称" item="集合遍历出来的元素/项" separator="每一次遍历使用的分隔符" open="遍历开始前拼接的片段" close="遍历结束后拼接的片段"> </foreach> 
<?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.demo.crud.mapper.EmpMapper"><!--提取重复代码 --><sql id="commonSelect">select * from emp </sql><!--  查询-->
<!--   resultType单条记录封装的类型 --><select id="list" resultType="com.demo.crud.pojo.Emp"><include refid="commonSelect"/><where><if test="name != null">name like concat('%',#{name},'%')</if>order by update_time desc</where></select><!--删除操作--><delete id="deleteByIds">delete from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete></mapper> 
