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

华强北电子网站建设自媒体人专用网站

华强北电子网站建设,自媒体人专用网站,新乡优化,6网站建设做网站需求 : 根据用户的输入情况进行插入 动态SQL:根据需求动态拼接SQL 用户往表中插入数据,有的数据可能不想插入,比如不想让别人知道自己的性别,性别就为空 insert into userinfo(username,password,age,gender,phone) values(?,?,?,?,?); insert into userinfo(username,…

需求 : 根据用户的输入情况进行插入

动态SQL:根据需求动态拼接SQL

用户往表中插入数据,有的数据可能不想插入,比如不想让别人知道自己的性别,性别就为空

insert into userinfo(username,password,age,gender,phone) values(?,?,?,?,?);
insert into userinfo(username,password,age,gender) values(?,?,?,?);
insert into userinfo(username,password,age,phone) values(?,?,?,?);

接下来看看 mybatis 注解的方式该如何实现动态SQL,新建了一个 userInfo2Mapper 接口

<if>标签里面的意思是 : 如果gener不为null,那就输出if标签的内容

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfo2Mapper {@Insert("<script>" +" insert into userinfo(username,password,age," +"<if test='gender!=null'>gender,</if>" +"phone) " +"value(#{username},#{password},#{age}," +"<if test='gender!=null'>#{gender},</if>" +"#{phone})" +"</script>")Integer insert(UserInfo userInfo);
}

 然后老样子,Generate,test,勾选 insert ,然后补充代码,我们先每个数据都插入内容

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfo2MapperTest {@Autowiredprivate UserInfo2Mapper userInfo2Mapper;@Testvoid insert() {UserInfo userInfo = new UserInfo();userInfo.setUsername("kiki");userInfo.setPassword("555www");userInfo.setAge(23);userInfo.setGender(1);userInfo.setPhone("177966");userInfo2Mapper.insert(userInfo);}
}

插入成功 

数据库中也能成功找到刚刚插入的数据 

接下来我们要测试性别为空的情况,把 test代码里面的 userinfo.setGender 给去掉, 再次运行

 数据库也能找到,说明性别为空也插入成功了

上面是注解的方式,接下来我们看看 XML 的方式该如何实现 

在resources 中创建 Userinfo2XMLMapper.xml 文件

然后在 userInfo2Mapper 接口 中声明这个方法

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfo2Mapper {Integer insertByXML(UserInfo userInfo);
}

将 Userinfo2XMLMapper.xml 文件中的 namespace 进行修改,改为 userInfo2Mapper 接口中的第一行 package 的内容再加上接口名

<?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.example.mybatisdemo.mapper.UserInfo2Mapper"></mapper>

然后补充代码

<?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.example.mybatisdemo.mapper.UserInfo2Mapper"><insert id="insertByXML">insert into userinfo(username,password,age,<if test="gender!=null">gender,</if>phone)values(#{username},#{password},#{age},<if test="gender!=null">#{gender},</if>#{phone})</insert>
</mapper>

再回到接口,然后Generate,test,勾选insertByXML,ok,先测试每个数据都插入的情况

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
class UserInfo2MapperTest {@Autowiredprivate UserInfo2Mapper userInfo2Mapper;@Testvoid insertByXML() {UserInfo userInfo = new UserInfo();userInfo.setUsername("io");userInfo.setPassword("555www");userInfo.setAge(23);userInfo.setGender(1);userInfo.setPhone("177966");userInfo2Mapper.insertByXML(userInfo);}
}

成功插入

再把 userinfo.setGender 给去掉,再次运行

 

 没毛病

http://www.hkea.cn/news/952943/

相关文章:

  • python基础教程雪峰东莞搜索seo网站关键词优化
  • b2b网站开发供应商小程序开发教程全集免费
  • 用自己的手机做网站外链网站是什么
  • 市场调研公司介绍网站推广优化公司
  • 玉溪人民政府网站建设现状新网站seo
  • 湖南餐饮网站建设2023北京封控了
  • 重庆网站设计人员外贸网站搭建推广
  • 局域网内的网站建设西安网站建设公司排名
  • 普通网站报价多少中南建设集团有限公司
  • 蚌埠做网站哪家好全网营销国际系统
  • 沈阳市网站制作谷歌香港google搜索引擎入口
  • 做美食网站的背景高端网站建设制作
  • 文件什么上传到wordpress泉州seo技术
  • 网站地址地图怎么做网页制作的软件有哪些
  • 如何用万网建设网站口碑营销策划方案
  • 做网站的基础架构东莞seo建站公司
  • 嘉兴做网站的哪家好龙岗网站制作
  • 论坛做网站好吗百度官方网页
  • 微信开发者工具获取系统日期seo优化一般包括
  • 怎么用文本做网站百度排行榜风云榜
  • 未来网站开发需求多搜索网站有哪几个
  • 网站建设 成都郑州高端网站制作
  • 快站怎么做淘客网站深圳关键词
  • 做网站时如何去掉网站横条小红书软文案例
  • 图虫南宁百度快速排名优化
  • 上城网站建设app推广文案
  • 网站建设特点宁波seo搜索引擎优化公司
  • 地产商网站建设网球新闻最新消息
  • 做爰全过程网站免费的视频谷歌seo搜索引擎
  • 怎么架设网站seo推广培训