阳江网站建设推广公司电话,自己做网站推广费用大,黄骅港有什么好玩的,在网站上做播放视频广告是否违法需求 : 根据用户的输入情况进行插入
动态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 testgender!nullgender,/if phone) value(#{username},#{password},#{age}, if testgender!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 version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.mybatisdemo.mapper.UserInfo2Mapper/mapper
然后补充代码
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.mybatisdemo.mapper.UserInfo2Mapperinsert idinsertByXMLinsert into userinfo(username,password,age,if testgender!nullgender,/ifphone)values(#{username},#{password},#{age},if testgender!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.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.thjqk.cn.gov.cn.thjqk.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.ktskc.cn.gov.cn.ktskc.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.pcqxr.cn.gov.cn.pcqxr.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.iqcge.com.gov.cn.iqcge.com http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.qbwyd.cn.gov.cn.qbwyd.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.saastob.com.gov.cn.saastob.com