误给传销公司做网站算犯罪吗,全球速卖通官网入口,seo比较好的公司,网站搭建免费软件文章目录 1.动态SQL使用什么是动态sql为什么用动态sql标签拼接标签拼接标签拼接标签拼接标签拼接 补充1#xff1a;resultType和resultMap补充2#xff1a;后端开发中单元测试工具使用#xff08;Junit框架#xff09; 1.动态SQL使用
以insert标签为例
什么是动态sql
是… 文章目录 1.动态SQL使用什么是动态sql为什么用动态sql标签拼接标签拼接标签拼接标签拼接标签拼接 补充1resultType和resultMap补充2后端开发中单元测试工具使用Junit框架 1.动态SQL使用
以insert标签为例
什么是动态sql
是mybatis的特性之一能在xml里边写逻辑判断if else for循环
为什么用动态sql
对于非必传参数如果传了就做xx处理如果没传就给默认值。 常用的就5个掌握这些即可。
标签拼接
作用判断。例如注册分为必填项和非必填项如果有值就拼接上去如果没值就不拼接。 使用test里边判断的key是属性不是数据库中字段还有{}里边的
int addUser(UserEntity user);insert idaddUser2insert into userinfo(username,passwordif testphoto!null and photo!,photo/if)values(#{username},#{password}if testphoto!null and photo!,#{photo}/if)
/insertTest
void addUser2(){String usernameliliu;String password123456;UserEntity usernew UserEntity();user.setUsername(username);user.setPassword(password);int resultuserMapper.addUser2(user);System.out.println(result);
}标签拼接
作用去除前置空格和结尾空格 使用有四个属性 prefix:表示整个语句块以prefix值作为前缀 suffix:表示整个语句块以suffix的值作为后缀 prefixOverrides:表示整个语句块要去除的前缀 suffixOverrides:表示整个语句块要去除的后缀
表示语句必须以(开始)结束去除最后的,
insert idaddUser3insert into userinfotrim prefix( suffix) suffixOverrides,if testusername!null and username!username,/ifif testimg!null and img!photo,/ifif testpwd!null and pwd!password,/if/trimvaluestrim prefix( suffix) suffixOverrides,if testusername!null and username!#{username},/ifif testimg!null and img!#{img},/ifif testpwd!null and pwd!#{pwd},/if/trim/insertTransactional
Test
void addUser3() {String username liliu;String password 123456;UserEntity user new UserEntity();user.setUsername(username);user.setPwd(password);
// user.setImg(cat.png);int result userMapper.addUser3(user);System.out.println(添加 result);}标签拼接
作用条件查询的时候进行使用 场景进行列表页的查询,多个非必传参数的处理解决方案之where标签 用法会自动去除and前缀不会自动去除and后缀有条件自动加上where条件没有则不加
ListArticleInfoVO getListByIdOrTitle(Param(id) Integer id,Param(title) String title);select idgetListByIdOrTitle resultTypecom.example.demo.entity.vo.ArticleInfoVOselect * from articleinfowhereif testid!null and id0id#{id}/ifif testtitle!null and title!and title like concat(%,#{title},%)/if/where/selectTestvoid getListByIdOrTitle() {ListArticleInfoVO list articleMapper.getListByIdOrTitle(1, null);System.out.println(list.size());}标签拼接
作用进行就修改的时候去掉最后的逗号有修改内容就生成set否则不加 场景例如修改用户昵称
int updateById(User user);
update idupdateById parameterTypeorg.example.model.Userupdate usersetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password},/ifif testnickname ! nullnickname#{nickname},/ifif testsex ! nullsex#{sex},/ifif testbirthday ! nullbirthday#{birthday},/ifif testhead ! nullhead#{head},/ifif testcreateTime ! nullcreate_time#{createTime},/if/setwhere id#{id}
/update可以在controller层进行控制必须要有对应的参数不为空既然来了就必须要改
标签拼接
作用对集合进行遍历 用法属性 collection:绑定方法参数的集合 item:遍历时开始的字符串 open:语句块开始的字符串 close:语句块结束的字符串 separator:每次遍历之间间隔的字符串 加载需要循环的部分上边 场景批量删除指定id的文章
// 根据文章id集合批量删除文章int delByIdList(ListInteger idList);delete iddelByIdList!-- where id in(1,2..)--delete from articleinfowhere id inforeach collectionidList itemaid open( close) separator,#{aid}/foreach/deleteTransactionalTestvoid delByIdList() {ListInteger idList new ArrayList();idList.add(1);idList.add(2);idList.add(3);int result articleMapper.delByIdList(idList);System.out.println(删除条数 result);}补充1resultType和resultMap
使用场景:数据库表名和实体类中的名字不同框架不能再自动映射。 需要手动写自己做映射。映射表和实体类字段名和属性。前边是实体类后边是数据库表 对应的标签resultType改成resultMap 一个xml中可以有多个resultMap
补充2后端开发中单元测试工具使用Junit框架
在使用之前需要在springboot项目中添加junit框架 但是一般来说springboot项目加载后这个依赖自动就拉去了。
在需要单元测试的类中右键Test接口中Junit5springboot内置的5添加单元测试代码 在测试类上边加上SpringBootTest。将对象交给Spring管理是在springboot项目中进行测试的在测试方法上加上Transactional不污染数据库。默认还是污染所以切记在增删改操作测试时加上此注解可以回滚事务不污染数据库。他可以修饰类也可以修饰方法Transactional实现原理方法执行前开启事务结束后回滚事务 roll back。注入属性调用对应测试的方法传输响应数据进行测试另外添加测试代码和新建测试类区别在于会有一个追加的提示框确认即可
具体使用方法在上边的例子中演示。 开发中只是看一个结果具体测试会交给测试人员
特点说明
简单直观快速测试某功能是否正确跳过用户校验直接测试代码。可以每次只执行一个方法可以在不污染数据库的前提下测试功能 过用户校验直接测试代码。可以每次只执行一个方法可以在不污染数据库的前提下测试功能帮助在打包发现一些问题