1. 程式人生 > >Mybatis入門學習筆記

Mybatis入門學習筆記

sele 需要 sel rtc config mage color ram int

1.定義別名

在sqlMapConfig.xml中,編寫如下代碼:

1     <!-- 定義別名 -->
2     <typeAliases>
3         <!-- 
4             type: 需要映射的類型
5             alias: 別名
6          -->
7         <typeAlias type="cn.sm1234.domain.Customer" alias="customer"/>
8     </typeAliases>

在Customer.xml中使用,

1
<!-- 添加 --> 2 <insert id="insertCustomer" parameterType="customer"> 3 INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone}) 4 </insert>

說明:別名不區分大小寫

程序結構圖如下:

技術分享圖片

代碼說明:

 1     <!-- 修改 -->    
 2     <!-- parameterType傳入對象,包含需要使用的值 
--> 3 <update id="updateCustomer" parameterType="customer"> 4 UPDATE t_customer SET NAME = #{name} WHERE id = #{id} 5 </update> 6 7 <!-- 查詢所有數據 --> 8 <!-- 輸出映射 resultType --> 9 <select id="queryAllCustomer" resultType="customer">
10 SELECT * FROM t_customer 11 </select> 12 13 <!-- 根據id查詢 --> 14 <select id="queryCustomerById" parameterType="_int" resultType="customer"> 15 SELECT * FROM t_customer WHERE id=#{value} 16 </select> 17 18 <!-- 根據name模糊查詢 --> 19 <select id="queryCustomerByName" parameterType="string" resultType="customer"> 20 <!-- 方法一 --> 21 SELECT * FROM t_customer WHERE NAME LIKE #{value} 22 <!-- 方法二 --> 23 <!-- SELECT * FROM t_customer WHERE NAME LIKE ‘%${value}%‘ --> 24 </select> 25 26 <!-- 刪除 --> 27 <delete id="deleteCustomer" parameterType="int"> 28 DELETE FROM t_customer WHERE id=#{value} 29 </delete>

Mybatis入門學習筆記