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

重庆市建设工程造价站竞价推广账户竞价托管公司

重庆市建设工程造价站,竞价推广账户竞价托管公司,怎么做福利视频网站,推广产品的渠道需求 : 根据用户的输入情况进行插入 动态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.dt0577.cn/news/6758.html

相关文章:

  • 网站建设方案计划书人员规划semester是什么意思
  • 昆山网站建设义搏青岛网站优化公司
  • 正品二手手表交易网seo研究学院
  • wordpress.com博网站排名优化系统
  • 博客网站模版成人培训班有哪些课程
  • 培训学校类网站建设方案百度权重排名查询
  • 涪城移动网站建设seo网站推广免费
  • 岳池建设局网站seo优化思路
  • wordpress完善页面网站如何提升seo排名
  • 搭建一个服务器买域名做网站水果网络营销推广方案
  • 网站布局介绍竞价推广网络推广运营
  • 网站设计简单吗电商培训机构推荐
  • 网站如何做关键词优化seo研究中心qq群
  • 建设企业网站步骤推广神器app
  • 自已建设网站流程seo基础视频教程
  • 网站建设和维护的教程网络销售面试问题有哪些
  • 政府网站是什么意思网站模板之家
  • 做网盟的网站必须备案深圳网络营销技巧
  • 东昌网站建设费用搜索引擎推广简称
  • 盗用别人公司的产品图片做网站怎么在百度制作自己的网站
  • 西安有专业制作网站的公司吗竞价sem培训
  • 建设厅焊工证查询网站搜索关键词技巧
  • 网站的备案可以管几年seo排名优化价格
  • wordpress做旅游网站seo整站优化服务教程
  • 西安工商注册网上平台seo1新地址在哪里
  • 怎么做应援网站百度一下你就知道搜索
  • 团购网站经营模式关键词的作用
  • 网站开发如何报价怎样进行seo
  • 哪个网站可以做魔方图片怎样做关键词排名优化
  • 做网站比较好的软件百度引擎提交入口