开发个网站多少钱,广州网站建设推广公司哪家好,简历旅游网站开发经验,未来产品设计上一篇我们介绍了在Mybatis映射器中使用SelectProvider、InsertProvider、UpdateProvider、DeleteProvider进行对数据的增删改查操作#xff1b;本篇我们介绍如何使用SQL构建器在Provider中优雅的构建SQL语句。
如果您对在Mybatis映射器中使用SelectProvider、InsertProvider…上一篇我们介绍了在Mybatis映射器中使用SelectProvider、InsertProvider、UpdateProvider、DeleteProvider进行对数据的增删改查操作本篇我们介绍如何使用SQL构建器在Provider中优雅的构建SQL语句。
如果您对在Mybatis映射器中使用SelectProvider、InsertProvider、UpdateProvider、DeleteProvider进行对数据的增删改查操作不太了解建议您先进行了解后再阅读本篇可以参考
Mybatis 映射器中使用InsertProvider,UpdateProvider,DeleteProvider,SelectProviderhttps://blog.csdn.net/m1729339749/article/details/133122304
一、数据准备
这里我们直接使用脚本初始化数据库中的数据
-- 如果数据库不存在则创建数据库
CREATE DATABASE IF NOT EXISTS demo DEFAULT CHARSET utf8;
-- 切换数据库
USE demo;
-- 创建用户表
CREATE TABLE IF NOT EXISTS T_USER(ID INT PRIMARY KEY,USERNAME VARCHAR(32) NOT NULL,AGE INT NOT NULL
);
-- 插入用户数据
INSERT INTO T_USER(ID, USERNAME, AGE)
VALUES(1, 张三, 20),(2, 李四, 22),(3, 王五, 24);创建了一个名称为demo的数据库并在库里创建了名称为T_USER的用户表并向表中插入了数据
二、创建实体类
在cn.horse.demo下创建UserInfo、UserInfoQuery类
UserInfo类
package cn.horse.demo;public class UserInfo {private Integer id;private String name;private Integer age;public void setId(Integer id) {this.id id;}public Integer getId() {return id;}public void setName(String name) {this.name name;}public String getName() {return name;}public void setAge(Integer age) {this.age age;}public Integer getAge() {return age;}Overridepublic String toString() {StringBuilder stringBuilder new StringBuilder();stringBuilder.append({);stringBuilder.append(id: this.id);stringBuilder.append(, );stringBuilder.append(name: this.name);stringBuilder.append(, );stringBuilder.append(age: this.age);stringBuilder.append(});return stringBuilder.toString();}
}UserInfoQuery类
package cn.horse.demo;public class UserInfoQuery {private Integer startAge;private Integer endAge;public void setStartAge(Integer startAge) {this.startAge startAge;}public Integer getStartAge() {return startAge;}public void setEndAge(Integer endAge) {this.endAge endAge;}public Integer getEndAge() {return endAge;}
}三、创建UserInfoMapper映射器、UserInfoSqlProvider类
在cn.horse.demo下创建UserInfoMapper接口、UserInfoSqlProvider类
UserInfoMapper接口
package cn.horse.demo;import org.apache.ibatis.annotations.*;import java.util.List;public interface UserInfoMapper {SelectProvider(type UserInfoSqlProvider.class, method select)ListUserInfo find(Param(query) UserInfoQuery query);InsertProvider(type UserInfoSqlProvider.class, method insert)Integer insert(Param(userInfo) UserInfo userInfo);UpdateProvider(type UserInfoSqlProvider.class, method update)Integer update(Param(userInfo) UserInfo userInfo);DeleteProvider(type UserInfoSqlProvider.class, method delete)Integer delete(Param(id) Integer id);
}在UserInfoMapper接口中我们把查询、新增、修改、删除数据的SQL语句的构建委托给了UserInfoSqlProvider类中的select、insert、update、delete方法
UserInfoSqlProvider类
package cn.horse.demo;import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;import java.util.*;public class UserInfoSqlProvider {public String select(Param(query) UserInfoQuery query) {SQL sql new SQL().SELECT(ID, USERNAME name, AGE).FROM(T_USER);if(Objects.isNull(query)) {return sql.toString();}if(Objects.nonNull(query.getStartAge())) {sql.WHERE(AGE #{query.startAge});}if(Objects.nonNull(query.getEndAge())) {sql.WHERE(AGE #{query.endAge});}return sql.toString();}public String insert(Param(userInfo) UserInfo userInfo) {return new SQL().INSERT_INTO(T_USER).VALUES(ID, USERNAME, AGE, #{userInfo.id},#{userInfo.name},#{userInfo.age}).toString();}public String update(Param(userInfo) UserInfo userInfo) {SQL sql new SQL().UPDATE(T_USER);sql.SET(ID #{userInfo.id});if(Objects.nonNull(userInfo.getName())) {sql.SET(USERNAME #{userInfo.name});}if(Objects.nonNull(userInfo.getAge())) {sql.SET(AGE #{userInfo.age});}sql.WHERE(ID #{userInfo.id});return sql.toString();}public String delete(Param(id) Integer id) {return new SQL().DELETE_FROM(T_USER).WHERE(ID #{id}).toString();}
}在UserInfoSqlProvider类的select、insert、update、delete方法中我们使用SQL类进行构建SQL语句其提供了一些常用的方法来帮助我们优雅的构建SQL语句。
四、引入配置文件
在resources下新建mybatis-config.xml配置文件并引入UserInfoMapper映射器。
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationsettingssetting namelogImpl valueJDK_LOGGING//settingsenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valueorg.gjt.mm.mysql.Driver/property nameurl valuejdbc:mysql://localhost:3306/demo?useUnicodetrueamp;useSSLfalseamp;characterEncodingutf8/property nameusername valueroot/property namepassword valuehorse//dataSource/environment/environmentsmappersmapper classcn.horse.demo.UserInfoMapper //mappers
/configuration这里我们使用mapper引入映射器只需要设置class属性为UserInfoMapper接口的全限类名。
五、启动程序配置
1、会话工具类
在cn.horse.demo包下新建SqlSessionUtils工具类
package cn.horse.demo;import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;
import java.util.Objects;public class SqlSessionUtils {private static final SqlSessionFactory sqlSessionFactory;static {// 读取mybatis配置文件InputStream inputStream ClassLoader.getSystemClassLoader().getResourceAsStream(mybatis-config.xml);// 根据配置创建SqlSession工厂sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);}/*** 开启会话* return*/public static SqlSession openSession() {return sqlSessionFactory.openSession();}/*** 关闭会话* param sqlSession*/public static void closeSession(SqlSession sqlSession) {if(Objects.nonNull(sqlSession)) {sqlSession.close();}}
}2、JDK 日志系统配置
在resources的目录下新建logging.properties配置文件
handlersjava.util.logging.ConsoleHandler
.levelINFOcn.horse.demo.UserInfoMapper.levelFINER
java.util.logging.ConsoleHandler.levelALL
java.util.logging.ConsoleHandler.formatterjava.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format%1$tY-%1$tm-%1$td %1$tT.%1$tL %4$s %3$s - %5$s%6$s%n在cn.horse.demo下创建JdkLogConfig类
JdkLogConfig类
package cn.horse.demo;import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;public class JdkLogConfig {public JdkLogConfig() {try {InputStream inputStream ClassLoader.getSystemClassLoader().getResourceAsStream(logging.properties);LogManager.getLogManager().readConfiguration(inputStream);} catch (IOException e) {throw new RuntimeException(e);}}
}3、启动程序配置
package cn.horse.demo;import org.apache.ibatis.session.SqlSession;import java.util.List;
import java.util.function.Consumer;public class Main {public static void main(String[] args) {// 引入JDK日志配置System.setProperty(java.util.logging.config.class, cn.horse.demo.JdkLogConfig);}private static void execute(ConsumerUserInfoMapper function) {SqlSession sqlSession null;try {sqlSession SqlSessionUtils.openSession();function.accept(sqlSession.getMapper(UserInfoMapper.class));sqlSession.commit();} finally {SqlSessionUtils.closeSession(sqlSession);}}
}execute方法用于执行操作方法中使用sqlSession.getMapper方法获取映射器对象然后将映射器对象具体的执行操作委托给了Consumer对象。
六、查询数据
// 引入JDK日志配置
System.setProperty(java.util.logging.config.class, cn.horse.demo.JdkLogConfig);// 查询
execute((UserInfoMapper userInfoMapper) - {UserInfoQuery query new UserInfoQuery();query.setEndAge(20);ListUserInfo userInfoList userInfoMapper.find(query);for (UserInfo userInfo: userInfoList) {System.out.println(userInfo);}
});执行后的结果如下 七、新增数据
// 引入JDK日志配置
System.setProperty(java.util.logging.config.class, cn.horse.demo.JdkLogConfig);// 插入
execute((UserInfoMapper userInfoMapper) - {UserInfo userInfo new UserInfo();userInfo.setId(5);userInfo.setName(王五1);userInfo.setAge(5);Integer total userInfoMapper.insert(userInfo);System.out.println(插入条数: total);
});执行后的结果如下 八、修改数据
// 引入JDK日志配置
System.setProperty(java.util.logging.config.class, cn.horse.demo.JdkLogConfig);// 更新
execute((UserInfoMapper userInfoMapper) - {UserInfo userInfo new UserInfo();userInfo.setId(5);userInfo.setName(王五11);Integer total userInfoMapper.update(userInfo);System.out.println(更新条数: total);
});执行后的结果如下 九、删除数据
// 引入JDK日志配置
System.setProperty(java.util.logging.config.class, cn.horse.demo.JdkLogConfig);// 删除
execute((UserInfoMapper userInfoMapper) - {Integer total userInfoMapper.delete(5);System.out.println(删除条数: total);
});执行后的结果如下
文章转载自: http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.webife.com.gov.cn.webife.com http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.kpcjl.cn.gov.cn.kpcjl.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.knnc.cn.gov.cn.knnc.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.lgmty.cn.gov.cn.lgmty.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.xqffq.cn.gov.cn.xqffq.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.prplf.cn.gov.cn.prplf.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.llxyf.cn.gov.cn.llxyf.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.cprbp.cn.gov.cn.cprbp.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.jlthz.cn.gov.cn.jlthz.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn