• Mybatis实体类的映射文件中select,insert语句使用


    id:在命名空间中唯一的标识符,可以被用来引用这条语句。

    parameterType:设置传入这条语句的参数的数据类型,如int,String......

    resultType:设置从这条语句中返回数据的类型。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。可以使用

          resultType 或 resultMap,但不能同时使用。

    select

    1)查询某个表的所有记录

    <select id="queryall" resultType="com.test.Person">
    	select * from person
    </select>
    

    2)根据某个字段的值查询

      ① 直接查询

        传入的参数值通过 #{id} 传递给sql语句(id可自定义为其他的名字)

    <!-- 根据id查用户 -->
    <select id="querypersonbyid" parameterType="int" resultType="com.test.Person">
    	select * from person where id = #{id}
    </select>

      ② 模糊查询

        原生方法:like "%${value}%"  注意使用的是 $ 并且参数名只能用 value,否则会报错

    <!-- 模糊查询  -->
    <select id="querypersonbyname" parameterType="String" resultType="com.test.Person">
    	select * from person where name like "%${value}%"
    </select>
    

        mysql数据库:like CONCAT('%',#{name},'%')  这里是使用CONCAT进行拼接,name可以为其他名字

    <select id="querypersonbyname" parameterType="String" resultType="com.test.Person">
    	select * from person where name like CONCAT('%',#{name},'%')
    </select>
    

     

    Insert

    1)往表中插入一条记录

    <insert id="insertAuthor">
        insert into Author (id,username,password,email,bio)
        values (#{id},#{username},#{password},#{email},#{bio})
    </insert>
    

    2)插入时主键自动生成

      首先,如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上就 OK 了。

    <insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
        insert into Author (username,password,email,bio)
        values (#{username},#{password},#{email},#{bio})
    </insert>
    

      如果你的数据库还支持多行插入, 你也可以传入一个 Author 数组或集合,并返回自动生成的主键。

    <insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
         insert into Author (username, password, email, bio) values
        <foreach item="item" collection="list" separator=",">
            (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
        </foreach>
    </insert>
    

      对于不支持自动生成类型的数据库或可能不支持自动生成主键的 JDBC 驱动,MyBatis 有另外一种方法来生成主键。

      selectKey 元素中的语句将会首先运行,person 的 id 会被设置,然后插入语句会被调用。这可以提供给你一个与数据库中自动生成主键类似的行为,同时保持了 Java 代码的简洁。

    <insert id="insertperson" parameterType="com.test.Person">
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(name,age,address,birthday)
        value(#{name},#{age},#{address},#{birthday})
    </insert>        
    

      selectKey 元素描述如下:

    <selectKey
      keyProperty="id"
      resultType="int"
      order="BEFORE"
      statementType="PREPARED">
    

      keyProperty:selectKey 语句结果应该被设置的目标属性。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。

      resultType:结果的类型。

      order:这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先生成主键,设置 keyProperty 然后执行插入语句。如果设置为AFTER,那么先执行插入语句,然后是 selectKey 中的语句 - 这和 Oracle 数据库的行为相似,在插入语句内部可能有嵌入索引调用。

      statementType:MyBatis 支持 STATEMENT,PREPARED 和 CALLABLE 语句的映射类型,分别代表 PreparedStatement 和 CallableStatement 类型。

  • 相关阅读:
    ORACLE数据库找回用户密码
    PO、POJO、BO、DTO、VO之间的区别(转)
    Http报头Accept与Content-Type的区别
    java.lang.IllegalStateException: getWriter() has already been called for this response
    利用策略模式实现了同一接口的多个Servicel实现类,如何同时注入Controller
    java.util.Stack类简介
    java为什么要重写hashCode和equals方法?
    PowerDesigner15连接Oracle数据库并导出Oracle的表结构
    解决ODBC连接Oracle数据库报Unable to connect SQLState=08004问题
    IIS 返回 405
  • 原文地址:https://www.cnblogs.com/silence-x/p/10863365.html
Copyright © 2020-2023  润新知