Java中使用Springboot和Mybatis框架中,Mapper.xml对SQL语句的用法

本文详细介绍了在Java开发中如何通过ORM映射进行数据库查询、插入操作,包括参数绑定、模糊查询、多条件查询、嵌套查询以及使用`insert-select`等高级技巧,涉及Oracle特定的数据类型和SQL语法。
摘要由CSDN通过智能技术生成
1.查询数据时,将实体类变量与数据库中对应
<sql id="colum">
    USER_NAME as userName,
    USER_AGE as userAge
</sql>
Select <include refid =”colum”/> from userinfo
 2.将插入后的数据id映射到实体类上
<insert id = "insertUser">
    <selectKey keyProperty=”id” resultType = “int”order = “BEFORE”>
        (select nvl(max(ID),0) + 1 as ID from user t)
    </selectKey >
    insert into user(name) values( #{name} )
</insert>
3.for循环添加

Mapper.Java中

    int inserUser( @Param("userList") List<User> userList);

Mapper.XML中

<insert id="insertUser" parameterType="java.util.List">
insert all
    	<foreach collection = "userList" index = "index" item = "item">
        	into user values( #{item.userId} )
    </foreach>
    select * from dual
</insert>
4. 模糊查询

Mapper.Java中

List<User> getUserInfo(@Param("name") String name);

Mapper.XML中

<select id="getUserInfo">
    <bind name="userName" value="'%' + name + '%'"/>
    select * from user where name like #{userName}
</select>
5.多嵌套查询
<resultMap id = “getUser” type = “对应的实体类”>
    <result column = “数据库的名称” property = “实体类的名称”/>
    <collection property="featIds" select="getFeatId" 	column="{type = type, userName = userName}"/>
<resultMap/>
 
<select id = “getUserInfo” resultMap = “getUser”/>
<select id = “getFeatId” resultType = “对应的实体类”/>
6.大于号小于号

大于号 &gt;   小于号  &lt;

7. Oracle对应的数据类型

 jdbcType=NUMERIC   (数字)     

 jdbcType=VARCHAR  (字符串)

 8.时间格式

 to_date(#{date},'yyyy-MM-dd hh24:mi:ss')

  9.Oracle中添加当前时间

Sysdate

可以在对应的字段下直接写

列如:

        INSERT INTO USER(username, age, time) 

        VALUES (

                        "张三",

                        "18",

                        sysdate

        )

 10.Trim标签属性

prefix表示以xxx开头

prefixOverrides表示若标签中间内容以xxx开头,就删除

suffix表示以xxx结尾

suffixOverrides表示若标签中间内容以xxx结尾,就删除

列如:

        UPDATE USERNAME 

        < trime prefix = "set"  sufficOverrdes = ",">

                <if test="state != null">

                        state = #{state} ,

                 </if>

                <if test="age!= null">

                        age= #{age} ,

                 </if>

           where id = #{id}

这句话就是代表这段代码以 "set" 开头 , 如果结尾是"逗号" 就删掉.

 11.If-Else标签

choose (when,otherwise)

例如:

        <choose>
            <when test="params!=null">  //when 代表如果
                right JOIN
            </when>
            <otherwise> // otherwise代表否则
                LEFT JOIN
            </otherwise>
        </choose>

 12.if标签中大于号小于号

大于   gt

大于等于 gte

小于  lt 

小于等于  lte

例如:  

        <if text = "age gt '1'  and age lt  '3'"/>

 13.in多参查询

Mapper.Java中

List<User> getUser(@Parm("userIds") List<Integer> userIds);

Mapper.XML中 

select * from user
where userid in
          <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
              #{item}
          </foreach>

 14.查询多个字符串类型

and regexp_like(t.userName, '张三|李四|王五')

15.字符的拼接

<!--两表查询拼接字段,截取“/”第八个之前的数据-->
    <select id="getSys001JiancetjInfo" resultType="com.qc.jy.pojo.Sys001JiancetjInfo">
        SELECT t2.*,
        SUBSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.jctp),'/'), 1, INSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.jctp),'/'), '/', 1, 8) - 1) AS pathjctb,
        SUBSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.JCBG),'/'), 1, INSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.JCBG),'/'), '/', 1, 8) - 1) AS pathjcbg,
        SUBSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.JCSP),'/'), 1, INSTR(CONCAT(CONCAT(CONCAT(CONCAT(t1.PATH,t1.gcbm),'/'),t2.JCSP),'/'), '/', 1, 8) - 1) AS pathjcsp
        FROM SYS_001_Wenjiantjinfo t1 JOIN SyS_001_JIANCETJINFO t2 ON t1.gcbm = t2.gcbm
        <where>
            <if test="featid !=null and featid!=''">
               FEATID = #{featid}
            </if>
        </where>
    </select>

16.使用insert-select进行添加数据

<insert id="addPipingNetworkSketch">
        insert into SYS_002_GUANWANGCAOTU(FROMID,TOID,GDTYPE,GDCZ,GDKJ,REMARK,FROMX,FROMY,TOX,TOY,UPLOADPERSON,ID,FEATID,STYLE,WIDTH,SSQZ,SFJS,LXFS,DWMC)
        SELECT #{fromId},#{toId},#{gdType},#{gdcz},#{gdkj},#{remark},#{fromX},#{fromY},#{toX},#{toY},#{uploadPerson}, MAX(ID) + 1 , #{featId} , #{style}, #{width}, #{ssqz}, #{sfjs}, #{lxfs}, #{dwmc} FROM SYS_002_GUANWANGCAOTU
    </insert>

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值