zoukankan      html  css  js  c++  java
  • mybatis select/insert/update/delete

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html

    SqlSession

    As mentioned above, the SqlSession instance is the most powerful class in MyBatis. It is where you'll find all of the methods to execute statements, commit or rollback transactions and acquire mapper instances.

    There are over twenty methods on the SqlSession class, so let's break them up into more digestible groupings.

    Statement Execution Methods
    These methods are used to execute SELECT, INSERT, UPDATE and DELETE statements that are defined in your SQL Mapping XML files. They are pretty self explanatory, each takes the ID of the statement and the Parameter Object, which can be a primitive (auto-boxed or wrapper), a JavaBean, a POJO or a Map.

    复制代码
    <T> T selectOne(String statement, Object parameter)
    <E> List<E> selectList(String statement, Object parameter)
    <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
    int insert(String statement, Object parameter)
    int update(String statement, Object parameter)
    int delete(String statement, Object parameter)
    复制代码

    The difference between selectOne and selectList is only in that selectOne must return exactly one object or null (none). If any more than one, an exception will be thrown. If you don't' know how many objects are expected, use selectList. If you want to check for the existence of an object, you're better off returning a count (0 or 1). The selectMap is a special case in that it is designed to convert a list of results into a Map based on one of the properties in the resulting objects. Because not all statements require a parameter, these methods are overloaded with versions that do not require the parameter object.

    The value returned by the insert, update and delete methods indicate the number of rows affected by the statement.

    复制代码
    <T> T selectOne(String statement)
    <E> List<E> selectList(String statement)
    <K,V> Map<K,V> selectMap(String statement, String mapKey)
    int insert(String statement)
    int update(String statement)
    int delete(String statement)
    复制代码

    而在mapper的xml中, 不需要(也不能)给insert, update, delete指定resultType, 如这里所解释

    http://mybatis.github.io/mybatis-3/sqlmap-xml.html

    http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

    insert, update, delete只支持这些Attributes: id, parameterType, parameterMap, flushCache, timeout, statementType, useGeneratedKeys, keyProperty, keyColumn, databaseId

    insert可以通过下面的方式指定返回值。。。

    <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
                select LAST_INSERT_ID();
     </selectKey>

    update和delete的返回值是实际数据库中受到影响的记录的数目。参看 https://stackoverflow.com/questions/34653120/what-are-the-return-values-for-the-mybatis-update-functions

  • 相关阅读:
    phonegap 捕获图片,音频,视屏 api capture
    phonegap的照相机 API
    phonegap 的指南针 api Compass
    PhoneGap Geolocation结合百度地图api获取地理位置api
    PhoneGap Geolocation 获取地理位置 api
    PhoneGap实现重力感应
    PhoneGap模仿微信摇一摇功能
    75-扩展GCD-时间复杂度
    15- 1 << k 时的益出
    6-画图
  • 原文地址:https://www.cnblogs.com/diegodu/p/7374050.html
Copyright © 2011-2022 走看看