mapper.xml設定檔詳解

來源:互聯網
上載者:User

標籤:

 

--命名空間通常為該mapper對應檔所對應maper介面所在的路徑

<mapper namespace="com.harbsoft.com.mybatis.mapper.UserMapper">


--開啟二級緩衝 (實體類必須序列化)

<cache type="org.mybatis.caches.ehcache.EhcacheCache" />


--抽取通用的SQL

<sql id="user_query_where">   

     通用sql

</sql>    


--if

<if test="id!=null and id!=‘‘">

       通常是where條件陳述式

 </if>


--foreach

<foreach collection="ids" item="id" open="and (" close=")" separator="or">

     user.id=#{id}

</foreach>


--$

AND user.username LIKE ‘${username}%‘


--#

AND user.sex = #{sex}


--resultMap對應的是表與實體類的映射  -- type 資料庫表對應的實體類,別名或完整類名都可以

<resultMap type="person" id="resultMapPerson">

<!-- 結果集的主鍵 -->

--主鍵  <id/>

<id  property="userid" column="id"/>

<!-- 普通的列 -->

--column 是資料庫中欄位, property是實體類中欄位

<result property="name"  column="username" />

<result property="addr"  column="address" />

</resultMap>


-- 一對一的關係處理(一個訂單對應一個使用者, 此處相當於一個類中的一個欄位,該欄位為一個對象)

<association property="user" javaType="com.harbosoft.mybatis.po.User">

<id  property="id" column="user_id"/>

<result  property="username" column="username"/>

<result  property="sex" column="sex"/>

<result  property="address" column="address"/>

</association>



--一對多的關係處理(一個使用者有多個訂單,此處相當於一個類中的一個欄位,該欄位為一個集合)

<!-- 訂單資訊 -->

<collection property="orders" ofType="com.harbosoft.mybatis.po.Orders">

<!-- 訂單號 -->

<result property="order_number" column="order_number" />

<result property="id" column="id" />

</collection>


三者可以嵌套使用

一個使用者--------多個訂單-------多個訂單明細

一個使用者--------多個訂單-------多個訂單明細--------多個商品



--select

public User findUserById(int id)

<select id="findUserById" parameterType="int"  resultType="user">

     SELECT * FROM USER WHERE id=#{id}   

     id-------mapper介面的方法名;

     parameterType -------mapper介面的方法參數的類型

     resultType ---------mapper介面的方法的傳回值類型 

     user ----------是別名(全名是com.harbosoft.mybatis.Items)

     id 和形參保持一致   (#)

</select>


--傳回值是list   user

public List<User> findUserByName(String username)

<select id="findUserByName" parameterType="string" resultType="user">

  SELECT * FROM USER WHERE username like ‘${value}%‘ 

    該方法傳回值類型為List,但是集合中裝的是user,所以resultType 的值只要和集合中儲存的一樣即可

     value 可以隨意些,什麼都可以  ($)

</select>


--傳回值是list   參數是user   resultType 

public List<User> findUserList(User user)throws Exception;

<!-- 綜合查詢使用者資訊 -->

<select id="findUserList" parameterType="user" resultType="user">

SELECT * FROM USER 

<where>

<!-- 使用者的查詢條件 -->

     <include refid="user_query_where"/>

     該條失去了可能會重用,所以抽取出來,引用時用include

</where>

</select>



<sql id="user_query_where">
          <if test="id!=null and id!=‘‘">
             AND user.id=#{id}

          </if>

          <foreach collection="ids" item="id" open="and (" close=")" separator="or">
               user.id=#{id} and (userid =
          </foreach>
          <if test="username!=null and username!=‘‘">
             AND user.username LIKE ‘${username}%‘
          </if>
          <if test="sex!=null and sex!=‘‘">
             AND user.sex = #{sex}
          </if>

</sql>


--傳回值是List   resultMap 

public List<Person> findUserListResultMap(User user)throws Exception;

<!-- 綜合查詢使用者資訊 使用resultMap-->

<select id="findUserListResultMap" parameterType="user" resultMap="resultMapPerson">

     SELECT * FROM USER WHERE username like ‘${username}%‘ and sex=#{sex}

</select>


--參數是map    hashmap   resultType

public List<User> findUserListByHashmap(Map map)throws Exception;

<!-- 通過hashmap查詢使用者資訊 -->

<select id="findUserListByHashmap" parameterType="hashmap" resultType="user">

     SELECT * FROM USER WHERE username like ‘${name}%‘ and sex=#{sex}

</select>


--傳回值是map    resultType

public Map findUserByIdReturnMap(int id) throws Exception;

<!-- 擷取單個使用者資訊返回hashmap -->

<select id="findUserByIdReturnMap" parameterType="int"  resultType="hashmap">

     SELECT * FROM USER WHERE id=#{id}

</select>



--insert

public void insertUser(User user) throws Exception;

<insert id="insertUser" parameterType="user">

<!--  keyProperty:指定主鍵映射的pojo對象的屬性

        order:selectKey的執行順序,mysql這裡設定為after

        企業中實際使用時,主鍵通常使用uuid()即 SELECT UUID()

-->

<selectKey keyProperty="id" order="AFTER" resultType="int">

      SELECT LAST_INSERT_ID()

</selectKey>

INSERT INTO USER(username,birthday,sex,address,detail,score)

     VALUES(#{username},#{birthday},#{sex},#{address},#{detail},#{score})

</insert>

       

--update

public void updateUserById(User user) throws Exception;

<update id="updateUserById"  parameterType="com.harbsoft.mybatis.po.User">

      update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address},detail=#{detail},score=#{score}
      where id=#{id}

</update>


--delete

public void deleteUserById(int id) throws Exception;

<delete id="deleteUserById"  parameterType="java.lang.Integer">
        delete from user where id=#{value}

</delete>


 

mapper.xml設定檔詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.