使用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. * @param user
  35. * @param id
  36. * @return
  37. * @throws Exception
  38. */
  39. @Update(" update t_user set username=#{u.username},password=#{u.password},account=#{u.account} where id=#{id}")
  40. public int updateUser (@Param("u")UserBean user,@Param("id")int id) throws Exception;
  41.  
  42. /**
  43. * 刪除用戶
  44. * @param id
  45. * @return
  46. * @throws Exception
  47. */
  48. @Delete(" delete from t_user where id=#{id} ")
  49. public int deleteUser(int id) throws Exception;
  50.  
  51. /**
  52. * 根据id查询用户信息
  53. * @param id
  54. * @return
  55. * @throws Exception
  56. */
  57.  
  58. @Select(" select * from t_user where id=#{id}")
  59. @Results({
  60.  
  61. @Result(id=true,property="id",column="id",javaType=Integer.class),
  62. @Result(property="username",column="username",javaType=String.class),
  63. @Result(property="password",column="password",javaType=String.class),
  64. @Result(property="account",column="account",javaType=Double.class)
  65. })
  66. public UserBean selectUserById(int id) throws Exception;
  67. /**
  68. * 查询所有的用户信息
  69. * @return
  70. * @throws Exception
  71. */
  72.  
  73. @Select(" select * from t_user")
  74. @ResultMap("userMap")
  75. public List<UserBean> selectAllUser() throws Exception;
  76.  
  77. /**
  78. * 批量增加
  79. * @param user
  80. * @return
  81. * @throws Exception
  82. */
  83. public int batchInsertUser(@Param("users")List<UserBean> user) throws Exception;
  84.  
  85. /**
  86. * 批量删除
  87. * @param list
  88. * @return
  89. * @throws Exception
  90. */
  91. public int batchDeleteUser(@Param("list")List<Integer> list) throws Exception;
  92.  
  93. /**
  94. * 分页查询数据
  95. * @param parma
  96. * @return
  97. * @throws Exception
  98. */
  99. public List<UserBean> pagerUser(Map<String, Object> parmas) throws Exception;
  100.  
  101. /**
  102. *
  103. * 分页统计数据
  104. * @param parma
  105. * @return
  106. * @throws Exception
  107. */
  108. public int countUser(Map<String, Object> parmas) throws Exception;
  109.  
  110. }

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. <delete id="batchDeleteUser">
  22. delete from t_user where id in (
  23. <foreach collection="list" item="list" separator=",">
  24. #{id}
  25. </foreach>
  26. )
  27. </delete>
  28.  
  29. <!--collection 为用于遍历的元素(必选),支持数组、List、Set -->
  30. <!-- item 表示集合中每一个元素进行迭代时的别名. -->
  31. <!--separator表示在每次进行迭代之间以什么符号作为分隔 符. -->
  32.  
  33. <!--#在生成SQL时,对于字符类型参数,会拼装引号
  34. $在生成SQL时,不会拼装引号,可用于order by之类的参数拼装
  35. -->
  36. <select id="pagerUser" parameterType="java.util.Map" resultMap="userMap">
  37. select * from t_user where 1=1
  38. <if test="username!=null">
  39. and username like '%${username}%'
  40. </if>
  41. limit ${index},${pageSize}
  42. </select>
  43.  
  44. <select id="countUser" parameterType="java.util.Map" resultType="int">
  45. select count(*) from t_user where 1=1
  46. <if test="username != null">
  47. and username like '%${username}%'
  48. </if>
  49. </select>
  50.  
  51. </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. * 根据ID查询丈夫资料
  28. * @param id
  29. * @return
  30. */
  31. @Select("select * from t_husband where id=#{id}")
  32. @ResultType(HusbandBean.class)
  33. public HusbandBean findHusbandById(int id);
  34.  
  35. /**
  36. * 根据ID查询丈夫与妻子资料
  37. * @param id
  38. * @return
  39. */
  40. @Select("select * from t_husband where id=#{id}")
  41. @Results({
  42. @Result(id=true,property="id",column="id",javaType=Integer.class),
  43. @Result(property="name",column="name",javaType=String.class),
  44. @Result(property="wife",column="id",javaType=WifeBean.class,one=@One(select="com.lovo.mybatis.mapper.WifeMapper.findWifeByHusbandId"))
  45. })
  46. public HusbandBean findHusbandAndWife(int id);
  47.  
  48. }
  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. @Select("select * from t_wife where fk_husband_id = #{id}")
  11. @ResultType(WifeBean.class)
  12. public WifeBean selectWifeByHusbandId(int id)throws Exception;
  13.  
  14. }

注意:使用resultType时,一定要保证,你属性名与字段名相同;如果不相同,就使用resultMap 。

 

MyBatis学习笔记(四) 注解的更多相关文章

  1. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  2. mybatis学习笔记四(动态sql)

    直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...

  3. MyBatis学习笔记(四)——解决字段名与实体类属性名不相同的冲突

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演 ...

  4. mybatis学习笔记(四)

    resultType 语句返回值类型的完整类名或别名 resultType 返回的是一个map集合,key是列名,value是对应的值 使用resultMap实现联表查询 resultMap 查询的结 ...

  5. Mybatis学习笔记(四) —— SqlMapConfig.xml配置文件

    一.properties(属性) SqlMapConfig.xml可以引用java属性文件中的配置信息 在config下定义db.properties文件,如下所示: db.properties配置文 ...

  6. mybatis学习笔记四

    记录下动态sql的常用标签: 1.where 一般用作数据操作添加的条件 例子: <select id="selectByRoleId" resultMap="re ...

  7. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  8. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

  9. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

随机推荐

  1. WAMP,BITNAMI上建立多个虚拟主机都访问到主站上去了怎么解决?

    新建立了多个虚拟主机,访问的结果都是localhost,只要把localhost也建立成一个虚拟主机所有的虚拟主机访问就正常了.

  2. Oracle OCCI学习之开篇

    官网:Oracle C++ Call Interface 一.OCCI介绍 Oracle C++ Call Interface(OCCI)是一个用于访问Oracle数据库的高性能且全面的API.基于标 ...

  3. Myeclipse 添加server library

    来自网络资料 (1)File->New->Other (2)弹出窗口勾上Show All Wizards,然后在type fiter text那里输入Server,选中server-> ...

  4. mfc控件学习

    0.所有控件 软件启动自动被选中:属性中的tabstop改为true或者false 1.Button  动态设置button的字:btn.SetWindowTextW(_T("我不是一个按钮 ...

  5. css基本知识

    WANGJUN59451   css基本知识 1.CSS 简介 CSS 指层叠样式表 (Cascading Style Sheets),是一种用来表现 HTML 文档样式的语言,样式定义如何显示 HT ...

  6. HTML DOM元素

    HTML DOM元素 1.创建新的HTML元素 向HTML DOM添加新元素,必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. <!DOCTYPE html> <h ...

  7. Bootstrap_排版

    标题: Bootstrap和普通的HTML页面一样,定义标题都是使用标签<h1>到<h6>,只不过Bootstrap覆盖了其默认的样式,使用其在所有浏览器下显示的效果一样,具体 ...

  8. oracle dual 表

    dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情,如下: 1.查看当前用户,可以在 SQL Plus中执行下面语句 sele ...

  9. windows tomcat 优化

    windows tomcat 优化 1.  tomcat conf server.xml 在server.xml中修改以一部分,增加节点数目,可以很好的提高性能: <Connector port ...

  10. [HDOJ1175]连连看

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 连连看 Time Limit: 20000/10000 MS (Java/Others)     ...