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

网站登录系统企业网站推广有哪些方式

网站登录系统,企业网站推广有哪些方式,googleseo新手怎么做,怎么设置批发网站怎么做MyBatis的动态SQL 1、if标签的用法2、choose标签的用法3、where标签4、set标签5、trim的用法6、foreach标签7、bind标签 使用过JDBC或者是其他的ORM框架的开发者都知道,在很多操作中都需要去根据具体的条件进行SQL语句的拼接,并且在有些时候一些标点符号…

MyBatis的动态SQL

      • 1、if标签的用法
      • 2、choose标签的用法
      • 3、where标签
      • 4、set标签
      • 5、trim的用法
      • 6、foreach标签
      • 7、bind标签

使用过JDBC或者是其他的ORM框架的开发者都知道,在很多操作中都需要去根据具体的条件进行SQL语句的拼接,并且在有些时候一些标点符号、空格之类的东西会导致开发工作很难去进行。而MyBatis的动态SQL就为了解决这样的问题应用而生的。

在MyBatis3版本之前,使用动态的SQL需要使用非常多的标签,并且非常麻烦。但是随着MyBatis的不断发展,它提供了强大的OGNL(Object-Graph
Navigation Language)表达式语言,用着种语言来消除多余的标签。那么下面我们就来看看MyBatis提供的标签有哪些。

1、if标签的用法

if标签常被用在where语句中,用来判断某个参数值是否满足条件。当然后来随着越来越多的应用,它也会被用于update语句用来判断某个字段是否被更新,或者是在insert语句中用来判断某个字段是否有值要插入。

假设,现在有一个多条件查询的功能需要开发,这些条件都是需要组合查询的。遇到这样的需求的时候,就需要我们去编写一个SQL来完成条件的查询。那么这个时候问题来了,对于组合查询来讲,有些字段需要模糊查询、有些字段需要精准匹配。按照传统的写法,代码如下。

<select id="test" resultType="com.demo.bean.User">select id , name ,age, address from user where name like concat('%',#{username},'%') and age = #{age}
</select>  

这个时候执行这个语句,只有当我们同时输入了name和age两个参数的时候这个SQL才会被正确的执行。如果单纯的只提供一个参数的时候这个SQL就得不到我们想要的结果。这个时候我们就可以将上面的查询进行修改。

<select id="test" resultType="com.demo.bean.User">select id , name ,age, address from user where 1=1<if test="name!=null and name!=''">and name like concat('%',#{username},'%') </if><if test="age!=null and age!=''">and age = #{age}</if>
</select> 

这个时候,当我们执行这条语句的时候,如果name参数不传的话那么OGNL表达式就会判断是否为空,如果表达式为true那么就会将对应的条件语句拼接到SQL中,如果为false则不会将条件语句进行拼接。

这里需要注意的就是,在使用if标签的时候它有一个test的属性是用来编写OGNL表达式的,并且这个表达式可以支持任意字段的判断;而且判断条件只限于String类型的字段值是否存在;当有多个条件需要判断的时候,可以用and或者是or进行连接

当然上面提到的在update、insert语句中的使用方式也是类似的。

2、choose标签的用法

上面我们介绍了关于if标签的用法,但是if标签的用法中并没有提供一个if……else……类似的用法,那么如果想要实现这种逻辑操作,那么就需要用到 choose when otherwise标签了。

在一个choose标签中包含了两个子标签 when 和 otherwise ,并且一个choose中至少有一个when,可以有0个或者1个otherwise。

例如,在有些场景中有这样的操作,使用用户名、手机号、身份证号查询,这个时候就需要将这三个条件进行组合进行查询。也许这里会有人说,直接使用or语句进行连接不就可以了么?当然这样也是可以的。如果说是要优先使用用户名查询、其次是手机号、最后是身份证号。这样的需求如何来编写这个SQL呢?

<select id="test" resultType="com.demo.bean.User">select id , name ,age, address from user where 1=1<choose><when test="name!=null and name!=''">and name like concat('%',#{username},'%') </when><when test="age!=null and age!=''">and age = #{age}</when><otherwise>and 1=2</otherwise>    </choose>
</select> 

需要注意的是在使用这个逻辑语句的时候一定要 保证逻辑的紧密性,不然就会出现严重的SQL错误。在上面这个查询中,如果otherwise没有限制的话,那么所有满足条件的用户都会被查询出来。那么这个时候就会导致返回多条用户信息,但是在接收参数中只接受一条用户信息的情况出现。但是如果我们加上了条件之后,就会导致用户查询不到的情况发生,无论如何查询都无法查询到用户。

3、where标签

首先where标签的作用,就是如果这个标签中存在返回值,那么就会插入一个where条件语句,如果where后面的字符串是and或者是or开头的,那么就会将其剔除。这样的话,上面的标签就可以写成如下的样子。

<select id="test" resultType="com.demo.bean.User">select id , name ,age, address from user <where><if test="name!=null and name!=''">and name like concat('%',#{username},'%') </if><if test="age!=null and age!=''">and age = #{age}</if></where
</select> 

这个时候当where条件中的if没有一个满足的时候where标签中就没有内容,所以按照上面的说法,where标签就不会出现在查询语句中,也就不会出现条件不匹配的问题。如果if标签中有满足条件的语句那么where条件会自动的将这些条件进行拼接然后使用。这样我们就不需要再使用where 1=1 这样的操作了。

4、set标签

set标签的作用:就是如果这标签中包含了对应的返回值,那么就会插入一个set,如果后面是逗号结尾,就会剔除这个逗号。使用如下

<update id="test">update user <set><if test="name!=null and name!=''">name = #{username}, </if><if test="age!=null and age!=''">age = #{age},</if>id = #{id}</set>where id = #{id}
</update > 

在set标签的用法中,SQL后面的逗号会自动剔除,但是如果set标签中没有满足条件的内容,照样也会出现问题。为了避免这样的问题出现,对于update语句中的条件一定要出现根据某个条件进行更新操作,并且在更新的时候对应的值应该是不需要进行判断,默认就是被传入的。

5、trim的用法

与where和set标签一样,trim标签可以代替where和set来完成对应的操作,其底层实现就是通过TrimSqlNode来实现。

trim标签有如下的属性

  • prefix :内容增加prefix前缀
  • prefixOverrides:把内容前缀字符去掉
  • suffix:增加后缀
  • suffixOverrides:取消后缀

6、foreach标签

<foreach 标签主要用在构建 in 条件中,它可以在 SQL 语句中迭代一个集合。

<foreach 标签的属性主要有 item、index、collection、open、separator、close。

  • item 表示集合中每一个元素进行迭代时的别名。
  • index 指定一个名字,用于表示在迭代过程中每次迭代到的位置。
  • open 表示该语句以什么开始。
  • separator 表示在每次进行迭代之间以什么符号作为分隔符。
  • close 表示以什么结束。

在使用 元素时,最关键、最容易出错的是 collection 属性,该属性是必选的,但在不同情况下该属性的值是不一样的,主要有以下 3 种情况:

  • 如果传入的是单参数且参数类型是一个 List,collection 属性值为 list。
  • 如果传入的是单参数且参数类型是一个 array 数组,collection 的属性值为 array。
  • 如果传入的参数是多个,需要把它们封装成一个 Map,当然单参数也可以封装成 Map。Map 的 key 是参数名,collection 属性值是传入的 List 或 array 对象在自己封装的 Map 中的 key。

接下来针对上述三种情况进行演示:

(1)单参数且参数类型是List

在IStudentDao添加方法。

  List<Student> forEachTest1(List<Integer> list);

在studentMapper.xml中添加sql映射语句。

    <select id="forEachTest1" resultType="com.day1.entity.Student">select * from t_student where age in<foreach collection="list" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>

进行测试。

 @Testpublic void testForEach01() throws IOException {//1、读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapperConfig.xml");//2、创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3、使用工厂生产SqlSession对象SqlSession sqlSession = factory.openSession();//4、使用SqlSession创建dao接口的代理对象IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);//5、使用代理对象执行方案List<Integer> list = new ArrayList<>();list.add(21);list.add(43);list.add(19);List<Student> students = studentDao.forEachTest1(list);for(Student stu : students){System.out.println(stu);}//6、释放资源sqlSession.close();in.close();}

(2)单参数且参数类型是array

在IStudentDao添加方法。

    List<Student> forEachTest2(int[] ages);

在studentMapper.xml中添加sql映射语句。

<select id="forEachTest2" resultType="com.day1.entity.Student">select * from t_student where age in<foreach collection="array" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>

进行测试。

    @Testpublic void testForEach02() throws IOException {//1、读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapperConfig.xml");//2、创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3、使用工厂生产SqlSession对象SqlSession sqlSession = factory.openSession();//4、使用SqlSession创建dao接口的代理对象IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);//5、使用代理对象执行方案int[] ages = new int[]{19, 56};List<Student> students = studentDao.forEachTest2(ages);for(Student stu : students){System.out.println(stu);}//6、释放资源sqlSession.close();in.close();}

(3)封装map

在IStudentDao添加方法。

    List<Student> forEachTest3(Map<String, Object> map);

在studentMapper.xml中添加sql映射语句。

<select id="forEachTest3" resultType="com.day1.entity.Student">select * from t_student where age > #{age} or address in<foreach collection="locations" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></select>

进行测试。

    @Testpublic void testForEach03() throws IOException {//1、读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapperConfig.xml");//2、创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3、使用工厂生产SqlSession对象SqlSession sqlSession = factory.openSession();//4、使用SqlSession创建dao接口的代理对象IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);//5、使用代理对象执行方案List<String> locations = new ArrayList<>();locations.add("北京");locations.add("河北");locations.add("安徽");Map<String, Object> map = new HashMap<>();map.put("age", 30);map.put("locations", locations);List<Student> students = studentDao.forEachTest3(map);for(Student stu : students){System.out.println(stu);}//6、释放资源sqlSession.close();in.close();}

7、bind标签

通常在进行模糊查询时,如果使用“${}”拼接字符串,则无法防止 SQL 注入问题。如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同,MySQL 用的是的 concat 函数、Oracle 则是连接符号“||”,这样 SQL 映射文件就需要根据不同的数据库提供不同的实现,显然比较麻烦,且不利于代码的移植。MyBatis 提供了 元素来解决这一问题。

<bind标签可以使用OGNL表达式创建一个变量并将其绑定到上下文中。标签的两个属性都是必选项:name 为绑定到上下文的变量名;value 为 OGNL 表达式。

(1)在IStudentDao添加方法。

    List<Student> testBind(Student student);

(2)在studentMapper.xml中添加sql映射语句。

<!--使用bind元素进行模糊查询--><select id="testBind" resultType="com.day1.entity.Student" parameterType= "com.day1.entity.Student"><!-- bind 中的 username 是 com.day1.entity.Student 的属性名--><bind name="username" value="'%' + username + '%'"/>select * from t_student where username like #{username}</select>

(3)进行测试。

    @Testpublic void testBind() throws IOException {//1、读取配置文件InputStream in = Resources.getResourceAsStream("SqlMapperConfig.xml");//2、创建SqlSessionFactory工厂SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();SqlSessionFactory factory = builder.build(in);//3、使用工厂生产SqlSession对象SqlSession sqlSession = factory.openSession();//4、使用SqlSession创建dao接口的代理对象IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);//5、使用代理对象执行方案Student student = new Student();student.setUsername("王");List<Student> students = studentDao.testBind(student);for(Student stu : students){System.out.println(stu);}//6、释放资源sqlSession.close();in.close();}

文章转载自:
http://dinncoantigravity.bpmz.cn
http://dinncophimosis.bpmz.cn
http://dinncohype.bpmz.cn
http://dinncoursuline.bpmz.cn
http://dinncodemonolatry.bpmz.cn
http://dinncosoftbank.bpmz.cn
http://dinncopostal.bpmz.cn
http://dinncosabreur.bpmz.cn
http://dinncostraightness.bpmz.cn
http://dinncoford.bpmz.cn
http://dinncounprimitive.bpmz.cn
http://dinncochloe.bpmz.cn
http://dinncooos.bpmz.cn
http://dinncobefore.bpmz.cn
http://dinncoassert.bpmz.cn
http://dinncoassuring.bpmz.cn
http://dinncodrake.bpmz.cn
http://dinncoquatrain.bpmz.cn
http://dinncoforrader.bpmz.cn
http://dinncogruesome.bpmz.cn
http://dinncohyperglycaemia.bpmz.cn
http://dinncomyrmecophile.bpmz.cn
http://dinncoenigmatic.bpmz.cn
http://dinncomagnetotactic.bpmz.cn
http://dinncooxyparaffin.bpmz.cn
http://dinncomirabilite.bpmz.cn
http://dinncotapotement.bpmz.cn
http://dinncophotoconductive.bpmz.cn
http://dinncopolyangular.bpmz.cn
http://dinncoforeseeingly.bpmz.cn
http://dinncovilely.bpmz.cn
http://dinncodraftee.bpmz.cn
http://dinncothecate.bpmz.cn
http://dinncozeugmatography.bpmz.cn
http://dinncoyon.bpmz.cn
http://dinncoexoatmosphere.bpmz.cn
http://dinncoxat.bpmz.cn
http://dinncobardic.bpmz.cn
http://dinncotoddler.bpmz.cn
http://dinncomarocain.bpmz.cn
http://dinnconosy.bpmz.cn
http://dinncoinfuscate.bpmz.cn
http://dinncofingo.bpmz.cn
http://dinncowort.bpmz.cn
http://dinncounoffending.bpmz.cn
http://dinncoerogenous.bpmz.cn
http://dinncoproductive.bpmz.cn
http://dinncopenang.bpmz.cn
http://dinncotoxicologist.bpmz.cn
http://dinncoopsonic.bpmz.cn
http://dinncocycloidal.bpmz.cn
http://dinncoabirritate.bpmz.cn
http://dinncoconvincible.bpmz.cn
http://dinncocollarwork.bpmz.cn
http://dinncotiu.bpmz.cn
http://dinncophotophoresis.bpmz.cn
http://dinncoequipotent.bpmz.cn
http://dinncopolytheist.bpmz.cn
http://dinncohud.bpmz.cn
http://dinncoadmiralty.bpmz.cn
http://dinncowastepaper.bpmz.cn
http://dinncocdrom.bpmz.cn
http://dinncolabanotation.bpmz.cn
http://dinncobooted.bpmz.cn
http://dinncobaldicoot.bpmz.cn
http://dinncotalent.bpmz.cn
http://dinncoallusive.bpmz.cn
http://dinncocorrie.bpmz.cn
http://dinncobenthograph.bpmz.cn
http://dinncoglyph.bpmz.cn
http://dinncoramequin.bpmz.cn
http://dinncohamah.bpmz.cn
http://dinncokopeck.bpmz.cn
http://dinncocyanobacterium.bpmz.cn
http://dinncohistological.bpmz.cn
http://dinncocataplastic.bpmz.cn
http://dinncodemonstration.bpmz.cn
http://dinncogalloper.bpmz.cn
http://dinncoexcommunication.bpmz.cn
http://dinncofunction.bpmz.cn
http://dinncopedometer.bpmz.cn
http://dinncobackdown.bpmz.cn
http://dinncoaeonian.bpmz.cn
http://dinncoachlorhydria.bpmz.cn
http://dinncoreigning.bpmz.cn
http://dinncoscopolamine.bpmz.cn
http://dinncofiver.bpmz.cn
http://dinncodrove.bpmz.cn
http://dinncofayum.bpmz.cn
http://dinncofrostily.bpmz.cn
http://dinnconativist.bpmz.cn
http://dinncokipper.bpmz.cn
http://dinncogull.bpmz.cn
http://dinncohyperphysically.bpmz.cn
http://dinncosugarbush.bpmz.cn
http://dinncosubbass.bpmz.cn
http://dinncodisulfuram.bpmz.cn
http://dinncotzarevitch.bpmz.cn
http://dinncorummery.bpmz.cn
http://dinncoann.bpmz.cn
http://www.dinnco.com/news/131788.html

相关文章:

  • 做网站需要哪些东西和步骤自动提取关键词的软件
  • 国外网站兼职做效果图黑帽seo排名技术
  • 罗湖做网站的公司哪家好seo营销论文
  • 哪个企业的网站做的比较好搜客
  • 沈阳网站建设公司的公司实时排名软件
  • 中文域名注册网站阿里云域名查询
  • 珠海模板建站公司网站制作推广电话
  • 网站建设是设计师吗北京网站优化对策
  • 网站如何做快捷支付北京网络营销推广培训哪家好
  • 用ul做的网站为何浮动不上去搜索引擎优化案例
  • 从零做网站百度网页版登录入口官网
  • 有没有做高仿手表的网站网络营销的盈利模式
  • 百度宿迁市建设局网站淘宝流量平台
  • 新闻wordpress主题一个企业seo网站的优化流程
  • 创意营销案例seo兼职招聘
  • 四川有那些网站建设公司关联词有哪些四年级
  • 企业所得税税率5% 10% 25%自动seo优化
  • 政府网站建设 托管百度快照官网登录
  • 前端asp网站开发亚马逊站外推广网站
  • 一个正规的网站建设公司惠州关键词排名提升
  • 厦门网站建设找哪家比较好济南seo怎么优化
  • 电商网站设计公司有哪些陕西网站建设制作
  • 学校网站做网页飘窗怎么做广告营销包括哪些方面
  • wordpress shopping网站seo培训
  • 电子商务怎样建立网站的微信营销推广的方式有哪些
  • 上海做网站 公司有哪些平台可以做推广
  • 做网站推广 seo的竞价排名是按照什么来计费的
  • 浙江省网站集约化建设通知seo新站如何快速排名
  • 制作企业网站软件html网页制作用什么软件
  • 淘宝客服推销做网站的技巧关键词排名优化官网