首页
关于
友情链接
Search
1
Mybatis-动态SQL
93 阅读
2
Mybatis-XML映射文件
80 阅读
3
springboot配置文件yml
80 阅读
4
Mybatis-基础操作
79 阅读
5
拦截器(Interceptor)
74 阅读
学习记录
MySql
Java
vue
Mybatis
默认分类
代码记录
项目搭建教程
go项目
源码分享
经验分享
登录
Search
标签搜索
java
mysql
Mybatis
spring boot
编程
lombok
go
MinDoc
JAVAweb
Typecho
累计撰写
16
篇文章
累计收到
1
条评论
首页
栏目
学习记录
MySql
Java
vue
Mybatis
默认分类
代码记录
项目搭建教程
go项目
源码分享
经验分享
页面
关于
友情链接
搜索到
3
篇与
的结果
2023-05-18
Mybatis-动态SQL
随着用户的输入或外部条件的变化而变化的SQL语句,我们称为 动态SQL。举例<mapper namespace="com.itheima.mapper.EmpMapper"> <sql id="commonSelect"> select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp </sql> <!-- 动态更新员工--> <update id="update2"> update emp <set> <if test="username != null">username = #{username},</if> <if test="name != null">name = #{name},</if> <if test="gender != null">gender = #{gender},</if> <if test="image != null">image = #{image},</if> <if test="job != null">job = #{job},</if> <if test="entrydate != null">entrydate = #{entrydate},</if> <if test="deptId != null">dept_id = #{deptId},</if> <if test="updateTime != null">update_time = #{updateTime}</if> </set> where id = #{id} </update> <!--resultType: 单条记录封装的类型--> <select id="list" resultType="com.itheima.pojo.Emp"> <include refid="commonSelect"/> <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> </where> order by update_time desc </select> <!--批量删除员工 (18,19,20)--> <!-- collection: 遍历的集合 item: 遍历出来的元素 separator: 分隔符 open: 遍历开始前拼接的SQL片段 close: 遍历结束后拼接的SQL片段 --> <delete id="deleteByIds"> delete from emp where id in <foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete> </mapper>代码解读if标签用于判断条件是否成立,如果条件为true,则拼接SQL。test属性为判断条件形式:… where标签 where 元素只会在子元素有内容的情况下才插入where子句,而且会自动去除子句的开头的AND 或OR set标签 动态地在行首插入 SET 关键字,并会删掉额外的逗号。(用在update语句中)foreach标签sql代码 delete from emp where id in (17,18,19);collection: 遍历的集合 item: 遍历出来的元素 separator: 分隔符open: 遍历开始前拼接的SQL片段 close: 遍历结束后拼接的SQL片段 SQL标签 实现sql片段的复用 属性id 可以理解为该片段的名字 include标签 引用SQL片段 属性refid的值为SQL标签的id
2023年05月18日
93 阅读
0 评论
0 点赞
2023-05-18
Mybatis-XML映射文件
规范1.XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名)。 2.XML映射文件的namespace属性为Mapper接口全限定名一致。 3.XML映射文件中sql语句的id与Mapper 接口中的方法名一致,并保持返回类型一致。@Mapper public interface EmpMapper { public List<Emp> List (String name, Short gender , LocalDate begin , LocalDate end); }<?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"> <mapper namespace="com.itheima.mapper.EmpMapper"> <select id="List" resultType="com.itheima.pojo.Emp"> select * from emp where name like concat('%',#{name},'%') </select> </mapper>什么情况下使用官方解释https://mybatis.net.cn/getting-started.html 1.使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。 2.选择何种方式来配置映射,以及认为是否应该要统一映射语句定义的形式,完全取决于你和你的团队。 换句话说,永远不要拘泥于一种方式,你可以很轻松的在基于注解和 XML 的语句映射方式间自由移植和切换。
2023年05月18日
80 阅读
0 评论
0 点赞
2023-05-08
Mybatis-基础操作
删除数据当有返回值时 返回值为影响的行数@Mapper public interface EmpMapper { @Delete("delete from emp where id =#{id}") public void Delete(Integer id); }@SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void testDelete(){ empMapper.Delete(17); } }新增数据@Mapper public interface EmpMapper { @Insert("insert into emp (" + "username, name, gender, image, job, entrydate, dept_id, create_time, update_time) value (" + "#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})") public void insert(Emp emp); }@SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void testInsert(){ Emp emp=new Emp(); emp.setUsername("yujian"); emp.setName("遇见"); emp.setPassword("123456"); emp.setImage(""); emp.setGender((short) 1); emp.setJob((short) 1); emp.setEntrydate(LocalDate.of(2023,5,8)); emp.setCreateTime(LocalDateTime.now()); emp.setUpdateTime(LocalDateTime.now()); emp.setDeptId(1); empMapper.insert(emp); } }新增数据(主键返回)@Mapper public interface EmpMapper { @Options(useGeneratedKeys = true,keyProperty = "id") //会自动将生成的主键值赋值给emp对象的id属性 @Insert("insert into emp (" + "username, name, gender, image, job, entrydate, dept_id, create_time, update_time) value (" + "#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})") public void insert(Emp emp); }更新数据@Mapper public interface EmpMapper { @Update("update emp set username=#{username},name=#{name} where id=#{id}") public void update(Emp emp); }@SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void testUpdate(){ Emp emp=new Emp(); emp.setId(18); emp.setUsername("id18"); emp.setName("遇见"); empMapper.update(emp); } }查询(根据ID查询)@Mapper public interface EmpMapper { @Select("select * from emp where id=#{id}") public Emp getById(Integer id); }@SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void testGetById(){ Emp emp=empMapper.getById(18); System.out.println(emp); } }有三个字段为空 原因查看下面的数据封装Emp(id=18, username=id18, password=123456, name=遇见, gender=1, image=, job=1, entrydate=2023-05-08, deptId=null, createTime=null, updateTime=null)数据封装实体类属性名 和 数据库表查询返回的字段名一致,mybatis会自动封装。如果实体类属性名 和 数据库表查询返回的字段名不一致,不能自动封装。起别名给字段起别名 使别名与实体类的属性名一致@Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id} ") public Emp getById(Integer id); 手动映射结果 使用 注解@Results @Result映射封装 @Results({ //column 数据库字段名 //property实体类属性名 @Result(column = "",property = ""), @Result(column = "",property = "") }) @Select("select * from emp where id=#{id}") public Emp getById(Integer id);开启驼峰命名如果字段名与属性名符合驼峰命名规则,mybatis会自动通过驼峰命名规则映射。开启驼峰命名自动映射,即从数据库字段名 a_column 映射到Java 属性名 aColumn。mybatis.configuration.map-underscore-to-camel-case=trueSQL代码create table emp ( id int unsigned primary key auto_increment comment 'ID', username varchar(20) not null unique comment '用户名', password varchar(32) default '123456' comment '密码', name varchar(10) not null comment '姓名', gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女', image varchar(300) comment '图像', job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师', entrydate date comment '入职时间', dept_id int unsigned comment '部门ID', create_time datetime not null comment '创建时间', update_time datetime not null comment '修改时间' ) comment '员工表';Emp类@Data @NoArgsConstructor @AllArgsConstructor public class Emp { private Integer id; private String username; private String password; private String name; private Short gender; private String image; private Short job; private LocalDate entrydate; private Integer deptId; private LocalDateTime createTime; private LocalDateTime updateTime; }查询(条件查询)@Mapper public interface EmpMapper { @Select("select * from emp where name like'%${name}%'") public List<Emp> List(String name); }@SpringBootTest class SpringbootMybatisCrudApplicationTests { @Autowired private EmpMapper empMapper; @Test public void list(){ System.out.println(empMapper.List("遇")); } }SQL语句中 如果使用%#{name}% 会预编译成 %?% 从而执行失败 所以用$替代 ¥符是字符拼接符号 他不会生成预编译SQL 但是 没有进行预编译 会有两个问题(性能低 不安全) 所以 可以用到MySQL的concat函数(字符拼接函数)实现 查询的条件 可以理解成 两个%与关键字组成(%关键字%) 因此 查询语句 可以替换成 select * from emp where name like concat('%',#{name},'%') 配置输出日志mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImplid=? ?表示参数占位符 Parameters:17 参数17 updates:0表示影响的记录数为0 预编译SQL优势1. 性能更高 执行SQL语句时都会执行以下4个流程 如果进行预编译 会将已经执行过的SQL语句模板放入缓存中 下次执行时直接从缓存中获取模板语句 2. 更安全(防SQL注入) SQL注入解释参数占位符说明SQL语句中 参数使用 #{} 花括号内填写参数名,只有一个参数时 参数名可以随意填写
2023年05月08日
79 阅读
0 评论
0 点赞