zoukankan      html  css  js  c++  java
  • MyBatis的SQL语句映射文件详解(二)----增删改查

    1、select语句

    public List<User> findUser() {
      // TODO Auto-generated method stub
      
      List users= (List<User>) sqlSessionTemplate.selectList("findAllUser");
      
      return users;
      
     }


    <!-- 查询所有用户-->
    <
    select id="findAllUser" resultType="User"> select * from user </select>

    select具体属性

    属性 描述 取值 默认
    id 在这个模式下唯一的标识符,可被其它语句引用    
    parameterType 传给此语句的参数的完整类名或别名    
    resultType

    语句返回值类型的整类名或别名。注意,如果是集合,那么这里填写的是集合的项的整类名或别名,而不是集合本身的类名。(resultType 与resultMap 不能并用)

    别名与<typeAliases>
          <typeAlias type="com.mybaits.bean.User" alias="User"/>
        </typeAliases>

    中的alias值对应

       
    resultMap 引用的外部resultMap 名。结果集映射是MyBatis 中最强大的特性。许多复杂的映射都可以轻松解决。(resultType 与resultMap 不能并用)    
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout  设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。 预准备语句、可调用语句 STATEMENT PREPARED CALLABLE PREPARED
    resultSetType forward_only,scroll_sensitive,scroll_insensitive 只转发,滚动敏感,不区分大小写的滚动 FORWARD_ONLY SCROLL_SENSITIVE SCROLL_INSENSITIVE 驱动器决定

     2.insert 语句

    public void addUser(User user) {
      // TODO Auto-generated method stub
      sqlSessionTemplate.insert("addUser", user);
     }

    <
    insert id="addUser" parameterType="User" > insert into user (username,usernumber,loginname,loginpassword,sex,birthday) values(#{username},#{usernumber},#{loginname},#{loginpassword},#{sex},#{birthday}) </insert>

    insert语句属性

    属性 描述 取值 默认
    id 在这个模式下唯一的标识符,可被其它语句引用    
    parameterType 传给此语句的参数的完整类名或别名    
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout  设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。 预准备语句、可调用语句 STATEMENT PREPARED CALLABLE PREPARED
    useGeneratedKeys

    告诉MyBatis 使用JDBC 的getGeneratedKeys 方法来获取数据库自己生成的主键(MySQL、SQLSERVER 等

    关系型数据库会有自动生成的字段)。默认:false

    true|false false
    keyProperty

    标识一个将要被MyBatis 设置进getGeneratedKeys 的key 所返回的值,或者为insert 语句使用一个selectKey

    子元素。

       

     3.update,delete语句

      
    <
    delete id="deleteUser" parameterType="String"> delete from user where user.usernumber=#{usernumber} </delete> <update id="updateUser" parameterType="User" > update user set username=#{username},loginname=#{loginname},loginpassword=#{loginpassword},sex=#{sex},birthday=#{birthday} where usernumber=#{usernumber} </update>
    属性 描述 取值 默认
    id 在这个模式下唯一的标识符,可被其它语句引用    
    parameterType 传给此语句的参数的完整类名或别名    
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout  设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。 预准备语句、可调用语句 STATEMENT PREPARED CALLABLE PREPARED

     4.sql

    sql元素用来定义一个可以复用的SQL 语句段,供其它语句调用。比如:

            <sql id="selectAll">
                select * from user
            </sql>
            <select id="getUserByUsername" resultType="User" parameterType="String">
                <include refid="selectAll"></include>
                        where username = #{username}
            </select>
  • 相关阅读:
    【前端积累】Awesome初识
    【Python系列】Python3获取控制台输入
    【linux系列】Centos下安装mysql数据库
    Intellij 部署项目java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    【大数据系列】节点的退役和服役[datanode,yarn]
    【大数据系列】使用api修改hadoop的副本数和块大小
    【规范】alibaba编码规范阅读
    【大数据系列】hadoop上传文件报错_COPYING_ could only be replicated to 0 nodes
    【分布式系列之ActiveMq】ActiveMq入门示例
    类的生命周期
  • 原文地址:https://www.cnblogs.com/nww57/p/4691151.html
Copyright © 2011-2022 走看看