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

新北市网站建设_网站建设公司_网站建设_seo优化

wordpress有插件怎么用,站长工具seo综合查询外部链接数量,怎么建立一个博客网站,苏州网站建设方案外包目录 一、什么是MyBatis 二、MyBatis框架的搭建 1、搭建MyBatis框架 2、设置MyBaits项目的配置 三、使用MyBatis完成数据库的操作 1、MyBatis程序中sql语句的即时执行和预编译 1.1、即时执行#xff08;${}#xff09; 1.2、预编译#xff08;#{}#xff09; 1.3、即…目录 一、什么是MyBatis 二、MyBatis框架的搭建 1、搭建MyBatis框架 2、设置MyBaits项目的配置 三、使用MyBatis完成数据库的操作 1、MyBatis程序中sql语句的即时执行和预编译 1.1、即时执行${} 1.2、预编译#{} 1.3、即时执行和预编译的优缺点 2、传统的MyBatis程序的写法XML方式 2.1、查询表中所有的成员信息 2.2、根据某个字段来查询表中单个成员信息  2.3、根据字段删除表中信息 2.4、根据字段名修改表中的信息 2.5、添加操作 2.6、特殊的添加返回自增id 2.7、like查询 2.8、实体类中的属性和数据库表中的字段名不一致出现的问题的三种解决方式 3、使用注解的方式在MyBatis程序中构造SQL语句 3.1、多表联查一对一 3.2、多表联查一对多  4、安装MyBatis X插件 四、动态SQL的使用 1、if标签 2、trim标签 3、where标签 4、set标签 5、foreach标签  一、什么是MyBatis MyBatis是一款持久层框架它支持自定义SQL存储过程以及高级映射MyBatis可以通过简单的XML或注解来配置和映射原始类型和接口为数据库中的记录。简单来说MyBatis是更简单完成程序和数据库交互的工具也就是更简单的操作和读取数据库工具。MyBaits的底层是基于JDBC实现的。 二、MyBatis框架的搭建 MyBaits在程序和数据库之间是起到一个桥梁的作用。 1、搭建MyBatis框架 MyBatis项目是基于Spring Boot项目的所以我们只需要添加一个MyBatios框架即可。创建Spring Boot项目在之前的博客中有这里就直接展示添加MyBatis框架即可。 这里需要注意的是由于我们创建好MyBatis项目之后并没有设置MyBatis连接MySql数据库详细信息所以新创建的项目点击运行之后会报错。  2、设置MyBaits项目的配置 1️⃣设置数据库的连接地址和XML文件的保存格式和位置 #设置数据库的相关连接信息 spring.datasource.urljdbc:mysql://127.0.0.1:3306/mycnblog?characterEncodingutf8 spring.datasource.usernameroot spring.datasource.password123456 spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver #设置MyBatis xml 存放路径和命名格式 mybatis.mapper-locationsclasspath:mybatis/*Mapper.xml #配置MyBatis 执行是打印SQL可选配置 mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl #设置某个文件中日志的级别应为StdOutImpl类中的日志级别为debug而项目默认为info所以设置我们自己的类中打印的日志为debug级别 logging.level.com.example.demodebug 三、使用MyBatis完成数据库的操作 1、MyBatis程序中sql语句的即时执行和预编译 我们在JDBC中在构造sql语句的时候常常给字段的值用问号(?)代替最后在使用方法对这些问好进行赋值这是预编译。使用预编译的好处可以防止sql注入。当然还有一种sql的执行方式就是即时执行。下面我们来了解一下MyBatis程序中的即使执行和预编译的构建方式 1.1、即时执行${} 就像下面我们写道的根据某个字段查询单个信息的时候我们传递了参数在xml文件中对相应的字段进行赋值的时候使用${}这种方式就是构造sql语句即时执行的方式。 select idgetUserById resultTypecom.example.demo.model.Userinfoselect * from userinfo where id${id}/select1.2、预编译#{} 这种写法在程序执行的时候我们可以看到sql语句中id的值先是被将位置占着的。这里表示的是只能是值而不能是sql语句这就防止了sql注入。 select idgetUserById resultTypecom.example.demo.model.Userinfoselect * from userinfo where id#{id}/select 1.3、即时执行和预编译的优缺点 预编译#{} 1️⃣优点 它的执行是安全的可以防止sql注入。预编译他会将传入的值当成value来看待判断这个value是否和数据库中这个字段中的值是否相等相等就会执行成功不相等会查找不到。在使用#{}这种写法的时候如果我们传递的参数是字符串类型的我们不需要使用单引号( )将#{}括起来执行的时候他会自动给value添加单引号。 2️⃣缺点 不能传递SQL命令当传递SQL命令的时候他会给这个命令自动添加单引号( )但是给SQL命令添加单引号SQL语句就会报错。 //UserMapper.java类/** 传递排序规则* */ListUserinfo getAllByOrder(Param(myorder)String myorder);//UserMapper.xml文件select idgetAllByOrder resultTypecom.example.demo.model.Userinfoselect * from userinfo order by id #{myorder}/select//测试类 class UserMapperTest { Testvoid getAllByOrder() {ListUserinfo list userMapper.getAllByOrder(desc);System.out.println(list);} } 当时当我们将#{}换成${}再次运行就会执行成功。 select * from userinfo order by id ${myorder} 即时执行${} 1️⃣优点 当我们逛淘宝的时候筛选商品点击价格按照从低到高这个时候传递的就是SQL命令。从低到高传递的就是asc从高到低传递的就是desc。 2️⃣缺点 它的执行不安全的存在sql注入。在使用${}时如果传入的参数是字符串类型的数据还需要再构造sql的语句的时候使用单引号将传入的参数引住${}。 SQL注入 使用一些特殊的sql语句来完成一些非法的操作。 可以看到数据库中用户表的用户名和密码都是admin。  正常情况下我们使用不正确的密码是登录不成功的。 /** 用户登录的场景* */Userinfo login(Param(username)String username,Param(password)String password); } select idlogin resultTypecom.example.demo.model.Userinfoselect * from userinfo where username${username} and password${password}/select但是非法的情况下我们给password的属性填写一个sql语句就可以成功登录。  这就是最简单的SQL注入。 ✨总结 当业务需要传递SQL命令是只能使用${}不能使用#{}。如果要使用${}那么传递的参数一定要是能被穷举的否则不能使用。能使用#{}的时候就不使用${}. 2、传统的MyBatis程序的写法XML方式 2.1、查询表中所有的成员信息 这里需要我们创建两个文件 1️⃣创建一个接口来声明方法供给Service层调用这些方法。 2️⃣创建一个XML文件实现接口中的方法 我们想要在demol包中创建一个UserInfo类这个类中的属性要和数据库中表的字段名相对应。这里我们用来演示的用户表。 1️⃣在创建MyBatis项目中的类之前我们先来创建数据库中的表信息。 - 创建数据库 drop database if exists mycnblog; create database mycnblog DEFAULT CHARACTER SET utf8mb4;-- 使用数据数据 use mycnblog;-- 创建表[用户表] drop table if exists userinfo; create table userinfo(id int primary key auto_increment,username varchar(100) not null,password varchar(32) not null,photo varchar(500) default ,createtime timestamp default current_timestamp,updatetime timestamp default current_timestamp,state int default 1 ) default charset utf8mb4;-- 创建文章表 drop table if exists articleinfo; create table articleinfo(id int primary key auto_increment,title varchar(100) not null,content text not null,createtime timestamp default current_timestamp,updatetime timestamp default current_timestamp,uid int not null,rcount int not null default 1,state int default 1 )default charset utf8mb4;-- 创建视频表 drop table if exists videoinfo; create table videoinfo(vid int primary key,title varchar(250),url varchar(1000),createtime timestamp default current_timestamp,updatetime timestamp default current_timestamp,uid int )default charset utf8mb4; 2️⃣UserInfo类 package com.example.demo.model;import lombok.Data;import java.time.LocalDateTime;Data public class Userinfo {private int id;private String username;private String password;private String photo;private LocalDateTime createtime;private LocalDateTime updatetime;private int state;} 3️⃣UserMapper接口 在Mapper包当中创建一个UserMapper接口用来声明方法。这里Mapper包就相当于之前项目中创建的dao包。这里我们实现的方法是查询表中所有的数据。在MyBaits项目中数据持久层不在使用五大类注解而是使用Mapper注解。 package com.example.demo.mapper;import com.example.demo.model.Userinfo; import org.apache.ibatis.annotations.Mapper;import java.util.List;Mapper//这是数据持久层的标志 public interface UserMapper {ListUserinfo getAll();}4️⃣UserMapper.xml文件 用来实现接口中的方法这里两个文件的关联并不是使用文件名而是使用XML文件的配置。 ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.demo.mapper.UserMapperselect idgetAll resultTypecom.example.demo.model.Userinfoselect * from userinfo/select/mapper 这里在XML文件中实现查询语句的时候需要设置方法的名称和UserMapper中声明的方法名称相同在XML文件设置方法名称需要使用id属性。还需要设置返回值类型。可以使用resultType属性或者resultMap属性来设置。 5️⃣这里我们创建一个测试类来检测我们的所写的代码的执行结果 SpringBootTest//不能省略告诉当前的测试程序目前项目是运行在Spring容器中的 class UserMapperTest {Autowiredprivate UserMapper userMapper;Testvoid getAll() {ListUserinfo list userMapper.getAll();System.out.println(list);} } 上述构建查询方法的步骤在进行数据库增删改查的操作时大体步骤都是相同的只是在细节上有差异。 当然这种写法是传统的写法后序我们还会说道使用注解的方式来构造sql语句。 2.2、根据某个字段来查询表中单个成员信息  这里根据字段查询单个信息和查询表中所有成员信息不同查询单个信息需要给这个方法传递参数。 ✨注意 这里需要注意的是我们在MyBatis代码的时候创建的方法需要传递参数那么参数的数据类型不能使用基础的数据类型需要使用基础数据类型的包装类。这是因为使用包装类的时候。如果前端没有给你传递参数的时候包装类是可以接收null值的但是如果是基础数据类型那么你的后端程序直接会报500内部服务器错误。但是这里的问题好像被MyBatis进行了修改如果你传的参数是基础的数据类型那么他在执行的时候会自动发生装包的操作将基础数据类型转变为它的类类型。还需要注意的是如果MyBatis程序方法中传递的参数不是我们自己定义的类的对象需要使用Param注解将Param注解中的参数动态的传递给XML文件的代码中。这里Param注解中的参数可以和我们方法的参数相同也可以不同但是这里小编建议还是写成相同的这样可以省去很多麻烦。 import java.util.List;Mapper//这是数据持久层的标志 public interface UserMapper {/** 根据id查询一条信息* */Userinfo getUserById(Param(id)Integer id); }select idgetUserById resultTypecom.example.demo.model.Userinfoselect * from userinfo where id#{id}/select SpringBootTest//不能省略告诉当前的测试程序目前项目是运行在Spring容器中的 class UserMapperTest {Autowiredprivate UserMapper userMapper;Testvoid getUserById() {Userinfo userinfo userMapper.getUserById(1);System.out.println(userinfo.toString());}2.3、根据字段删除表中信息 删除信息默认返回的是受影响的行数所以我们在声明方法的时候设置的返回值类型为int. /** 根据id删除成员* */int delById(Param(id)Integer id); delete iddelByIddelete from userinfo where id#{id}/delete Testvoid delById() {int id 2;int result userMapper.delById(id);System.out.println(受影响的行数result);} 这里我们在测试类进行的删除操作会影响到数据库中的数据为了不让我们的我们的测试代码修改数据库中的数据我们可以在测试类的删除或者修改数据的方法上添加一个Transactional注解让程序执行完成之后实现数据回滚的操作。 2.4、根据字段名修改表中的信息 修改的实现和删除一样在xml文件中的update标签中不用设置返回值类型(resultMap或者resultType)默认的返回值是受影响的行数所以在UserMapper接口中声明方法的时候返回值类型为int。 之前的方法中我们传递的都是属性这个方法中我们传递一个自定义的类对象。 /** 根据id修改成员信息* */int update(Userinfo userinfo); 虽然我们在传递参数的时候传递的是userinfo对象但是在xml文件中实现修改的时候不用写userinfo.属性名还是和之前一样直接写属性即可。 update idupdate update userinfo set username#{username} where id#{id}/update Testvoid update() {Userinfo userinfo new Userinfo();userinfo.setId(1);userinfo.setUsername(超级管理员);int result userMapper.update(userinfo);System.out.println(受影响的行数result);} 2.5、添加操作 添加操作和修改操作相同在接口中声明方法的时候定义的返回值类型是int因为默认的返回值是受影响的行数在XML文件实现add方法时也不需要规定返回值类型。 /** 添加操作* */int add(Userinfo userinfo); 这里我们只需要添加username、password和photo属性的值其他的cteatetime和updatetime的值会根据当前时间自动添加。 insert idaddinsert into userinfo(username,password,photo)values(#{username},#{password},#{photo})/insert Testvoid add() {Userinfo userinfo new Userinfo();userinfo.setUsername(张三);userinfo.setPassword(123);userinfo.setPhoto(/image/default.png);int result userMapper.add(userinfo);System.out.println(受影响的行数 result);} 2.6、特殊的添加返回自增id 之前的方法默认情况下返回的是受影响的行数如果想要返回自增id具体实现如下。 这个方法的声明和之前的添加方法的声明是相同的但是在XML文件中的具体实现有些差别。这里再声明方法的时候返回值类型是int不要将它认为是自增id的数据类型它还是受影响的行数的返回值类型。 /** 添加操作返回自增id* */int insert(Userinfo userinfo); 在XML文件中的insert标签中添加useGenerateKeys、keyColumn和keyProperty属性。 insert idinsert useGeneratedKeystrue keyColumnid keyPropertyidinsert into userinfo(username,password,photo)values(#{username},#{password},#{photo})/insert useGeneratedKeys:表示获取数据库中开启自增主键的值。在insert标签中表示的意思为获取本次添加的成员的自增主键的值。默认值为false.keyColumn:表示设置自增主键在数据表中的字段名。keyProperty:表示将获取到的自增主键的值赋值给keyProperty所指的属性实体类. 通过XML文件中insert方法的实现我们直接将自增主键的值设置到了userinfo实体类的id属性中了所以在测试类中只需要在打印的时候打印userinfo对象的id属性值就可以得到自增主键的值。 Testvoid insert() {Userinfo userinfo new Userinfo();userinfo.setUsername(李四);userinfo.setPassword(123);userinfo.setPhoto();//因为在接口中声明的insert方法的参数为userinfo,所以测试的时候可以直接将userinfo对象传给这个insert方法int result userMapper.insert(userinfo);System.out.println(受影响的行数 result | id: userinfo.getId());} 2.7、like查询 like查询我们按照学习MySQL是使用的语法在XML文件中构造sql语句在执行的时候会出现报错的问题。 /** like查询* */ListUserinfo getLikeList(Param(username)String username); select idgetLikeList resultTypecom.example.demo.model.Userinfoselect * from userinfo where username like %#{username}%/select Testvoid getLikeList() {String username 三;ListUserinfo list userMapper.getLikeList(username);System.out.println(list);} 有的老铁就会说了使用预编译的方式不行那就使用即时执行的方式这种方式执行确实结果是对的但是这里使用即时执行的方式并不满足使用它的条件会出现sql注入的情况。 ✨两种解决方法 1️⃣第一种解决方法是在XML中继续直接使用#{username}我们在业务代码中给username赋值为%三% select * from userinfo where username like #{username} Testvoid getLikeList() {String username %三%;ListUserinfo list userMapper.getLikeList(username);System.out.println(list);}  虽然这种方式可以解决问题但是在业务代码中写看起来就是不太好看。 2️⃣第二种解决方法是使用SQL语法中的concat字段对多个字符进行拼接。 select * from userinfo where username like concat(%,#{username},%) Testvoid getLikeList() {String username 三;ListUserinfo list userMapper.getLikeList(username);System.out.println(list);} 这种写法既不会出现单引号套单引号的问题在业务代码中进行like查询传参的时候也没有出现使用多余的符号的问题。 2.8、实体类中的属性和数据库表中的字段名不一致出现的问题的三种解决方式 MyBatis是通过实体类的属性名称和数据库中的字段名进行映射的如果实体类中的属性名和数据库表中的字段名不同在进行查询的时候出现的结果中字段的值会为null. ✨解决方案 将实体类中的属性名修改成和数据库表中的数据修改成一致的。这种方式只适合于当前这个实体类只有你一个人使用了如果其他人的代码中也使用了你创建的实体类那么就不能使用这种方式来修改了。使用SQL语句中的as对数据表中的字段名进行重命名让字段名等于创建的实体类的属性名。  定义一个resultMap将属性名和字段名进行手动映射。 3、使用注解的方式在MyBatis程序中构造SQL语句 3.1、多表联查一对一 这里我们查询一篇文章对应的作者的名字站在文章的角度进行多表联合查询就是一对一的情况。 使用注解的方式在MyBaits程序中构造SQL语句我们想要使用SQL的查询就可以在接口中的方法上加上注解Select想要使用删除可以在接口的方法上添加Delete想要使用插入可以在方法上添加Insert想要实现修改可以在方法上添加Update然后将要执行的sql语句写在这些注解的参数中即可。 1️⃣创建文章实体类 import lombok.Data;import java.time.LocalDateTime;Data public class ArticleInfo {private int id;private String title;private String content;private LocalDateTime createtime;private LocalDateTime updatetime;private int uid;private int rcount;private int state;//链表字段private String username; }2️⃣定义接口 package com.example.demo.mapper;import com.example.demo.model.ArticleInfo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import java.util.List;Mapper public interface ArticleMapper {Select(select articleinfo.*,userinfo.username from articleinfo left join userinfo on articleinfo.uiduserinfo.id)ListArticleInfo getAll(); }3️⃣创建单元测试 package com.example.demo.mapper;import com.example.demo.model.ArticleInfo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;SpringBootTest class ArticleMapperTest {Autowiredprivate ArticleMapper articleMapper;Testvoid getAll() {ListArticleInfo list articleMapper.getAll();System.out.println(list);} } 4️⃣执行结果 3.2、多表联查一对多  一对多的多表查询这里我们将查询步骤分为三步 根据id找到用户信息根据uid查询文章列表然后将得到的文章信息和用户信息进行组装即可 1️⃣首先我们需要在userinfo类用户实体类中添加一个alist属性最后用来将得到文章信息组装到userinfo对象中。 package com.example.demo.model;import lombok.Data;import java.time.LocalDateTime; import java.util.List;Data public class Userinfo {private int id;private String username;private String password;private String photo;private LocalDateTime createtime;private LocalDateTime updatetime;private int state;private ListArticleInfo alist; } 2️⃣然后在数据持久层的UserMapper类和ArticleMapper类中添加查询的方法 UserMapper接口中添加根据id查找用户的方法 Select(select * from userinfo where id#{id})Userinfo getUserById2(Param(id)Integer id); ArticleMapper接口中添加根据uid查找文章的方法 Select(select * from articleinfo where uid#{uid})ListArticleInfo getListByUid(Param(uid)Integer uid); 3️⃣在UserMapperTest单元测试类中创建一个getUserList方法在这个方法中调用上述两个方法最后调用setAlist方法将getListByUid方法中得到的文章列表添加到userinfo对象中就完成了多表查询的一对多的情况 package com.example.demo.mapper;import com.example.demo.model.ArticleInfo; import com.example.demo.model.Userinfo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;SpringBootTest//不能省略告诉当前的测试程序目前项目是运行在Spring容器中的 class UserMapperTest {Autowiredprivate UserMapper userMapper;Autowiredprivate ArticleMapper articleMapper;Testvoid getUserList(){int uid 1;//1.根据uid查询userinfoUserinfo userinfo userMapper.getUserById2(uid);//2.根据uid查询文章列表ListArticleInfo list articleMapper.getListByUid(uid);//3.组装数据userinfo.setAlist(list);System.out.println(userinfo);}} 4️⃣执行结果 4、安装MyBatis X插件 上述我们将在接口中声明的方法在xml文件中实现之后当接口中只有几个方法的时候我们想要找一个方法在XML文件中的具体实现我们可以点到XML文件中找但是如果存接口中存在很多的方法想要找XML中对接口中的某个方法的具体实现就需要我们一行一行的找效率非常慢这时候我们就需要安装MyBatis X插件它可以方便开发MyBatis实现XML和对应的接口方法之间的快速跳转。 四、动态SQL的使用 动态sql是MyBatis的强大特性之一能够完成不同条件下不同的sql拼接。 1、if标签 我们在上网时经常需要填写一些表单其中有些选项是必填的有些是选填的那么这个时候在MyBatis程序中按照XML的方式构造sql语句时是不能完全胜任的。比如填通讯信息的时候出现了一个选填项是填写QQ号如果不填这个选项前端传给后端代码中的这个数据的值为null现在规定让这一项在数据库中默认为空如果如不使用if标签那么在XML中是无法完成这个规定。在数据库中null和空是两个概念。 ✨语法 !-- test中的表达式是满足使用多个条件 -- if test表达式!-- 满足表达式的条件就会进入执行其中的内容 -- .... /if 1️⃣在接口中声明方法 /** 动态sql添加操作if* */int add2(Userinfo userinfo); 2️⃣在XML文件中实现动态sql这里再sql语句中添加if标签用来判断是否设置了photo的值如果没有设置那就不添加这个字段在sql语句中如果添加这个字段的值就会在sql语句中添加这个字段。 insert idadd2insert into userinfo(username,passwordif testphoto ! null,photo/if)values(#{username},#{password}if testphoto ! null,#{photo}/if)/insert 3️⃣测试单元 //给对象的属性设置值得时候给photo属性添加值Testvoid add2() {Userinfo userinfo new Userinfo();userinfo.setUsername(张三);userinfo.setPassword(123);userinfo.setPhoto(cat.png);int result userMapper.add2(userinfo);System.out.println(执行的结果 result);} 4️⃣执行结果 可以看到如果给photo没有设置值那么在数据看中photo这一列是空的不会出现null.这就解决了表单中可选项的填写问题了。如果填了表单中的可选项就会将值保存在数据库中如果没有填写可选项那么数据库中这个字段就不会有值。 2、trim标签 上面我们说的表单中存在某个选填项假设表单上所有的选项都是选填的那么使用if标签就不能满足我们的需求了。因为在判断给字段是否传值时使用if标签将字段包裹起来了但是字段和字段之间要使用逗号隔开所以我们还需要将逗号拼接上。但是我们不知道用户选填了那些字段所以将逗号拼接上之后还需要考虑逗号不能出现在开始的字段前面结束的字段后面不能出现逗号。这个就需要使用trim标签中的属性来解决了。 ✨trim标签的属性 prefix表示整个语句块以prefix的值作为前缀suffix表示整个语句块以suffix的值作为后缀prefixOverrides表示整个语句块要去除掉的前缀suffixOverrides表示整个语句块要去除掉的后缀 1️⃣在接口中声明方法 /** 动态sql trim标签* */int add3(Userinfo userinfo); 2️⃣在XML文件中实现方法由于trim标签中的prefix和suffix属性可以添加整个语句块的前缀和后缀所以这里我们直接使用这两个属性拼接括号我们在if标签中将逗号拼接在字段的后面使用suffixOverrides属性指定要去除语句块中某个后缀逗号整个时候就会将语句块中最后一个字段之后的逗号去掉。 insert idadd3insert into userinfotrim prefix( suffix) suffixOverrides,if testusername ! nullusername,/ifif testpassword ! nullpassword,/ifif testphoto!nullphoto,/if/trimvaluestrim prefix( suffix) suffixOverrides,if testusername ! null#{username},/ifif testpassword ! null#{password},/ifif testphoto ! null#{photo}/if/trim/insert3️⃣单元测试这里我们只添加名字和密码不添加照片的属性。 Testvoid add3() {Userinfo userinfo new Userinfo();userinfo.setUsername(李四);userinfo.setPassword(666);int result userMapper.add2(userinfo);System.out.println(执行的结果 result);} 4️⃣执行结果 可以看到执行结果中字段中没有将photo拼接上并且语句块中结尾的字段之后也没有逗号。数据保存在数据库中也是按照我们的预想执行的没有添加照片字段的值photo列为空不是null. 3、where标签 当传入对象之后where标签会判断对象中的属性来决定是否生成where关键字如果对象中的属性都没有传值的情况下就不会生成where关键字如果是查询操作的话就会将表中所有的数据查出来。where标签还可以将字段前面拼接的and字段去掉。 1️⃣接口中声明方法 /** 动态sql where标签* */ListUserinfo getListByWhere(Userinfo userinfo); 2️⃣在XML文件中实现方法 select idgetListByWhere resultTypecom.example.demo.model.Userinfoselect * from userinfowhereif testid0id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!nulland password#{password}/if/where where标签可以实现的效果trim标签也可以实现。 select idgetListByWhere resultTypecom.example.demo.model.Userinfoselect * from userinfotrim prefixwhere prefixOverridesandif testid0id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!nulland password#{password}/if/trim/select通过trim标签的prefix将where作为代码块的前缀如果代码块中没有字段则where不会加在sql语句中prefixOverrides属性将整个语句块要去除掉的前缀and去除。 3️⃣单元测试在这里没有传递id那么程序执行的时候where标签会将username字段之前的and去掉。 Testvoid getListByWhere() {Userinfo userinfo new Userinfo();userinfo.setUsername(李四);userinfo.setPassword(666);ListUserinfo list userMapper.getListByWhere(userinfo);System.out.println(list);}4️⃣执行结果 有属性值传入的时候 没有属性值传入的时候 4、set标签 set标签和where标签在sql语句中添加方式相同只不过where标签用在查询set标签用在修改。但是set标签是去掉代码块的后缀的而where标签是去掉代码块的前缀的。 1️⃣接口中声明方法 /** 动态sql set标签* */int update2(Userinfo userinfo); 2️⃣XML中实现方法 update idupdate2update userinfosetif testusername!nullusername#{username},/ifif testpassword!nullpassword#{password},/ifif testphoto!nullphoto#{photo},/if/setwhere id#{id}/update 3️⃣单元测试 Testvoid update2() {Userinfo userinfo new Userinfo();userinfo.setId(9);userinfo.setUsername(王五);int result userMapper.update2(userinfo);System.out.println(执行结果 result);} 4️⃣执行结果 5、foreach标签  如果我们想要删除多条数据的时候就会使用到foreach标签。前端传过来要删除的一组数据这个时候后端接收到的是一个集合sql语句中没有办法操作集合foreach标签可以通过循环遍历的方式将集合中的数据取出来给sql语句这就会就完成删除多条数据的操作。 ✨foreach标签的属性 collection绑定方法参数中的集合如ListSetMap或数组对象item遍历时的每一个对象open表示该语句以什么开始最常用的是左括弧’(’close表示该语句以什么结束最常用的是右括弧’)’separator每次遍历之间间隔的字符串表示循环的时候用什么分割每一项的值 1️⃣接口中声明方法 /**动态sql foreach标签* */int delById2(ListInteger ids); 2️⃣在XML中实现方法 这里设置的item属性是遍历集合中每个对象的别名item中设置的值要和#{}中的值相同否则程序就会报错。 delete iddelById2delete from userinfo where id inforeach collectionids open( close) itemid separator,#{id}/foreach/delete3️⃣单元测试 Testvoid delById2() {ListInteger list new ArrayListInteger();list.add(7);list.add(8);list.add(9);int result userMapper.delById2(list);System.out.println(result :result);} 4️⃣执行结果
http://www.ihoyoo.com/news/19412.html

相关文章:

  • 保定做网站开发的公司有哪些网站首页分类怎么做的
  • 品牌网站建设精湛磐石网络网站建设考试多选题
  • 网站如何做背景音乐多城市地方门户网站系统
  • 购物网站开发需要什么技术建立网站 知乎
  • 服务网站欣赏做博客网站赚钱吗
  • 交易网站开发文档wordpress账户密码为空
  • 如何做外贸品牌网站建设怎么开公司
  • 成都建网站云尚网络科技有限公司介绍
  • 创一家网站家具网站建设的背景
  • 青岛网站开发哪家好自己可以建设环保公益网站吗
  • 做网站和易语言电子商务的工作岗位有哪些?
  • 滁州seo网站推广方案开发工程师是程序员吗
  • asp网站文章自动更新网页打不开用什么浏览器
  • 株洲做网站定制苏州百度推广排名优化
  • 东莞网站建设培训班新余集团网站建设
  • 深圳网站搜索排名潍坊专业网站建设公司
  • 小说阅读网站系统模板下载建立一个app要多少钱
  • 建设网站的源代码网站开发需要会什么
  • 巴州区建设局网站网络推广网站培训班
  • 南京网站制作联系宋网站视频不能下载怎么保存视频
  • 游戏logo设计网站wordpress更新500错误
  • 四平网站seo北京企业查询
  • 站长工具源码优化网站链接的方法
  • 网站建设用dw白云做网站的公
  • 兰州北山生态建设局网站威海教育行业网站建设
  • 网站平台规划沈阳市住房和城乡建设局网站
  • 网站开发学生职业规划开源手机建站系统
  • 网站的客户体验做室内意向图的网站
  • 食品网站建设的照片网站建设宣传图片
  • 长沙城乡建设部网站首页不花钱做推广的网站