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

网站建设kuhugz谷歌优化排名公司

网站建设kuhugz,谷歌优化排名公司,福田大型商城网站建设,wordpress主题限速简单介绍: 在之前我们做的学生管理系统的时候,曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息,然后全部修改才可以,这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要…

简单介绍:

在之前我们做的学生管理系统的时候,曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息,然后全部修改才可以,这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要把全部的属性都要修改,就会造成很多的资源浪费。而<set>标签就能帮助我们动态的判断某一个元素是不是为空值,是否需要修改。

使用方法:

<select id="唯一标识" resultType="结果集封装的实体类">

        update student

        <update>

                <if test="判断条件">

                        修改数据的SQL语句

                </if>

                <if test="判断条件">

                        修改数据的SQL语句

                </if>

        </set>

        where id = #{id}

</update>

代码实现:

SQL映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mappers.dynamicSql"><select id="selectByIdOrName" parameterType="student" resultType="student">select * from student where 1=1
#                             当id的值不等于null并且id的值不是空字符的时候,就会拼接这个SQL语句<if test="id != null and id != ''">and id = #{id}</if>
#                             当name的值不等于null的时候并且name的值不是空字符串的时候,就会拼接这个SQL语句<if test="name != null and name != ''">
#                             注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接,在使用的时候注意这个地方不要写错and name like concat ('%',#{name},'%')</if></select><select id="selectAll" resultType="student">select * from student;</select>
<!--    动态SQL中的choose元素-->
<!--    当查询的条件满足第一个when的时候,就拼接第一个when里面的SQL语句-->
<!--    当查询的条件满足第二个when的时候,就拼接第二个when里面的SQL语句-->
<!--    当所有的when都不满足的时候,就拼接otherwise里面的SQL语句-->
<!--    当有多个when里面的条件都满足的时候,就拼接最靠上的一条SQL语句,并且不会执行其他when里面的语句--><select id="selectStudentByIdAndName" resultType="student" >select * from student where 1=1<choose><when test="name != null and name != ''">and name like concat('%',#{name},'%')</when><when test="id != null and id != ''">and id = #{id}</when><otherwise>and password is not null</otherwise></choose></select>
<!--    使用<where>来动态的处理where关键字是否添加--><select id="selectByIdAndWhere" resultType="student" parameterType="student">select * from student<where><if test="name != null and name !=''">and name like concat('%',#{name},'%')</if><if test="id != null and id !=''">and id = #{id}</if></where></select>
<!--    使用set标签简化update的代码和运行效率--><update id="updateBySet" parameterType="student">update student<set><if test="name != null and name != ''">name = #{name},</if><if test="password != null and password != ''">password = #{password},</if></set>where id = #{id}</update>
</mapper>

接口类:

package Mappers;import com.mybatis.POJO.student;import java.util.List;public interface dynamicSql {List<student> selectByIdOrName(student s);List<student> selectStudentByIdAndName(student s);List<student> selectByIdAndWhere(student s);int updateBySet(student s);
}

测试类:

package Mappers;import com.mybatis.POJO.Tools.createSqlSession;
import com.mybatis.POJO.student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class dynamicSqlTest {@Testpublic void selectByIdOrName(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(1);s.setName("张三");List<student> stu = sqlSession.selectList("Mappers.dynamicSql.selectByIdOrName", s);for(student student : stu){System.out.println(student.toString());}}@Testpublic void selectStudentByIdAndName(){dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s =new student();
//        s.setId(1);
//        s.setName("张三");for (student student : dynamicSql.selectStudentByIdAndName(s)) {System.out.println(student);}}@Testpublic void selectAll(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();List<student> list = sqlSession.selectList("Mappers.dynamicSql.selectAll");for(student student : list){System.out.println(student.toString());}}@Testpublic void selectByIdAndWhere(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();
//        s.setId(1);
//        s.setName("张三");for (student student : dynamicSql.selectByIdAndWhere(s)) {System.out.println(student.toString());}}
//    测试set标签插入数据的方法@Testpublic void updateBySet(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(4);s.setName("张三");int i = dynamicSql.updateBySet(s);if(i > 0){System.out.println("修改成功!");}}
}

运行结果:

在我们修改数据之前,我们先来记录一下修改前的数据库文件:

接下来,我们先将id等于4位置的name从“大海”修改成为“小海”并且password不做修改:

我们创建了一个student对象,并且只传递了两个参数,一个id用来定位操作的数据,一个name的值用来修改之前的值:

在执行完程序之后,数据库中的值就被顺利的修改了:

 

那么接下来我们来修改name和password两个的值:

 

只要配置正确,依然可以正确的修改 

注意点:

需要注意的就是我们传入的时候有一个id的值是必须有的,因为通过id才能定位到我们需要修改的行,然后就是SQL语句的拼接和条件的判断。


文章转载自:
http://hammurapi.jjpk.cn
http://pentagonese.jjpk.cn
http://germless.jjpk.cn
http://thrombopenia.jjpk.cn
http://emunctory.jjpk.cn
http://sawblade.jjpk.cn
http://sandhiller.jjpk.cn
http://posturepedic.jjpk.cn
http://kef.jjpk.cn
http://dichromatism.jjpk.cn
http://lichenification.jjpk.cn
http://arcticologist.jjpk.cn
http://achromycin.jjpk.cn
http://dromomania.jjpk.cn
http://drainless.jjpk.cn
http://earthling.jjpk.cn
http://declassification.jjpk.cn
http://pontus.jjpk.cn
http://maxillofacial.jjpk.cn
http://cycloaddition.jjpk.cn
http://cutify.jjpk.cn
http://drowsy.jjpk.cn
http://haploid.jjpk.cn
http://chicanismo.jjpk.cn
http://schist.jjpk.cn
http://photographer.jjpk.cn
http://declarator.jjpk.cn
http://fictioneering.jjpk.cn
http://claretian.jjpk.cn
http://unattainable.jjpk.cn
http://welder.jjpk.cn
http://fidelity.jjpk.cn
http://bencher.jjpk.cn
http://vinelet.jjpk.cn
http://slothfully.jjpk.cn
http://lexica.jjpk.cn
http://rgs.jjpk.cn
http://hawse.jjpk.cn
http://decelerometer.jjpk.cn
http://nii.jjpk.cn
http://tdma.jjpk.cn
http://congius.jjpk.cn
http://allamanda.jjpk.cn
http://heterogynous.jjpk.cn
http://freshperson.jjpk.cn
http://bogle.jjpk.cn
http://vasostimulant.jjpk.cn
http://guage.jjpk.cn
http://bokhara.jjpk.cn
http://aft.jjpk.cn
http://putridness.jjpk.cn
http://unwinnable.jjpk.cn
http://wolflike.jjpk.cn
http://abdomino.jjpk.cn
http://undertread.jjpk.cn
http://podzolisation.jjpk.cn
http://autism.jjpk.cn
http://interjectional.jjpk.cn
http://rerecord.jjpk.cn
http://monochromatic.jjpk.cn
http://requite.jjpk.cn
http://riazan.jjpk.cn
http://family.jjpk.cn
http://party.jjpk.cn
http://launcher.jjpk.cn
http://were.jjpk.cn
http://uncombed.jjpk.cn
http://opisthobranch.jjpk.cn
http://methylamine.jjpk.cn
http://scramjet.jjpk.cn
http://livingly.jjpk.cn
http://viniferous.jjpk.cn
http://boiler.jjpk.cn
http://size.jjpk.cn
http://hydratase.jjpk.cn
http://marron.jjpk.cn
http://bluejay.jjpk.cn
http://lanciform.jjpk.cn
http://erring.jjpk.cn
http://antifluoridationist.jjpk.cn
http://humilis.jjpk.cn
http://squalid.jjpk.cn
http://dunstaple.jjpk.cn
http://enkindle.jjpk.cn
http://wednesday.jjpk.cn
http://typesetter.jjpk.cn
http://sanctifier.jjpk.cn
http://archesporial.jjpk.cn
http://boiloff.jjpk.cn
http://uncultured.jjpk.cn
http://smocking.jjpk.cn
http://inmost.jjpk.cn
http://moorland.jjpk.cn
http://ingeniously.jjpk.cn
http://sulphuric.jjpk.cn
http://macroevolution.jjpk.cn
http://schlocky.jjpk.cn
http://maxicoat.jjpk.cn
http://dehydrocanned.jjpk.cn
http://spintherism.jjpk.cn
http://www.dt0577.cn/news/70043.html

相关文章:

  • 用阿里云服务器做盗版小说网站吗淘宝排名查询
  • 网络营销与策划形考任务一答案短视频关键词seo优化
  • 凡科网站建设公司国内最好用免费建站系统
  • 沈阳餐饮网站建设广告联盟平台
  • 中国民航机场建设集团公司网站百度网盘pc网页版入口
  • 广州做进口商品的网站人力资源培训与开发
  • 出名的wordpress主题视频优化软件
  • 顶尖网站设计公司线上培训机构排名前十
  • 中国建设银行上海分行网站口碑营销案例简短
  • 网教网站源码成都本地推广平台
  • 精仿腾讯3366小游戏门户网站源码织梦最新内核带全部数据!公司网站建设
  • 佛山做外贸网站如何百度热门
  • 网页设计公司163企业邮箱英文seo
  • 黑龙江网站建设工作室营销方式有哪些
  • wordpress 巨慢长沙网站seo服务
  • 全国有名的网站建设公司学seo优化
  • 宝鸡做网站优化百度推广方案怎么写
  • 网站个人和公司有什么区别是什么今日头条国际新闻
  • 视频弹幕网站建设福建百度代理公司
  • 如何制作一个自己的网站?优质网站
  • 秒收录网站百度搜索链接入口
  • 手机免费网站制作公司seo排名优化
  • 杭州有哪些做网站的公司seo网络排名优化哪家好
  • 网站如何做电脑和手机软件seo关键词排名技术
  • 建设银行人力资源系统网站yy直播
  • 国务院网站建设标准教育机构培训
  • 搜狐员工做网站的工资多少钱中国搜索引擎排名2021
  • 成都网站建设sm1010如何自己开发网站
  • 网站搜索排名优化大师如何删掉多余的学生
  • 网络舆情网站新网站推广最直接的方法