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

宝安商城网站建设哪家效益快做网站只用前端知识可以吗

宝安商城网站建设哪家效益快,做网站只用前端知识可以吗,潍坊市住房和城乡建设局网站,大航母网站建设与运营#x1f9f8;安清h#xff1a;个人主页 #x1f3a5;个人专栏#xff1a;【计算机网络】 #x1f6a6;作者简介#xff1a;一个有趣爱睡觉的intp#xff0c;期待和更多人分享自己所学知识的真诚大学生。 文章目录 #x1f3af;一.动态SQL简单介绍 #x1f6a6;动态S… 安清h个人主页 个人专栏【计算机网络】 作者简介一个有趣爱睡觉的intp期待和更多人分享自己所学知识的真诚大学生。 文章目录 一.动态SQL简单介绍 动态SQL的基本概念 二.条件查询操作 数据库准备 POJO类准备 创建映射文件元素 修改核心配置文件 创建MybatisUtil工具类 创建接口类  修改测试类 ✨,,元素 ✨更新操作  ✨复杂查询操作  元素简单介绍 元素迭代List 元素迭代数组 元素迭代Map 总结 一.动态SQL简单介绍 动态SQL是MyBatis框架中一个非常强大的特性它允许开发者在构建SQL语句时根据条件动态地生成不同的SQL片段。这样做的好处是可以避免硬编码查询逻辑简化数据库查询的复杂度同时提高代码的可读性和维护性。 动态SQL的基本概念 动态SQL并不是一个新的概念它指的是在运行时根据条件构建SQL语句而不是使用静态的SQL语句。MyBatis通过一系列的动态SQL标签来实现这一功能这些标签包括 if根据条件动态拼接SQL。choose、when、otherwise类似于Java中的switch-case语句。trim、where、set用于处理SQL语句的不同部分如自动添加WHERE并去除多余的AND。foreach用于处理集合生成IN查询。 二.条件查询操作 数据库准备 在数据库mybatis下创建一个customer表并向其中插入几条数据代码如下 create table customer(id int(32) primary key auto_increment,username varchar(50),jobs varchar(50),phone varchar(16) );insert into customer values (1,joy,teacher,122222222); insert into customer values (2,jack,teacher,133333333); insert into customer values (3,tom,worker,1267567567); POJO类准备 一般放在pojo包里这里我直接在java包中建立了类中声明id,username,jobs,phone属性以及属性相对应get/set方法。 public class Customer {private Integer id;private String username;private String jobs;private String phone;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getJobs() {return jobs;}public void setJobs(String jobs) {this.jobs jobs;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone phone;}Overridepublic String toString() {return Customer{ id id , username username \ , jobs jobs \ , phone phone \ };} }创建映射文件if元素 if元素中的test属性多用于条件判断语句中用于判断真假在此处我们对用户姓名和工作都做了非空判断如果传入的查询条件非空就进行动态SQL组装。 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceCustomerMapperselect idQueryByNameAndJobs parameterTypeCustomer resultTypeCustomerselect * from customerwhere 11if testusername!null and username!and username like concat(%,#{username},%)/ifif testjobs!null and jobs!and jobs#{jobs}/if/select /mapper 修改核心配置文件 在核心配置文件mybatis-config.xml中引入CustomerMapper.xml映射文件代码如下 ?xml version1.0 encodingUTF-8 ? !DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd !-- 配置文件的根元素 -- configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql:///mybatis?characterEncodingutf-8/property nameusername valueroot/property namepassword valueroot//dataSource/environment/environmentsmappersmapper resourceCustomerMapper.xml//mappers /configuration 创建MybatisUtil工具类 这段代码的目的是为了封装MyBatis的初始化过程并提供一个全局访问点来获取SqlSession使得在应用的其他部分可以很方便地使用MyBatis进行数据库操作而不需要关心SqlSessionFactory的创建和配置细节。这样做可以减少代码重复提高代码的可维护性。 public class MybatisUtil {private static SqlSessionFactory sqlSessionFactorynull;static {try {Reader reader Resources.getResourceAsReader(mybatis-config.xml);sqlSessionFactorynew SqlSessionFactoryBuilder().build(reader);} catch (IOException e) {throw new RuntimeException(e);}}public static SqlSession getSession(){return sqlSessionFactory.openSession();} } 创建接口类  public interface CustomerMapper {ListCustomer QueryByNameAndJobs(Customer customer); } 修改测试类 在测试类MybatisTest中编写测试方法testQuery该方法用于通过姓名和工作查询客户信息。 public class MybatisTest {Testpublic void testQuery(){SqlSession sqlSessionMybatisUtil.getSession();CustomerMapper customerMappersqlSession.getMapper(CustomerMapper.class);Customer customernew Customer();customer.setUsername(jack);customer.setJobs(teacher);ListCustomer listcustomerMapper.QueryByNameAndJobs(customer);for(Customer c:list){System.out.println(c);}sqlSession.close();} } ✨choose,when,otherwise元素 在MyBatis的动态SQL中choose, when, otherwise元素组合用于条件分支选择类似于Java中的if-else或switch语句。这些元素允许在SQL语句中根据不同的条件执行不同的SQL片段。 以下是这些元素的基本用法 choose元素表示一个条件选择块的开始它本身不生成任何SQL语句。when元素表示一个条件分支它内部包含一个test属性该属性用于指定条件表达式。如果test属性中的表达式计算为true则该分支内的SQL会被包含在最终的SQL语句中。otherwise元素表示在所有when条件都不满足时执行的分支。它类似于switch语句中的default分支。 1在映射文件CustomerMapper.xml中添加使用 choose,when,otherwise实现以下场景 在客户名称不为空时只根据客户名称查找。客户名称为空客户职业不为空时只根据客户职业查找。客户名称和客户职业都为空时查询出所有电话不为空的客户信息。 ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceCustomerMapperselect idfindByWhere parameterTypeCustomer resultTypeCustomerselect * from customer where 11choosewhen testusername!null and username!and username like concat(%,#{username},%)/whenwhen testjobs!null and jobs!and jobs#{jobs}/whenotherwiseand phone is not null/otherwise/choose/select /mapper 上述使用choose元素进行SQL拼接当第一个when元素中的条件为真时只动态组装第一个when元素内的SQL片段并执行否则就继续向下判断第二个when元素中的条件是否为真以此类推直到某个when元素中的条件为真结束判断。当前面所有的when元素中的条件都不为真时则动态组装otherwise元素内的SQL片段并执行。 2在测试类MybatisTest中编写测试方法findByWhere()具体代码如下 public class MybatisTest {Testpublic void findByWhere(){SqlSession sqlSessionMybatisUtil.getSession();CustomerMapper customerMappersqlSession.getMapper(CustomerMapper.class);Customer customernew Customer();customer.setUsername(jack);customer.setJobs(teacher);ListCustomer listcustomerMapper.findByWhere(customer);for(Customer c:list){System.out.println(c);}sqlSession.close();} } 不同的查询结果如下 1.客户姓名不为空时 2.客户姓名为空客户职业不为空时 3.客户姓名和客户职业都为空时 ✨更新操作  在MyBatis中set标签用于构建动态SQL语句中的UPDATE操作它允许根据条件动态地更新表中的列。set标签会自动地为你插入的每个列添加逗号分隔并且会忽略空格使得构建动态更新语句更加方便。 set标签通常与if标签结合使用以便在运行时根据条件动态地构建更新的列和值。以下是一个基本的示例 1在映射文件CustomerMapper.xml中使用set元素执行更新操作的动态SQL update idupdateCustomerBySet parameterTypeCustomerupdate customersetif testusername!null and username!username#{username},/ifif testjobs!null and jobs!jobs#{jobs}/ifif testphone!null and phone!phone#{phone}/if/setwhere id#{id}/update (2)在CustomerMapper接口中添加如下操作 public interface CustomerMapper {int updateCustomerBySet(Customer customer); } 3在测试类MybatisTest中编写测试方法testUpdate()具体实现代码如下 Testpublic void testUpdate(){SqlSession sqlSessionMybatisUtil.getSession();CustomerMapper customerMappersqlSession.getMapper(CustomerMapper.class);Customer customernew Customer();customer.setId(3);customer.setPhone(123456789);int rowscustomerMapper.updateCustomerBySet(customer);if(rows0){System.out.println(您修改成功了rows条数据);}else{System.out.println(您修改失败了);}sqlSession.commit();sqlSession.close();} 4修改成功后就可以看到 在表中的数据如下图 ✨复杂查询操作  foreach元素简单介绍 在MyBatis中foreach标签用于遍历集合常用于构建IN条件子句或批量操作如批量插入、更新、删除。foreach标签可以处理集合或数组类型的参数为每个元素生成SQL片段并将这些片段组合起来。 属性描述 collection 指定要遍历的集合或数组 item 指定集合中每个元素的别名可以在遍历块内部使用 index 指定集合中每个元素的索引或键的别名可以在遍历块内部使用 open 指定遍历输出的开始符号 close 指定遍历输出的结束符号 separator 指定遍历元素之间的分隔符 nullable 指定是否允许collection为空值   foreach元素迭代List 1在映射文件CustomerMapper.xml中添加使用foreach元素迭代List执行批量查询操作具体代码如下 select idfindByArray resultTypeCustomerselect * from Customer where id inforeach itemid collectionlist open( separator, close)#{id}/foreach/select 2在测试类MybatisTest中编写测试方法testforeach()具体实现代码如下 Testpublic void testforeach(){SqlSession sqlSessionMybatisUtil.getSession();Customer customernew Customer();ListInteger ids new ArrayListInteger();ids.add(2);ids.add(3);ListCustomer listsqlSession.selectList(CustomerMapper.findByArray,ids);for(Customer c:list){System.out.println(c);}sqlSession.close();} 3在接口CustomerMapper 中添加以下代码 ListCustomer findByArray(String customerMapper, ListInteger ids); 4查询结果如下图 foreach元素迭代数组 1在映射文件CustomerMapper.xml中添加使用foreach元素迭代数组执行批量查询操作具体代码如下 select idfindByList resultTypeCustomerselect * from Customer where id inforeach itemid collectionlist open( separator, close)#{id}/foreach/select 2在测试类MybatisTest中编写测试方法testforeach()具体实现代码如下 Testpublic void testforeach(){SqlSession sqlSessionMybatisUtil.getSession();Customer customernew Customer();Integer[] ids{1,2};ListCustomer listsqlSession.selectList(CustomerMapper.findByList,ids);for(Customer c:list){System.out.println(c);}sqlSession.close();} foreach元素迭代Map 由于Mybatis传入参数均为一个参数如果传入参数为多个参数例如查询出性别为男性且职业为教师的所有客户信息此时需要把这些参数封装成一个Map集合进行处理。 1在映射文件CustomerMapper.xml中添加使用foreach元素迭代Map执行批量查询操作具体代码如下 select idfindByMap parameterTypejava.util.Map resultTypeCustomerselect * from customer where jobs#{jobs} and id inforeach itemroleMap indexindex collectionid open( separator, close)#{roleMap}/foreach/select 2在测试类MybatisTest中编写测试方法testforeach()具体实现代码如下 Testpublic void testforeach(){SqlSession sqlSessionMybatisUtil.getSession();ListInteger idsnew ArrayListInteger();ids.add(1);ids.add(2);ids.add(3);MapString,Object mapnew HashMapString, Object();map.put(id,ids);map.put(jobs,teacher);ListCustomer listsqlSession.selectList(CustomerMapper.findByMap,map);for(Customer c:list){System.out.println(c);}sqlSession.close();} 总结 以上就是今天要讲的内容了主要在if,choose,when,otherwise,foreach方面做了重点的讲解非常感谢您的阅读如果这篇文章对您有帮助那将是我的荣幸。我们下期再见啦
http://www.hkea.cn/news/14437812/

相关文章:

  • wordpress网站基础知识山东省建设安全监督站的网站
  • 广州网站建设推广专家团队wordpress资讯模板
  • 上海闵行网站建设线上外包平台
  • dedecms景区网站模板网页设计模板的结构
  • 咸阳商城网站开发设计杭州做网站一般多少钱
  • 做公众号编辑用什么网站辽宁电力建设监理有限公司网站
  • 怎么建网站app那种电影网站怎么建设
  • 依安县建设网站糕点网站设计
  • 律师行业做网站的必要性中轻成都设计院
  • 做纸箱在什么网站找客户域名申请好后 如何建设网站
  • 市桥网站建设培训哪里建设网站
  • 正规代加工项目提供邢台网站优化
  • 一般网站有哪些模块做视频付费网站
  • 贸易公司广告网站东圃手机网站建设电话
  • 东营seo网站推广费用英文网站seo
  • 织梦网站添加视频教程垂直+网站开发
  • 怎么做简单网站国外云服务器厂商
  • 做网站是什么会计科目义乌 网站制作
  • 响应式网站建设服务商商城开发方案
  • 做网站还是做游戏公司做的网站访问很慢
  • 网站建设get你有哪些可以在线做app的网站有哪些问题
  • 论坛的网站制作做电商必备的八个软件
  • 网站关键词排名优化价格网络广告策划
  • 网站建设准备资料表格pyhton可以做网站吗
  • 潍坊网站制作人才招聘临沂经开区建设局网站
  • 可以找网图的软件长沙网站seo技术厂家
  • 精通网站建设 100%全能建站密码pdf常德做网站报价
  • 想开一个网站开发的公司wordpress编辑空两格
  • 主视觉设计网站ppt软件下载免费版
  • 国内免费图片素材网站快手直播间挂人气自助网站