1. 程式人生 > >MyBatis學習筆記之四--annotation註解

MyBatis學習筆記之四--annotation註解

使用MyBatis註解開發,可以省去類配置檔案,簡潔方便。但是比較複雜的SQL和動態SQL還是建議書寫類配置檔案。

註解還是不推薦使用的。只是瞭解瞭解!簡單的CRUD可以使用註解。簡單寫寫。

    把之前的例子改成使用註解的。

 

UserMapper.java

 

複製程式碼

  1 package com.cy.mybatis.mapper;
  2 
  3 import java.util.List;
  4 import java.util.Map;
  5 
  6 import org.apache.ibatis.annotations.Delete;
  7 import org.apache.ibatis.annotations.Insert;
  8 import org.apache.ibatis.annotations.Options;
  9 import org.apache.ibatis.annotations.Param;
 10 import org.apache.ibatis.annotations.Result;
 11 import org.apache.ibatis.annotations.ResultMap;
 12 import org.apache.ibatis.annotations.Results;
 13 import org.apache.ibatis.annotations.Select;
 14 import org.apache.ibatis.annotations.Update;
 15 
 16 import com.cy.mybatis.beans.UserBean;
 17 
 18 public interface UserMapper {
 19     // 簡單的增刪改查可以使用註解
 20     // 註解+配置檔案
 21     
 22     /**
 23      * 新增使用者
 24      * @param user
 25      * @return
 26      * @throws Exception
 27      */
 28     @Insert("insert into t_user value (null,user.username,user.password,user.account)")
 29     @Options(useGeneratedKeys=true,keyProperty="user.id")
 30     public int insertUser(@Param("user")UserBean user) throws Exception;
 31     
 32     
 33     /**
 34      * 修改使用者
 35      * @param user
 36      * @param id
 37      * @return
 38      * @throws Exception
 39      */
 40     @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}")
 41     public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception;
 42     
 43      /**
 44       * 刪除使用者
 45       * @param id
 46       * @return
 47       * @throws Exception
 48       */
 49     @Delete(" delete from t_user where id=#{id}  ")
 50     public int deleteUser(int id) throws Exception;
 51     
 52     
 53     /**
 54      * 根據id查詢使用者資訊
 55      * @param id
 56      * @return
 57      * @throws Exception
 58      */
 59     
 60     @Select(" select * from t_user where id=#{id}")
 61     @Results({
 62         
 63         @Result(id=true,property="id",column="id",javaType=Integer.class),
 64         @Result(property="username",column="username",javaType=String.class),
 65         @Result(property="password",column="password",javaType=String.class),
 66         @Result(property="account",column="account",javaType=Double.class)
 67     })
 68     public UserBean selectUserById(int id) throws Exception;
 69      /**
 70       * 查詢所有的使用者資訊
 71       * @return
 72       * @throws Exception
 73       */
 74     
 75     @Select(" select * from t_user")
 76     @ResultMap("userMap")
 77     public List<UserBean> selectAllUser() throws Exception;
 78     
 79     
 80     /**
 81      * 批量增加
 82      * @param user
 83      * @return
 84      * @throws Exception
 85      */
 86    public int batchInsertUser(@Param("users")List<UserBean> user) throws Exception;
 87    
 88    /**
 89     * 批量刪除
 90     * @param list
 91     * @return
 92     * @throws Exception
 93     */
 94    public int batchDeleteUser(@Param("list")List<Integer> list) throws Exception;
 95    
 96    
 97    /**
 98     * 分頁查詢資料
 99     * @param parma
100     * @return
101     * @throws Exception
102     */
103    public List<UserBean> pagerUser(Map<String, Object> parmas) throws Exception;
104    
105    /**
106     * 
107     * 分頁統計資料
108     * @param parma
109     * @return
110     * @throws Exception
111     */
112     public int countUser(Map<String, Object> parmas) throws Exception;  
113     
114 }

複製程式碼

 UserMapper.xml

 

複製程式碼

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.cy.mybatis.mapper.UserMapper">
 4 <!-- 自定義返回結果集 -->
 5    <resultMap id="userMap" type="UserBean">
 6         <id property="id" column="id" javaType="java.lang.Integer"></id>
 7         <result property="username" column="username" javaType="java.lang.String"></result>
 8         <result property="password" column="password" javaType="java.lang.String"></result>
 9         <result property="account" column="account" javaType="java.lang.Double"></result>
10     </resultMap>
11     
12     <!-- 批量操作和foreach標籤 -->
13     
14     <insert id="batchInsertUser" parameterType="java.util.List">
15        insert into t_user values 
16         <foreach collection="users" item="users" separator=",">
17          (null,#{users.username},#{users.password},#{users.account})
18         </foreach>
19     </insert>
20     
21     
22     <delete id="batchDeleteUser">
23        delete from t_user where id in (
24          <foreach collection="list" item="list" separator=",">
25           #{id}
26          </foreach>
27        )
28     </delete>
29     
30     <!--collection 為用於遍歷的元素(必選),支援陣列、List、Set  -->
31     <!-- item 表示集合中每一個元素進行迭代時的別名. -->
32     <!--separator表示在每次進行迭代之間以什麼符號作為分隔 符.  -->
33     
34     <!--#在生成SQL時,對於字元型別引數,會拼裝引號
35          $在生成SQL時,不會拼裝引號,可用於order by之類的引數拼裝
36       -->
37     <select id="pagerUser" parameterType="java.util.Map" resultMap="userMap">
38       select * from t_user where 1=1
39       <if test="username!=null">
40        and username like '%${username}%'
41       </if>
42        limit ${index},${pageSize} 
43     </select>
44     
45     <select id="countUser" parameterType="java.util.Map" resultType="int">
46         select count(*) from t_user where 1=1 
47         <if test="username != null">
48             and username like '%${username}%'    
49         </if>
50     </select>    
51     
52 </mapper>    

複製程式碼

 

 簡單的一個一對一的使用註解的。

複製程式碼

 1 package com.lovo.mybatis.mapper;
 2 
 3 import org.apache.ibatis.annotations.Insert;
 4 import org.apache.ibatis.annotations.One;
 5 import org.apache.ibatis.annotations.Options;
 6 import org.apache.ibatis.annotations.Param;
 7 import org.apache.ibatis.annotations.Result;
 8 import org.apache.ibatis.annotations.ResultType;
 9 import org.apache.ibatis.annotations.Results;
10 import org.apache.ibatis.annotations.Select;
11 
12 import com.lovo.mybatis.beans.HusbandBean;
13 import com.lovo.mybatis.beans.WifeBean;
14 
15 public interface HusbandMapper {
16     
17     /**
18      * 儲存丈夫
19      * @param husband
20      * @return
21      */
22     @Insert("insert into t_husband values (null,#{h.name})")
23     @Options(useGeneratedKeys=true,keyProperty="h.id")
24     public int saveHusband(@Param("h")HusbandBean husband);
25     
26     
27     /**
28      * 根據ID查詢丈夫資料
29      * @param id
30      * @return
31      */
32     @Select("select * from t_husband where id=#{id}")
33     @ResultType(HusbandBean.class)
34     public HusbandBean findHusbandById(int id);
35     
36     
37     
38     /**
39      * 根據ID查詢丈夫與妻子資料
40      * @param id
41      * @return
42      */
43     @Select("select * from t_husband where id=#{id}")
44     @Results({
45         @Result(id=true,property="id",column="id",javaType=Integer.class),
46         @Result(property="name",column="name",javaType=String.class),
47         @Result(property="wife",column="id",javaType=WifeBean.class,
[email protected]
(select="com.lovo.mybatis.mapper.WifeMapper.findWifeByHusbandId")) 48 }) 49 public HusbandBean findHusbandAndWife(int id); 50 51 52 }

複製程式碼

 

複製程式碼

 1 package com.cy.mybatis.mapper;
 2 
 3 import org.apache.ibatis.annotations.ResultType;
 4 import org.apache.ibatis.annotations.Select;
 5 
 6 import com.cy.mybatis.beans.WifeBean;
 7 
 8 public interface WifeMapper {
 9     
10     
11     @Select("select * from t_wife where fk_husband_id = #{id}")
12     @ResultType(WifeBean.class)
13     public WifeBean selectWifeByHusbandId(int id)throws Exception;
14 
15 }

複製程式碼

 注意:使用resultType時,一定要保證,你屬性名與欄位名相同;如果不相同,就使用resultMap 。