做哪种网站能赚到钱,优秀的商城网站首页设计,网站建设平台杭州,学生如何做网站目录
一、删除
#xff08;1#xff09;在mapper接口执行sql删除语句
① 注解后sql语句没有提示怎么办#xff1f;
#xff08;2#xff09;测试层
#xff08;3#xff09;开启mybatis日志
#xff08;4#xff09;预编译SQL
二、新增
#xff08;1#…
目录
一、删除
1在mapper接口执行sql删除语句
① 注解后sql语句没有提示怎么办
2测试层
3开启mybatis日志
4预编译SQL
二、新增
1新增信息
2主键返回
三、更新
四、查询
1简单查询
当字段名与属性名不一致时mybatis不封装
① 解决办法1 ② 解决办法2
2条件查询
五、定义XML映射文件
1在resource文件下创建【与mapper接口所在包名一致】的目录文件
2在该目录下新建file文件
3在xml文件中搭建基础结构
① 获取接口全类名方法
4配置sql语句
① 定义方法名后如何快速在xml文件中生成对应标签
六、动态SQL
1if
2foreach
3sqlinclude 一、删除
1在mapper接口执行sql删除语句
① 注解后sql语句没有提示怎么办 Mapper
public interface EmpMapper {//根据id删除数据Delete(delete from emp where id #{id})public void delete(Integer id);
}
2测试层
SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textDelete() {empmapper.delete(17);}
}
3开启mybatis日志
在application.properties配置日志
mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl
4预编译SQL 更高效采用?占位符java发送语句的同时发送参数后续因为缓存已经有编译好的sql语句直接可以执行 更安全防止SQL注入SQL注入是通过操作输入的数据来修改事先定义好的SQL语句以达到执行代码对服务器进行攻击的方法 二、新增
1新增信息
注意#{}内采用【驼峰命名法】即_用大写字母替代egdept_id → deptId
Mapper
public interface EmpMapper {//新增员工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}))public void insert(Emp emp);
}
SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textInsert() {Emp emp new Emp();emp.setName(tom);emp.setUsername(TOM);emp.setImage(1.jpg);emp.setGender((short)1);emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDateTime.now());emp.setUpdateTime(LocalDateTime.now());emp.setId(1);empmapper.insert(emp);}2主键返回
在数据添加成功后需要获取插入数据的主键。eg添加套餐数据时需要返回套餐id来维护套餐-菜品关系 会将自动生成的主键值赋值给emp对象的id属性 Options(keyProperty id,useGeneratedKeys true) //新增员工Options(keyProperty id,useGeneratedKeys true)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}))public void insert(Emp emp);三、更新
根据id更新员工信息
Mapper
public interface EmpMapper {//更新员工Update(update emp set username #{username} ,name #{name},gender #{gender},image #{image},job #{job},entrydate #{entrydate},dept_id #{deptId},update_time #{updateTime} where id #{id})public void update(Emp emp);
}
SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textInsert() {Emp emp new Emp();emp.setName(kakak);emp.setUsername(88kakk);emp.setImage(1.jpg);emp.setGender((short)1);emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000,1,1));emp.setUpdateTime(LocalDateTime.now());emp.setId(18);emp.setDeptId(1);empmapper.update(emp);}
} 四、查询
1简单查询
Mapper
public interface EmpMapper {//根据id查询员工Select(select * from emp where id #{id})public Emp getById(Integer id);
}
SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textInsert() {Emp emp empmapper.getById(19);System.out.println(emp);}
}
当字段名与属性名不一致时mybatis不封装 ① 解决办法1
手动注解
Mapper
public interface EmpMapper {//根据id查询员工Results({Result(column dept_id,property deptId),Result(column crea_time,property createTime),Result(column update_time,property updateTime)})Select(select * from emp where id #{id})public Emp getById(Integer id);
} ② 解决办法2
在application.properties开启驼峰命名法开关
# 开启驼峰命名法自动映射开关
mybatis.configuration.map-underscore-to-camel-casetrue
2条件查询 #{}编译后会被?替代但?不能出现在‘’内因此我们不能使用#{}而要使用拼接${}但是${}有sql注入风险因此我们使用concat()字符串拼接函数 Mapper
public interface EmpMapper {//根据id查询员工Select(select * from emp where name like %${name}% and gender #{gender} and entrydate between #{begin} and #{end})public ListEmp list(String name, Short gender, LocalDate begin,LocalDate end);
}
Mapper
public interface EmpMapper {//根据id查询员工Select(select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end})public ListEmp list(String name, Short gender, LocalDate begin,LocalDate end);
} SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textList() {ListEmp empList empmapper.list(张,(short)1,LocalDate.of(2010,1,1),LocalDate.of(2020,1,1));System.out.println(empList);}
} 五、定义XML映射文件 注解开发简单的sqlxml开发动态sql 1在resource文件下创建【与mapper接口所在包名一致】的目录文件 2在该目录下新建file文件
文件名与mapper接口名一致
3在xml文件中搭建基础结构
namespace属性和mapper接口全类名一致
MyBatis中文网
① 获取接口全类名方法 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.itheima.mapper.EmpMapper/mapper
4配置sql语句
id与mapper接口中方法名一致保持返回类型一致
resultType和类名一致 ① 定义方法名后如何快速在xml文件中生成对应标签 把光标置于方法名上按alt回车回车直接跳转到xml文件并生成 六、动态SQL
随着用户的输入或外部条件变化而变化的SQL语句称为动态SQL
比方说查询框有姓名性别入职时间
用户如果不填某空缺少某参数用之前注解sql语句肯定会报错而动态sql就是解决这一问题的 1if
如果test属性成立则拼接SQL
mapper namespacecom.itheima.mapper.EmpMapper!-- resultType:单条记录所封装的类型--select idlist resultTypecom.itheima.pojo.Empselect *from empwhereif testname ! nullname like concat(%, #{name}, %)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/if/whereorder by update_time desc/select/mapper 2foreach
批量删除 collection遍历的集合item遍历出来的元素separator分隔符open遍历开始前拼接的SQL片段close遍历结束后拼接的SQL片段 mapper namespacecom.itheima.mapper.EmpMapper
!-- 批量删除员工(18,19,20)--delete iddeleteByIdsdelete from emp where id inforeach collectionids itemx separator, open( close)#{x}/foreach/delete
/mapper
SpringBootTest
class MybatisCrudApplicationTests {Autowiredprivate EmpMapper empmapper;Testpublic void textList() {ListInteger ids Arrays.asList(13,14,15);empmapper.deleteByIds(ids);}
}
3sqlinclude sql标签可以择出重复使用的语句并用include在所需要的地方引/ mapper namespacecom.itheima.mapper.EmpMappersql idcommonSelectselect id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_timefrom emp/sql!-- resultType:单条记录所封装的类型--select idlist resultTypecom.itheima.pojo.Empinclude refidcommonSelect/whereif testname ! nullname like concat(%, #{name}, %)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/if/whereorder by update_time desc/select/mapper