9.3.5 使用生成的代码

需要将生成工程中所生成的代码拷贝到自己的工程中。

拷这4个到我们原来的spring_mybatis1216工程下

ItemsMapper.java

  1. package cn.itcast.ssm.mapper;
  2.  
  3. import cn.itcast.ssm.po.Items;
  4. import cn.itcast.ssm.po.ItemsExample;
  5.  
  6. import java.util.List;
  7.  
  8. import org.apache.ibatis.annotations.Param;
  9.  
  10. public interface ItemsMapper {
  11. int countByExample(ItemsExample example);
  12. //删除符合条件的记录
  13. int deleteByExample(ItemsExample example);
  14. //根据主键删除
  15. int deleteByPrimaryKey(Integer id);
  16. //插入对象所有字段
  17. int insert(Items record);
  18. //插入对象不为空的字段
  19. int insertSelective(Items record);
  20.  
  21. List<Items> selectByExampleWithBLOBs(ItemsExample example);
  22. //自定义查询条件查询结果集
  23. List<Items> selectByExample(ItemsExample example);
  24. //根据主键查询
  25. Items selectByPrimaryKey(Integer id);
  26.  
  27. int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
  28.  
  29. int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
  30.  
  31. int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
  32. //根据主键将对象中不为空的值更新至数据库
  33. int updateByPrimaryKeySelective(Items record);
  34.  
  35. int updateByPrimaryKeyWithBLOBs(Items record);
  36. //根据主键将对象中所有字段的值更新至数据库
  37. int updateByPrimaryKey(Items record);
  38. }

ItemsMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="cn.itcast.ssm.mapper.ItemsMapper" >
  4. <resultMap id="BaseResultMap" type="cn.itcast.ssm.po.Items" >
  5. <id column="id" property="id" jdbcType="INTEGER" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="price" property="price" jdbcType="REAL" />
  8. <result column="pic" property="pic" jdbcType="VARCHAR" />
  9. <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
  10. </resultMap>
  11. <resultMap id="ResultMapWithBLOBs" type="cn.itcast.ssm.po.Items" extends="BaseResultMap" >
  12. <result column="detail" property="detail" jdbcType="LONGVARCHAR" />
  13. </resultMap>
  14. <sql id="Example_Where_Clause" >
  15. <where >
  16. <foreach collection="oredCriteria" item="criteria" separator="or" >
  17. <if test="criteria.valid" >
  18. <trim prefix="(" suffix=")" prefixOverrides="and" >
  19. <foreach collection="criteria.criteria" item="criterion" >
  20. <choose >
  21. <when test="criterion.noValue" >
  22. and ${criterion.condition}
  23. </when>
  24. <when test="criterion.singleValue" >
  25. and ${criterion.condition} #{criterion.value}
  26. </when>
  27. <when test="criterion.betweenValue" >
  28. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  29. </when>
  30. <when test="criterion.listValue" >
  31. and ${criterion.condition}
  32. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  33. #{listItem}
  34. </foreach>
  35. </when>
  36. </choose>
  37. </foreach>
  38. </trim>
  39. </if>
  40. </foreach>
  41. </where>
  42. </sql>
  43. <sql id="Update_By_Example_Where_Clause" >
  44. <where >
  45. <foreach collection="example.oredCriteria" item="criteria" separator="or" >
  46. <if test="criteria.valid" >
  47. <trim prefix="(" suffix=")" prefixOverrides="and" >
  48. <foreach collection="criteria.criteria" item="criterion" >
  49. <choose >
  50. <when test="criterion.noValue" >
  51. and ${criterion.condition}
  52. </when>
  53. <when test="criterion.singleValue" >
  54. and ${criterion.condition} #{criterion.value}
  55. </when>
  56. <when test="criterion.betweenValue" >
  57. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  58. </when>
  59. <when test="criterion.listValue" >
  60. and ${criterion.condition}
  61. <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
  62. #{listItem}
  63. </foreach>
  64. </when>
  65. </choose>
  66. </foreach>
  67. </trim>
  68. </if>
  69. </foreach>
  70. </where>
  71. </sql>
  72. <sql id="Base_Column_List" >
  73. id, name, price, pic, createtime
  74. </sql>
  75. <sql id="Blob_Column_List" >
  76. detail
  77. </sql>
  78. <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="cn.itcast.ssm.po.ItemsExample" >
  79. select
  80. <if test="distinct" >
  81. distinct
  82. </if>
  83. <include refid="Base_Column_List" />
  84. ,
  85. <include refid="Blob_Column_List" />
  86. from items
  87. <if test="_parameter != null" >
  88. <include refid="Example_Where_Clause" />
  89. </if>
  90. <if test="orderByClause != null" >
  91. order by ${orderByClause}
  92. </if>
  93. </select>
  94. <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.itcast.ssm.po.ItemsExample" >
  95. select
  96. <if test="distinct" >
  97. distinct
  98. </if>
  99. <include refid="Base_Column_List" />
  100. from items
  101. <if test="_parameter != null" >
  102. <include refid="Example_Where_Clause" />
  103. </if>
  104. <if test="orderByClause != null" >
  105. order by ${orderByClause}
  106. </if>
  107. </select>
  108. <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
  109. select
  110. <include refid="Base_Column_List" />
  111. ,
  112. <include refid="Blob_Column_List" />
  113. from items
  114. where id = #{id,jdbcType=INTEGER}
  115. </select>
  116. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  117. delete from items
  118. where id = #{id,jdbcType=INTEGER}
  119. </delete>
  120. <delete id="deleteByExample" parameterType="cn.itcast.ssm.po.ItemsExample" >
  121. delete from items
  122. <if test="_parameter != null" >
  123. <include refid="Example_Where_Clause" />
  124. </if>
  125. </delete>
  126. <insert id="insert" parameterType="cn.itcast.ssm.po.Items" >
  127. insert into items (id, name, price,
  128. pic, createtime, detail
  129. )
  130. values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL},
  131. #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR}
  132. )
  133. </insert>
  134. <insert id="insertSelective" parameterType="cn.itcast.ssm.po.Items" >
  135. insert into items
  136. <trim prefix="(" suffix=")" suffixOverrides="," >
  137. <if test="id != null" >
  138. id,
  139. </if>
  140. <if test="name != null" >
  141. name,
  142. </if>
  143. <if test="price != null" >
  144. price,
  145. </if>
  146. <if test="pic != null" >
  147. pic,
  148. </if>
  149. <if test="createtime != null" >
  150. createtime,
  151. </if>
  152. <if test="detail != null" >
  153. detail,
  154. </if>
  155. </trim>
  156. <trim prefix="values (" suffix=")" suffixOverrides="," >
  157. <if test="id != null" >
  158. #{id,jdbcType=INTEGER},
  159. </if>
  160. <if test="name != null" >
  161. #{name,jdbcType=VARCHAR},
  162. </if>
  163. <if test="price != null" >
  164. #{price,jdbcType=REAL},
  165. </if>
  166. <if test="pic != null" >
  167. #{pic,jdbcType=VARCHAR},
  168. </if>
  169. <if test="createtime != null" >
  170. #{createtime,jdbcType=TIMESTAMP},
  171. </if>
  172. <if test="detail != null" >
  173. #{detail,jdbcType=LONGVARCHAR},
  174. </if>
  175. </trim>
  176. </insert>
  177. <select id="countByExample" parameterType="cn.itcast.ssm.po.ItemsExample" resultType="java.lang.Integer" >
  178. select count(*) from items
  179. <if test="_parameter != null" >
  180. <include refid="Example_Where_Clause" />
  181. </if>
  182. </select>
  183. <update id="updateByExampleSelective" parameterType="map" >
  184. update items
  185. <set >
  186. <if test="record.id != null" >
  187. id = #{record.id,jdbcType=INTEGER},
  188. </if>
  189. <if test="record.name != null" >
  190. name = #{record.name,jdbcType=VARCHAR},
  191. </if>
  192. <if test="record.price != null" >
  193. price = #{record.price,jdbcType=REAL},
  194. </if>
  195. <if test="record.pic != null" >
  196. pic = #{record.pic,jdbcType=VARCHAR},
  197. </if>
  198. <if test="record.createtime != null" >
  199. createtime = #{record.createtime,jdbcType=TIMESTAMP},
  200. </if>
  201. <if test="record.detail != null" >
  202. detail = #{record.detail,jdbcType=LONGVARCHAR},
  203. </if>
  204. </set>
  205. <if test="_parameter != null" >
  206. <include refid="Update_By_Example_Where_Clause" />
  207. </if>
  208. </update>
  209. <update id="updateByExampleWithBLOBs" parameterType="map" >
  210. update items
  211. set id = #{record.id,jdbcType=INTEGER},
  212. name = #{record.name,jdbcType=VARCHAR},
  213. price = #{record.price,jdbcType=REAL},
  214. pic = #{record.pic,jdbcType=VARCHAR},
  215. createtime = #{record.createtime,jdbcType=TIMESTAMP},
  216. detail = #{record.detail,jdbcType=LONGVARCHAR}
  217. <if test="_parameter != null" >
  218. <include refid="Update_By_Example_Where_Clause" />
  219. </if>
  220. </update>
  221. <update id="updateByExample" parameterType="map" >
  222. update items
  223. set id = #{record.id,jdbcType=INTEGER},
  224. name = #{record.name,jdbcType=VARCHAR},
  225. price = #{record.price,jdbcType=REAL},
  226. pic = #{record.pic,jdbcType=VARCHAR},
  227. createtime = #{record.createtime,jdbcType=TIMESTAMP}
  228. <if test="_parameter != null" >
  229. <include refid="Update_By_Example_Where_Clause" />
  230. </if>
  231. </update>
  232. <update id="updateByPrimaryKeySelective" parameterType="cn.itcast.ssm.po.Items" >
  233. update items
  234. <set >
  235. <if test="name != null" >
  236. name = #{name,jdbcType=VARCHAR},
  237. </if>
  238. <if test="price != null" >
  239. price = #{price,jdbcType=REAL},
  240. </if>
  241. <if test="pic != null" >
  242. pic = #{pic,jdbcType=VARCHAR},
  243. </if>
  244. <if test="createtime != null" >
  245. createtime = #{createtime,jdbcType=TIMESTAMP},
  246. </if>
  247. <if test="detail != null" >
  248. detail = #{detail,jdbcType=LONGVARCHAR},
  249. </if>
  250. </set>
  251. where id = #{id,jdbcType=INTEGER}
  252. </update>
  253. <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.itcast.ssm.po.Items" >
  254. update items
  255. set name = #{name,jdbcType=VARCHAR},
  256. price = #{price,jdbcType=REAL},
  257. pic = #{pic,jdbcType=VARCHAR},
  258. createtime = #{createtime,jdbcType=TIMESTAMP},
  259. detail = #{detail,jdbcType=LONGVARCHAR}
  260. where id = #{id,jdbcType=INTEGER}
  261. </update>
  262. <update id="updateByPrimaryKey" parameterType="cn.itcast.ssm.po.Items" >
  263. update items
  264. set name = #{name,jdbcType=VARCHAR},
  265. price = #{price,jdbcType=REAL},
  266. pic = #{pic,jdbcType=VARCHAR},
  267. createtime = #{createtime,jdbcType=TIMESTAMP}
  268. where id = #{id,jdbcType=INTEGER}
  269. </update>
  270. </mapper>

测试ItemsMapper中的方法

ItemsMapperTest.java

  1. package cn.itcast.ssm.mapper;
  2.  
  3. import java.util.Date;
  4. import java.util.List;
  5.  
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10.  
  11. import cn.itcast.ssm.po.Items;
  12. import cn.itcast.ssm.po.ItemsExample;
  13.  
  14. public class ItemsMapperTest {
  15.  
  16. private ApplicationContext applicationContext;
  17.  
  18. private ItemsMapper itemsMapper;
  19.  
  20. //在setUp()这个方法得到spring容器
  21. @Before
  22. public void setUp() throws Exception {
  23. applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
  24. itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
  25. }
  26.  
  27. //自定义条件查询
  28. @Test
  29. public void testSelectByExample() {
  30. ItemsExample itemsExample = new ItemsExample();
  31. //通过criteria构造查询条件
  32. ItemsExample.Criteria criteria = itemsExample.createCriteria();
  33. criteria.andNameEqualTo("笔记本");
  34. //可能返回多条记录
  35. List<Items> list = itemsMapper.selectByExample(itemsExample);
  36. System.out.println(list);
  37. }
  38.  
  39. //根据主键查询
  40. @Test
  41. public void testSelectByPrimaryKey() {
  42. Items items = itemsMapper.selectByPrimaryKey(1);
  43. System.out.println(items);
  44. }
  45.  
  46. //插入
  47. @Test
  48. public void testInsert() {
  49. //构造items对象
  50. Items items = new Items();
  51. items.setName("手机");
  52. items.setPrice(900f);
  53. items.setDetail("不错");
  54. items.setCreatetime(new Date());
  55. itemsMapper.insert(items);
  56. }
  57.  
  58. //根据主键删除
  59. @Test
  60. public void testDeleteByPrimaryKey() {
  61. itemsMapper.deleteByPrimaryKey(4);
  62. }
  63.  
  64. //更新数据
  65. @Test
  66. public void testUpdateByPrimaryKey() {
  67. //对所有字段进行更新,需要先查询出来再更新
  68. Items items = itemsMapper.selectByPrimaryKey(1);
  69. items.setName("水杯");
  70.  
  71. itemsMapper.updateByPrimaryKey(items);
  72. //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新
  73. //itemsMapper.updateByPrimaryKeySelective(record);
  74. }
  75. }

  9.4 逆向工程注意事项

    9.4.1 Mapper文件内容不覆盖而是追加

XXXMapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。

解决方法:删除原来已经生成的mapper xml文件再进行生成。

Mybatis自动生成的po及mapper.java文件不是内容而是直接覆盖没有此问题。

    9.4.2 Table schema问题

下边是关于针对oracle数据库表生成代码的schema问题:

Schma即数据库模式,oracle中一个用户对应一个schema,可以理解为用户就是schema。

当Oralce数据库存在多个schema可以访问相同的表名时,使用mybatis生成该表的mapper.xml将会出现mapper.xml内容重复的问题,结果导致mybatis解析错误。

解决方法:在table中填写schema,如下:

<table schema="XXXX" tableName=" " >

XXXX即为一个schema的名称,生成后将mapper.xml的schema前缀批量去掉,如果不去掉当oracle用户变更了sql语句将查询失败。

快捷操作方式:mapper.xml文件中批量替换:“from XXXX.”为空

Oracle查询对象的schema可从dba_objects中查询,如下:

select * from dba_objects

mybatis由浅入深day02_9.3.5使用生成的代码_9.4逆向工程注意事项的更多相关文章

  1. mybatis由浅入深day02_9逆向工程

    9 逆向工程 9.1 什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mappe ...

  2. MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子

    什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...

  3. myBatis自动生成相关代码文件配置(Maven)

    pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...

  4. Mybatis上路_06-使用Java自动生成[转]

    Mybatis上路_06-使用Java自动生成 11人收藏此文章, 我要收藏发表于1个月前(2013-04-24 23:05) , 已有151次阅读 ,共0个评论 目录:[ - ] 1.编写Gener ...

  5. mybatis自动生成java代码

    SSM框架没有DB+Record模式,写起来特别费劲,只能用下面的方法勉强凑合. 上图中,*.jar为下载的,src为新建的空白目录,.xml配置如下. <?xml version=" ...

  6. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...

  7. (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...

  8. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00  博客园-所有随笔区 ...

  9. 使用mybatis-generator生成自动代码

    2019-02-22 配置文件: pom.xml  添加 dependency plugin    基于mybatis-plus <dependency> <groupId>o ...

随机推荐

  1. Zookeeper运维的一些经验[转]

    Zookeeper是一个分布式协调框架,有不错的性能,也经过许多公司的验证,所以在很多场景都有使用.大家一般用Zookeeper来实现服务发现(类似DNS),配置管理,分布式锁,leader选举等.在 ...

  2. singer页面点击歌手singer是跳转到singer-detail的设置

    1.创建components/singer-detail/singer-detail.vue 2.配置动态路由: { path: ':id', name:'singer-detail', compon ...

  3. 在开发JavaBean的过程中打开Tomcat的reloadable

    这样可以方便调试,就不用每次修改JavaBean都要重启服务器了,但是要记得,项目deploy阶段的时候要关闭这个选项(考虑服务器的性能问题) 配置Tomcat的/conf/server.xml即可 ...

  4. Linux Shell sort排序常用命令(转载)

    转载自:http://blog.csdn.net/monkeyduck/article/details/10097829 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则 ...

  5. ajaxupload 异步上传工具

    基于jquery库异步上传的jquery插件 $.ajaxFileUpload({ url:(baseURL+'/common/fileUploadAct!fileUpload.action?clas ...

  6. 数据库——SQL中EXISTS怎么用1(转)

    EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤   EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返 ...

  7. MySql 生成日期随机数

    select DATE_ADD(sd, INTERVAL FLOOR(1+ RAND() * ((ABS(UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd))) - 1)) ...

  8. Hibernate- QBC-基本查询

    01.环境搭建 02.基本查询 1.方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt &g ...

  9. java-request与response编码问题

    一.request.setCharacterEncoding("utf-8") 二.response.setContentType("text/html;charset= ...

  10. Windoows窗口程序七

    WM_QUIT--用于结束消息循环处理 wParam - PostQuitMessage函数传递的参数 lParam - 不使用 当GetMessage收到这个消息后,会返回false,结束while ...