mabits詳細配置教程


--命名空間通常為該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"/>       <!--該條SQL可能會重用,所以抽取出來,引用時用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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM