使用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 ListselectAllUser() throws Exception; 78 79 80 /** 81 * 批量增加 82 * @param user 83 * @return 84 * @throws Exception 85 */ 86 public int batchInsertUser(@Param("users")List user) throws Exception; 87 88 /** 89 * 批量删除 90 * @param list 91 * @return 92 * @throws Exception 93 */ 94 public int batchDeleteUser(@Param("list")List list) throws Exception; 95 96 97 /** 98 * 分页查询数据 99 * @param parma100 * @return101 * @throws Exception102 */103 public List pagerUser(Map parmas) throws Exception;104 105 /**106 * 107 * 分页统计数据108 * @param parma109 * @return110 * @throws Exception111 */112 public int countUser(Map parmas) throws Exception; 113 114 }
UserMapper.xml
1 2 34 5 6 11 12 13 147 8 9 10 15 insert into t_user values 16 20 21 2217 (null,#{users.username},#{users.password},#{users.account})18 1923 delete from t_user where id in (24 29 30 31 32 33 34 37 44 45 51 5225 #{id}26 27 )28
简单的一个一对一的使用注解的。
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 husband20 * @return21 */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 id30 * @return31 */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 id41 * @return42 */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 。