使用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,one=@One(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 。