安全狗网站白名单指什么,申请制作网站,做网投网站好,网站 用户登陆 模板hi,大家好,今天为大家带来MyBatis操作数据库的知识 文章目录 #x1f437;1.根据MyBatis操作数据库#x1f9ca;1.1查询操作#x1f347;1.1.1无参查询#x1f347;1.1.2有参查询 #x1f9ca;1.2删除操作#x1f9ca;1.3修改操作#x1f9ca;1.4增加操作#x1f9ca;… hi,大家好,今天为大家带来MyBatis操作数据库的知识 文章目录 1.根据MyBatis操作数据库1.1查询操作1.1.1无参查询1.1.2有参查询 1.2删除操作1.3修改操作1.4增加操作1.5特殊的添加:返回自增id1.6like查询1.7多表查询1.7.1一对一查询1.7.2一对多查询 1.8动态sql 2.动态标签2.1 if标签2.2trim标签2.3where标签2.4set标签2.5foreach标签 1.根据MyBatis操作数据库
1.接口:方法的声明(给Service层调用) 2. XML:实现接口
1.1查询操作
1.1.1无参查询
1.定义接口
Mapper//代表数据持久层
public interface UserMapper {ListUserinfo getAll();
}
2.在xml里面写接口的实现 这里的namespace里面的内容表明了当前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根据接口写测试类
package com.example.demo.mapper;import com.example.demo.model.Userinfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2023-08-08* Time: 9:56*/
SpringBootTest //不能省略,表明当前测试程序当前项目是运行在spring boot项目中的
class UserMapperTest {Autowiredprivate UserMapper userMapper;Testvoid getAll() {ListUserinfo listuserMapper.getAll();System.out.println(list);}
}从结果中可以看到运行是成功的,打印了sql语句,也打印了结果 为了方便开发MyBatis,实现XML和对应的接口之间的快速跳转,我们可以安装一个MyBatisX插件 红色的小鸟和蓝色的小鸟相互点击就可以跳转
1.1.2有参查询 select idgetById resultTypecom.example.demo.model.Userinfoselect * from userinfo where id${uid}/select加上这个Param注解,在传参的时候就必须用该注解里面的名字 我们可以测试一下 错误显示说ID不存在 改成id 运行成功了
MyBatis有参查询的时候传递参数的方法有两个 1.使用$符
这俩相对应,结果 显示的是直接替换,也叫做及时执行
这是直接将id位置的数字替换成出传过来的参数 2.采用# 运行结果是采用了JDBC里面那种占位符的形式 意味sql执行是预编译执行的 问题:${}和#{}的区别 1.${}是直接替换,#{}是预执行 2.${}不安全,存在sql注入,#{}安全,不存在sql注入 sql注入:使用一些特殊的sql语句完成一些非法操作,比如一个登录功能,只有用户名和密码均输入正确,才能登录成功,但是当密码胡乱输入也登陆成功,也就是sql注入 ${}可以实现的功能#{}都能实现. 而且${}还存在sql注入的问题,为啥还要用它?
因为有一种场景预编译做不到!
比如在网上购物时,浏览商品的时候有按照价格升序或者降序,此时如果使用#{}那么就会把升序或者降序当做value值,加一个单引号,那么在sql语句中自然是错误的,使用${}就直接替换,采用string拼接的方式,不会报错,举个例子
换成$符号 运行成功了
总结:$应用场景:当业务需要传递sql命令的时候
注意事项:使用$符传的的参数一定要被穷举 我们刚才的例子里面参数可以被穷举,一个是asc,一个是desc,符合条件
1.2删除操作 因为id85的用户压根不存在 当在测试的时候想要保证不影响数据库的数据,可以加上一个transitional注解,代表开启是事务,可以回滚 我们可以看到受影响的行数,但是也不影响数据库的数据 注意但类中属性和数据库表中的字段名不一样时.查询结果可能为null
解决办法:
1.将类名属性和表中字段名改为一致 2.使用sqi语句的as进行字段名重命名 3.定义一个resultMap,将属性名和字段名进行手动映射 resultMap idAseMap typecom.example.demo.model.Userinfoid columnid propertyid/idresult columnusername propertyname/resultresult columnpassword propertypassword/resultresult columnphoto propertyphoto/resultresult columncreatetime propertycreatetime/resultresult columnupdatetime propertyupdatetime/resultresult columnstate propertystate/result
/resultMapcolumn代表的是字段名,property代表的是属性名 可以看到显示了名字
1.3修改操作 可以看到修改成功 修改,删除,增加都默认返回受影响的行数
1.4增加操作 插入成功
1.5特殊的添加:返回自增id insert idadd2 useGeneratedKeystrue keyColumnid keyPropertyidinsert into userinfo(username,password,photo) values(#{username},#{password},#{photo})/insertTestvoid add2() {Userinfo userinfonew Userinfo();userinfo.setUsername(詹妮);userinfo.setPassword(5896);userinfo.setPhoto(null);int resultuserMapper.add2(userinfo);System.out.println(受影响的行数resultiduserinfo.getId());}为什么我这里没有设置id我却可以使用getid方法? 因为数据库的id已经设置到了该属性中 1.6like查询
ListUserinfo getByLike();
select idgetByLike resultTypecom.example.demo.model.Userinfoselect * from userinfo where username like _%三/selectTestvoid getByLike() {ListUserinfo listuserMapper.getByLike();System.out.println(list.toString());}当查询的时候不一定是按照这个字匹配,可以换成一个动态的,把匹配条件放到测试类里面
select idgetByLike resultTypecom.example.demo.model.Userinfoselect * from userinfo where username like #{username}/selectTestvoid getByLike() {String username_三%;ListUserinfo listuserMapper.getByLike(username);System.out.println(list.toString());}2.使用mysql内置函数concat
select idgetByLike resultTypecom.example.demo.model.Userinfoselect * from userinfo where username like concat(%,#{username},%)/selectTestvoid getByLike() {String username三;ListUserinfo listuserMapper.getByLike(username);System.out.println(list.toString());}
}1.7多表查询
1.7.1一对一查询
在查询文章表的过程中显示用户姓名 我们不采用xml实现接口,我们采用注解的方式 增删改查都有对应的注解
package com.example.demo.model;import lombok.Data;import java.time.LocalDateTime;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2023-08-08* Time: 18:43*/
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;}
Mapper
public interface ArticleMapper {Select(select * from articleinfo)ListArticleinfo getAll();
}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.*;/*** Created with IntelliJ IDEA.* Description:* User: WHY* Date: 2023-08-08* Time: 18:52*/
SpringBootTest
class ArticleMapperTest {Autowiredprivate ArticleMapper articleMapperTestvoid getAll() {ListArticleinfo listarticleMapper.getAll();System.out.println(list.toString());}
}将联合查询的语写在注解里面即可
Mapper
public interface ArticleMapper {Select(select articleinfo.*,userinfo.username from articleinfo,userinfo where articleinfo.uiduserinfo.id)ListArticleinfo getAll();
} 所以MyBatis不仅可以采用接口xml实现 还可以采用接口注解的方式实现
1.7.2一对多查询
查询一个用户的多篇文章 我们采用线程的方式 一个查用户表,一个查文章表,最后拼接起来 先采用单线程的方式来写
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 article;
}ListUserinfo getByLike(Param(username) String username);Select(select * from userinfo where id#{id})Userinfo getByid(Param(id) Integer id);Select(select * from articleinfo where uid#{uid})ListArticleinfo getByarticle(Param(uid) Integer uid);Testvoid getUserList(){int uid1;//1.先根据uid查询用户表Userinfo userinfo userMapper.getByid(uid);System.out.println(userinfo);//2.根据uid查询文章ListArticleinfo list articleMapper.getByarticle(uid);//组装数据userinfo.setArticle(list);System.out.println(userinfo);}
}采用多线程的方式 Testvoid getUserList(){int uid1;//定义线程池ThreadPoolExecutor threadPoolExecutornew ThreadPoolExecutor(5,10,3000, TimeUnit.SECONDS,new LinkedBlockingDeque(100));final Object[] resultnew Object[2];//1.先根据uid查询用户表threadPoolExecutor.submit(new Runnable() {Overridepublic void run() {result[0] userMapper.getByid(uid);}});//2.根据uid查询文章threadPoolExecutor.submit(new Runnable() {Overridepublic void run() {result[1] articleMapper.getByarticle(uid);}});//组装数据(等线程池执行结束以后)while (threadPoolExecutor.getTaskCount() !threadPoolExecutor.getCompletedTaskCount()) {}Userinfo userinfo(Userinfo) result[0];userinfo.setArticle((ListArticleinfo)result[1]);System.out.println(userinfo);}1.8动态sql
动态sql是MyBatis的强大特性之一 在填写某一个页面的时候,有些页面不需要填写,不插入这个字段 动态sql主要是在xml里面动态的进行判断 我们拿userinfo表中的photo来进行举例 看到photo默认是空 现在的问题是当不设定时photo啥也没有,符合预期,当设定photo的时候,就出现了值,我们想要的效果是当该字段默认为空,不可进行显示设置,所以我们需要使用标签
2.动态标签
2.1 标签 insert idadd3insert into userinfo(username,passwordif testphoto!null,photo/if)values(#{username},#{password}if testphoto!null,#{photo}/if)/insert单元测试来一波~~~ Testvoid add3() {Userinfo userinfonew Userinfo();userinfo.setUsername(张三);userinfo.setPassword(123);userinfo.setPhoto(dor.png);int resultuserMapper.add3(userinfo);System.out.println(受影响的行数result);}现在将photo的设定取消 可以看到没有photo再出现
2.2标签
标签针对单个字段是选填项的时候比较方便,当所有字段都是选填项时,我们可以使用标签 标签中有如下属性: prefix表示整个语句块以prefix的值作为前缀 suffix表示整个语句块以suffix的值作为后缀 prefixOverrides表示整个语句块要去除掉的前缀 suffixOverrides表示整个语句块要去除掉的后缀
insert idadd4insert 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/insert2.3标签
该标签也是和标签一起使用的.但是仅仅使用在where查询当中
select idgetBywhere resultTypecom.example.demo.model.Userinfoselect * from userinfowhereif testid0id#{id}/ifif testusername!nulland username#{username}/ifif testphoto!nulland password#{password}/if/where/select这个and为什么要加在属性的前面 可以看到没有sql语句,没有多余的and 这也恰恰体现了标签的特性 1.根据标签里面的内容决定是否生成关键字 2.去除最前面的and 那么where的功能用trim可以实现吗? 当然啦 select idgetBywhere resultTypecom.example.demo.model.Userinfoselect * from userinfotrim prefixwhere prefixOverridesandif testid0id#{id}/ifif testusername!nulland username#{username}/ifif testphoto!nulland password#{password}/if/trim/select2.4标签
用来修改数据,有值生成set,去掉的是最后面的符号
update idupdate2update userinfosetif testusername!nullusername#{username},/ifif testpassword!nullpassword#{password}/ifif testphoto!nullphoto#{photo}/if/setwhere id#{id}/update2.5标签
foreach 标签常用于遍历集合或数组类型的数据。其属性如下
collection: 指定要遍历的集合或数组类型的数据item: 指定遍历的集合中的元素别名可以在标签中使用该别名访问元素index: 指定遍历的集合中的元素索引别名可以在标签中使用该别名访问元素索引open: 指定遍历的开始标记close: 指定遍历的结束标记separator: 指定遍历元素之间的分隔符javaType: 指定集合或数组所对应的 Java 类型如果未指定则 MyBatis 会自动选择最匹配的类型。
适用情况:适用于多条sql删除的时候
delete iddeleteById2delete from userinfowhere id inforeach collectionids open( close) itemid separator,#{id}/foreach/deleteTestvoid deleteById2() {ListInteger list new ArrayListInteger() {{add(10);add(11);add(12);add(13);}};int resultuserMapper.deleteById2(list);System.out.println(resultresult);}这期的内容就到这,我们下期再见咯~~