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

国家大宗商品交易平台优化培训课程

国家大宗商品交易平台,优化培训课程,网站建设杭州哪家便宜,网站色调设计方案动态sql介绍 由于在开发过程不同的业务中会用到不同的操作条件,如果每个业务都拼接不同sql语句的话会是一个庞大的工作量;此时动态sql就能解决这个问题,可以针对不确定的操作条件动态拼接sql语句,根据提交的条件来完成业务sql的执…

动态sql介绍

由于在开发过程不同的业务中会用到不同的操作条件,如果每个业务都拼接不同sql语句的话会是一个庞大的工作量;此时动态sql就能解决这个问题,可以针对不确定的操作条件动态拼接sql语句,根据提交的条件来完成业务sql的执行!

sql根标签

<insert>,<update>,<select>,<delete>

动态sql标签

<if>,<choose>,<when>,<otherwise>,<trim>,<foreach>,<where>,<set>,<bind>

关联关系标签

<collection>,<association>

sql根标签介绍

01<select>

<select id="getById" resultMap="orderMap"/>

02<insert>

<update id="update" parameterType="employee"/>

03<update>

<insert id="insert" puseGeneratedKeys="true" keyProperty="id"/>

04<delete>

<delete id="delete" parameterType="employee"/>

注意哦

parameterType与resultMap为查询返回结果,且不能同时存在,id不能重复,插入时候使用puseGeneratedKeys、keyProperty来保证主键生成

常用动态sql标签介绍

01<foreach>

//循环插入等场景使用foreach

<select id="selectIDInEids" parameterType="java.util.List" resultType="com.atguigu.ssm.pojo.Employee">

select * from employee where id in

<foreach collection="eids" item="eid" open="(" separator="," close=")">

${eid}

</foreach>

</select>

02<if>

//通常用来处理条件语句的判断

<select id="queryEmployeeIf" parameterType="employee" resultType="employee">

select * from employee where 1=1

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != '' ">

and gender like '%${gender}%'

</if>

</select>

03<choose><when><otherwise>

<select id="queryEmployeeChoose" parameterType="employee" resultType="employee">

select * from employee where 1=1

<choose>

<when test="empName != null and empName != ''">

and emp_name = #{empName}

</when>

<when test="gender != null and gender != '' ">

and gender like '%${gender}%'

</when>

<otherwise>

and id = 18

</otherwise>

</choose>

</select>

04<where>

// where标签标明,有符合条件则主动拼接条件;不需要额外拼接where 1=1

<select id="queryEmployeeWhere" parameterType="employee" resultType="employee">

select * from employee

<where>

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != ''">

and gender = #{gender}

</if>

</where>

</select>

05<trim>

// 效果与 where类似,前缀添加where,如果符合条件则提出默认and。

<select id="queryEmployeeTrim" parameterType="employee" resultType="employee">

select * from employee

<trim prefix="where" prefixOverrides="and" >

<if test="empName != null and empName != ''">

and emp_name = #{empName}

</if>

<if test="gender != null and gender != ''">

and gender = #{gender}

</if>

</trim>

</select>

06<set>

// set标签可以用来进行更新操作,mybatis操作更新只需要指定对应元素即可进行局部更新,显的更加灵活

<update id="updateEmployeeInfo" parameterType="employee" >

update employee

<set>

<if test="empName != null and empName != ''">

emp_name=#{empName},

</if>

<if test="gender != null">

gender=#{gender},

</if>

</set>

where id=#{id}

</update>

07<bind>

// 该标签主要为了给传递的参数添加特色标签而完成特殊的需求;

// 如模糊查询可以通过该标签添加%name%

<select id="queryEmployeeInfoByBind" parameterType="employee" resultType="employee">

<bind name="pattern" value="'%' + empName + '%'"/>

select * from student where empName like #{pattern}

</select>

注意哦

01字符串空值问题

字符串变量需要针对null值和空字符串判断哦;

02基本类型转换问题

针对基本的类型的变量如果实体与前台字段中存在空字符串会报错哦,所以实例变量最好使用其包装类作为基本类型的替换类型;

03xml解析&&问题

动态sql时解析时xml无法失败&&,需要替换成为and,表示并且。

关联关系标签介绍

01<association>

// 该标签专门用来映射一对一类型关联关系,如每个人都拥有自己的身份标识,且仅仅为一对一,不会存在多种的可能

<resultMap id="orderMap" type="com.atguigu.entity.Order">

<id column="order_id" property="orderId"></id>

<result column="order_name" property="orderName"></result>

<result column="customer_id" property="customerId"></result>

<association property="customer" javaType="com.atguigu.entity.Customer">

<id column="customer_id" property="customerId"></id>

<result column="customer_name" property="customerName"></result>

</association>

</resultMap>

02<collection>

//该标签则能完成一对多的关联关系,多对多则可以理解为俩次处理一对多即可

<resultMap id="customerMap" type="com.atguigu.entity.Customer">

<id property="customerId" column="customer_id"></id>

<result property="customerName" column="customer_name"></result>

<collection property="orderList" ofType="com.atguigu.entity.Order">

<id property="orderId" column="order_id"></id>

<result property="orderName" column="order_name"></result>

</collection>

</resultMap>

小结

总之,在您学完MyBatis中动态拼接sql技能后,针对不同业务场景,灵活运用动态标签即可达到您想要的结果!

http://www.shuangfujiaoyu.com/news/27389.html

相关文章:

  • 网站制作关键慧生活798app下载
  • 公司如何登录网站做就业登记百度推广登录网址
  • 创造力网站设计中国十大seo
  • 大前端Wordpress图片主题top沈阳seo关键字优化
  • 深圳网站开seo软件安卓版
  • 陕西网站建设技术方案百度爱采购推广一个月多少钱
  • 企业网站每年的费用seo推广排名公司
  • 网站建设ppt介绍互动营销的案例有哪些
  • 如果网站没有做icp备案网络营销属于什么专业类型
  • 网站建设后的优势软文广告经典案例800字
  • 网站建设的目标是什么正规的计算机培训机构
  • iis安装wordpress宁波seo服务推广
  • 动态网站开发考试卷子站长工具seo综合查询权重
  • 做淘宝客网站能赚到钱吗百度教育app
  • 会建设简单的网站可以赚钱吗新闻发稿软文推广
  • 专业的网站建设与优化百度广告客服电话
  • 做新闻网站需要什么证件app拉新渠道商
  • 农家乐网站免费模板网站建设定制
  • html中文模板优化公司组织架构
  • 旅游网页设计图无线网络优化
  • 用dw做的网站怎么发布营销自动化
  • 制作网站公司网址杭州优化外包
  • 做企业网站收费多少建立一个网站需要多少钱
  • 做数学题挣钱的网站it培训机构哪家好
  • 电商网站建设运营协议百度手机助手app免费下载
  • 手机wap网站cms源码龙华线上推广
  • 模板网站购买北京最新发布信息
  • 汉南做网站个人免费网上注册公司
  • 编程培训机构哪个靠谱西安关键词seo
  • 学做课件的网站网络媒体广告代理