如何免费做一个网站攻略,手机网站免费建设平台,100m的网站 数据库,企业如何申请网址文章目录 前言LocalDate数据库代码po 包 ifwhere 标签 查trim 标签 增set 标签 改foreach 标签 删 前言
提示#xff1a;这里可以添加本文要记录的大概内容#xff1a;
查询条件是动态的
MyBatis的动态SQL语句是指在运行时根据不同条件选择不同的SQL语句执行。 这些条件可… 文章目录 前言LocalDate数据库代码po 包 ifwhere 标签 查trim 标签 增set 标签 改foreach 标签 删 前言
提示这里可以添加本文要记录的大概内容
查询条件是动态的
MyBatis的动态SQL语句是指在运行时根据不同条件选择不同的SQL语句执行。 这些条件可以是参数值、条件语句、动态标签等。 动态SQL语句的编写可以有效地减少重复代码
LocalDate
在 Java 8 中LocalDate 是 java.time 包中的一个类表示一个日期年、月、日。为了创建一个 LocalDate 对象需要使用 LocalDate.of() 方法该方法接受三个参数年、月、日。
例如
LocalDate date LocalDate.of(2022, 9, 1);这将创建一个 LocalDate 对象表示 2022 年 9 月 1 日。
在你的代码中为了将生日传递给 Cust 对象你需要使用 LocalDate.of() 方法来创建一个 LocalDate 对象并将其传递给 Cust 构造函数。例如
Cust cust new Cust(null, 吴三, 17446541321, LocalDate.of(2008, 5, 12), 10.0);这将创建一个 Cust 对象并将生日设置为 2008 年 5 月 12 日。
所以你需要注意的是LocalDate 是一个类而 LocalDate.of() 是用于创建 LocalDate 对象的方法。
具体使用
LocalDate 类提供了一个静态方法 of(int year, int month, int dayOfMonth)该方法允许创建一个指定年份、月份和日期的 LocalDate 对象。
以下是使用 LocalDate.of() 方法创建 LocalDate 对象的步骤
导入 java.time.LocalDate 类 import java.time.LocalDate;使用 of() 方法创建 LocalDate 对象 LocalDate date LocalDate.of(year, month, dayOfMonth);其中 year、month 和 dayOfMonth 分别代表年份、月份和日期是整数类型的值 LocalDate date LocalDate.of(2022, 10, 1);创建了一个 LocalDate 对象表示 2022 年 10 月 1 日。 处理 LocalDate 对象 一旦创建了一个 LocalDate 对象可以使用它来执行各种操作例如 获取日期的年份、月份和日期 int year date.getYear();int month date.getMonthValue();int day date.getDayOfMonth();获取星期几 DayOfWeek dayOfWeek date.getDayOfWeek();进行日期计算 LocalDate plusDays date.plusDays(7); // 返回一周后的日期LocalDate minusMonths date.minusMonths(2); // 返回两个月前的日期比较日期 LocalDate otherDate LocalDate.of(2022, 10, 2);boolean isBefore date.isBefore(otherDate); // 返回 trueboolean isAfter date.isAfter(otherDate); // 返回 falseboolean isEqual date.isEqual(otherDate); // 返回 false这些就是使用 LocalDate.of() 方法创建和处理 LocalDate 对象的步骤。
数据库代码
这里以客户表为例
CREATE TABLE cust (id INT PRIMARY KEY auto_increment, # idname VARCHAR(50) NOT NULL, # 姓名phone VARCHAR(20), # 电话birthday DATE, # 出生日期balance DOUBLE # 余额
);
INSERT INTO cust (id, name, phone, birthday, balance) VALUES (1, 张三, 13611111111, 1990-01-01, 1000.00);INSERT INTO cust (id, name, phone, birthday, balance) VALUES (2, 李四, 13722222222, 1995-02-02, 2000.00);INSERT INTO cust (id, name, phone, birthday, balance) VALUES (3, 王五, 13833333333, 1985-03-03, 3000.00);po 包
新建项目后完善 pom.xml 文件、mybatis 的配置文件、log4j 的日志文件
在 main/java 文件中新建包 mapper 和 po
po 包代码这里面有个Date类型要注意
public class Cust {private Integer id;private String name;private String phone;private Date birthday;private double balance;// 为了模糊查询某个时间段private Date startTime;private Date endTime;// 自动生成 Getter、Setter、toString()、有参无参方法
}提示以下是本篇文章正文内容下面案例可供参考
ifwhere 标签 查
ifwhere语句可以根据条件动态构建where子句以过滤查询结果。
在 mapper 包内新建接口文件和与之同名的映射文件 映射文件中的namespace必须是接口的限定名
先写接口文件 在模糊查询功能中输入 id 按照 id 查询输入名字按照名字查询输入手机号按照手机号查询所以返回值是多个查询多个用List
public interface CustMapper {ListCust queryCusts(Cust cust);
}复制方法名回到映射文件粘贴到 id
下面模糊查询某个时间段的映射文件代码
mapper namespacecom.mybatis.mapper.CustMapper!-- 动态的where条件 --select idqueryCusts parameterTypeCust resultTypeCustselect * from custwhereif teststartTime ! nulland birthday gt;#{startTime}/ifif testendTime ! nulland birthday lt;#{endTime}/if/where/select
/mapper以上代码中映射文件中的 select 语句使用了动态的 where 条件其中的 if 标签用于判断 startTime 和 endTime 是否为 null若不为 null则生成相应的SQL语句。 若输入了 startTime则添加一个条件为 birthdaystartTime 的 where 子句 若输入了 endTime则添加一个条件为 birthdayendTime 的 where 子句。
测试代码模糊查询某个时间段 Testpublic void where() throws Exception {CustMapper mapper session.getMapper(CustMapper.class);SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);Date startTime sdf.parse(1990-01-01);Date endTime sdf.parse(1999-12-31);Cust cust new Cust();cust.setStartTime(startTime);cust.setEndTime(endTime);mapper.queryCusts(cust);}在测试代码中将 startTime 和 endTime 分别解析成日期格式并通过set方法设置到Cust 对象中作为参数传递给 queryCusts 方法进行查询。 如果是模糊查询 id、姓名、手机号 映射文件代码
mapper namespacecom.mybatis.mapper.CustMapper!-- 动态的where条件 --select idqueryCusts parameterTypeCust resultTypeCustselect * from custwhereif testid ! nulland id #{id}/if!-- 类型是String也不能等于空 --if testname ! null and name ! and name like concat(%,#{name},%)/ifif testphone ! null and phone ! and phone like concat(%,#{phone},%)/if /where/select
/mapper测试代码模糊查询 id、姓名、手机号 Test public void where() {CustMapper mapper session.getMapper(CustMapper.class);Cust cust new Cust(null, 三, null,null,1000.0);mapper.queryCusts(cust);}在测试代码中创建了一个Cust 对象只设置了 name 属性值为三然后调用queryCusts 方法进行查询。因为 id 和 phone 属性值为null所以查询条件中只会有name 相关的查询语句。此时查询结果会返回所有名字包含三的记录。
需要注意的是测试代码中设置的name值必须是一个非空字符串才会被加入到查询条件中。如果 name 属性值为 null 或空字符串那么查询条件中不会包含对 name 的筛选。
查询结果
trim 标签 增
Trim语句可以包装一组语句去除多余的空格或逗号
mapper接口的代码
void insertCust(Cust cust);在映射文件增加对应的语句 增加客户信息
insert idinsertCust parameterTypeCust useGeneratedKeystrue keyPropertyidinsert into custtrim prefix( suffix) suffixOverrides,id, name,if testphone ! null and phone ! phone,/ifif testbirthday ! nullbirthday,/ifif testbalance ! nullbalance,/if/trimtrim prefixvalues( suffix) suffixOverrides,#{id}, #{name},if testphone ! null and phone ! #{phone},/ifif testbirthday ! null#{birthday},/ifif testbalance ! null#{balance},/if/trim
/insert接下来是测试【注意 LocalDate】当然前面的类型也要改 Testpublic void trim() {CustMapper mapper session.getMapper(CustMapper.class);Cust cust new Cust(null, 吴三, 17446541321, LocalDate.of(1998, 6, 12), 10.0);mapper.insertCust(cust);}set 标签 改
set语句可用于构建UPDATE语句的SET子句
mapper接口的代码
void updateCust(Cust cust);在对应的映射文件中
根据 id 更改名字 和 电话号码 update idupdateCust parameterTypeCustupdate custsetif testname ! null and name ! name #{name},/ifif testphone ! null and phone ! phone #{phone},/if/setwhere id #{id}/update测试类代码 Testpublic void set() {CustMapper mapper session.getMapper(CustMapper.class);Cust cust new Cust(10, 刘兆儿, 1459745610,null,1000.0);mapper.updateCust(cust);}控制台
foreach 标签 删
Foreach语句用于遍历集合或数组可以重复执行SQL语句
可能要删多个数据可以用数组array或 集合list
mapper接口的代码 void deleteCusts(int[] ids);在对应的映射文件中 delete iddeleteCusts parameterTypeintdelete from cust where id inforeach collectionarray itemno open( close) separator,#{no}/foreach/delete测试类代码 Testpublic void foreach() {CustMapper mapper session.getMapper(CustMapper.class);// idint [] nos {1,2,3,4,5};mapper.deleteCusts(nos);} 文章转载自: http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.yrbqy.cn.gov.cn.yrbqy.cn http://www.morning.bpwz.cn.gov.cn.bpwz.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.crfyr.cn.gov.cn.crfyr.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.lbhck.cn.gov.cn.lbhck.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.zgpgl.cn.gov.cn.zgpgl.cn