mybatis由浅入深day02_9.3.5使用生成的代码_9.4逆向工程注意事项
9.3.5 使用生成的代码
需要将生成工程中所生成的代码拷贝到自己的工程中。
拷这4个到我们原来的spring_mybatis1216工程下
ItemsMapper.java
- package cn.itcast.ssm.mapper;
- import cn.itcast.ssm.po.Items;
- import cn.itcast.ssm.po.ItemsExample;
- import java.util.List;
- import org.apache.ibatis.annotations.Param;
- public interface ItemsMapper {
- int countByExample(ItemsExample example);
- //删除符合条件的记录
- int deleteByExample(ItemsExample example);
- //根据主键删除
- int deleteByPrimaryKey(Integer id);
- //插入对象所有字段
- int insert(Items record);
- //插入对象不为空的字段
- int insertSelective(Items record);
- List<Items> selectByExampleWithBLOBs(ItemsExample example);
- //自定义查询条件查询结果集
- List<Items> selectByExample(ItemsExample example);
- //根据主键查询
- Items selectByPrimaryKey(Integer id);
- int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
- int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
- int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
- //根据主键将对象中不为空的值更新至数据库
- int updateByPrimaryKeySelective(Items record);
- int updateByPrimaryKeyWithBLOBs(Items record);
- //根据主键将对象中所有字段的值更新至数据库
- int updateByPrimaryKey(Items record);
- }
ItemsMapper.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="cn.itcast.ssm.mapper.ItemsMapper" >
- <resultMap id="BaseResultMap" type="cn.itcast.ssm.po.Items" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- <result column="price" property="price" jdbcType="REAL" />
- <result column="pic" property="pic" jdbcType="VARCHAR" />
- <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
- </resultMap>
- <resultMap id="ResultMapWithBLOBs" type="cn.itcast.ssm.po.Items" extends="BaseResultMap" >
- <result column="detail" property="detail" jdbcType="LONGVARCHAR" />
- </resultMap>
- <sql id="Example_Where_Clause" >
- <where >
- <foreach collection="oredCriteria" item="criteria" separator="or" >
- <if test="criteria.valid" >
- <trim prefix="(" suffix=")" prefixOverrides="and" >
- <foreach collection="criteria.criteria" item="criterion" >
- <choose >
- <when test="criterion.noValue" >
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue" >
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue" >
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue" >
- and ${criterion.condition}
- <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Update_By_Example_Where_Clause" >
- <where >
- <foreach collection="example.oredCriteria" item="criteria" separator="or" >
- <if test="criteria.valid" >
- <trim prefix="(" suffix=")" prefixOverrides="and" >
- <foreach collection="criteria.criteria" item="criterion" >
- <choose >
- <when test="criterion.noValue" >
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue" >
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue" >
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue" >
- and ${criterion.condition}
- <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Base_Column_List" >
- id, name, price, pic, createtime
- </sql>
- <sql id="Blob_Column_List" >
- detail
- </sql>
- <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="cn.itcast.ssm.po.ItemsExample" >
- select
- <if test="distinct" >
- distinct
- </if>
- <include refid="Base_Column_List" />
- ,
- <include refid="Blob_Column_List" />
- from items
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- <if test="orderByClause != null" >
- order by ${orderByClause}
- </if>
- </select>
- <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.itcast.ssm.po.ItemsExample" >
- select
- <if test="distinct" >
- distinct
- </if>
- <include refid="Base_Column_List" />
- from items
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- <if test="orderByClause != null" >
- order by ${orderByClause}
- </if>
- </select>
- <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- ,
- <include refid="Blob_Column_List" />
- from items
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from items
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <delete id="deleteByExample" parameterType="cn.itcast.ssm.po.ItemsExample" >
- delete from items
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- </delete>
- <insert id="insert" parameterType="cn.itcast.ssm.po.Items" >
- insert into items (id, name, price,
- pic, createtime, detail
- )
- values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL},
- #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR}
- )
- </insert>
- <insert id="insertSelective" parameterType="cn.itcast.ssm.po.Items" >
- insert into items
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- id,
- </if>
- <if test="name != null" >
- name,
- </if>
- <if test="price != null" >
- price,
- </if>
- <if test="pic != null" >
- pic,
- </if>
- <if test="createtime != null" >
- createtime,
- </if>
- <if test="detail != null" >
- detail,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- #{id,jdbcType=INTEGER},
- </if>
- <if test="name != null" >
- #{name,jdbcType=VARCHAR},
- </if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- </if>
- <if test="pic != null" >
- #{pic,jdbcType=VARCHAR},
- </if>
- <if test="createtime != null" >
- #{createtime,jdbcType=TIMESTAMP},
- </if>
- <if test="detail != null" >
- #{detail,jdbcType=LONGVARCHAR},
- </if>
- </trim>
- </insert>
- <select id="countByExample" parameterType="cn.itcast.ssm.po.ItemsExample" resultType="java.lang.Integer" >
- select count(*) from items
- <if test="_parameter != null" >
- <include refid="Example_Where_Clause" />
- </if>
- </select>
- <update id="updateByExampleSelective" parameterType="map" >
- update items
- <set >
- <if test="record.id != null" >
- id = #{record.id,jdbcType=INTEGER},
- </if>
- <if test="record.name != null" >
- name = #{record.name,jdbcType=VARCHAR},
- </if>
- <if test="record.price != null" >
- price = #{record.price,jdbcType=REAL},
- </if>
- <if test="record.pic != null" >
- pic = #{record.pic,jdbcType=VARCHAR},
- </if>
- <if test="record.createtime != null" >
- createtime = #{record.createtime,jdbcType=TIMESTAMP},
- </if>
- <if test="record.detail != null" >
- detail = #{record.detail,jdbcType=LONGVARCHAR},
- </if>
- </set>
- <if test="_parameter != null" >
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateByExampleWithBLOBs" parameterType="map" >
- update items
- set id = #{record.id,jdbcType=INTEGER},
- name = #{record.name,jdbcType=VARCHAR},
- price = #{record.price,jdbcType=REAL},
- pic = #{record.pic,jdbcType=VARCHAR},
- createtime = #{record.createtime,jdbcType=TIMESTAMP},
- detail = #{record.detail,jdbcType=LONGVARCHAR}
- <if test="_parameter != null" >
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateByExample" parameterType="map" >
- update items
- set id = #{record.id,jdbcType=INTEGER},
- name = #{record.name,jdbcType=VARCHAR},
- price = #{record.price,jdbcType=REAL},
- pic = #{record.pic,jdbcType=VARCHAR},
- createtime = #{record.createtime,jdbcType=TIMESTAMP}
- <if test="_parameter != null" >
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateByPrimaryKeySelective" parameterType="cn.itcast.ssm.po.Items" >
- update items
- <set >
- <if test="name != null" >
- name = #{name,jdbcType=VARCHAR},
- </if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- </if>
- <if test="pic != null" >
- pic = #{pic,jdbcType=VARCHAR},
- </if>
- <if test="createtime != null" >
- createtime = #{createtime,jdbcType=TIMESTAMP},
- </if>
- <if test="detail != null" >
- detail = #{detail,jdbcType=LONGVARCHAR},
- </if>
- </set>
- where id = #{id,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.itcast.ssm.po.Items" >
- update items
- set name = #{name,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL},
- pic = #{pic,jdbcType=VARCHAR},
- createtime = #{createtime,jdbcType=TIMESTAMP},
- detail = #{detail,jdbcType=LONGVARCHAR}
- where id = #{id,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKey" parameterType="cn.itcast.ssm.po.Items" >
- update items
- set name = #{name,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL},
- pic = #{pic,jdbcType=VARCHAR},
- createtime = #{createtime,jdbcType=TIMESTAMP}
- where id = #{id,jdbcType=INTEGER}
- </update>
- </mapper>
测试ItemsMapper中的方法
ItemsMapperTest.java
- package cn.itcast.ssm.mapper;
- import java.util.Date;
- import java.util.List;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.ssm.po.Items;
- import cn.itcast.ssm.po.ItemsExample;
- public class ItemsMapperTest {
- private ApplicationContext applicationContext;
- private ItemsMapper itemsMapper;
- //在setUp()这个方法得到spring容器
- @Before
- public void setUp() throws Exception {
- applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
- itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
- }
- //自定义条件查询
- @Test
- public void testSelectByExample() {
- ItemsExample itemsExample = new ItemsExample();
- //通过criteria构造查询条件
- ItemsExample.Criteria criteria = itemsExample.createCriteria();
- criteria.andNameEqualTo("笔记本");
- //可能返回多条记录
- List<Items> list = itemsMapper.selectByExample(itemsExample);
- System.out.println(list);
- }
- //根据主键查询
- @Test
- public void testSelectByPrimaryKey() {
- Items items = itemsMapper.selectByPrimaryKey(1);
- System.out.println(items);
- }
- //插入
- @Test
- public void testInsert() {
- //构造items对象
- Items items = new Items();
- items.setName("手机");
- items.setPrice(900f);
- items.setDetail("不错");
- items.setCreatetime(new Date());
- itemsMapper.insert(items);
- }
- //根据主键删除
- @Test
- public void testDeleteByPrimaryKey() {
- itemsMapper.deleteByPrimaryKey(4);
- }
- //更新数据
- @Test
- public void testUpdateByPrimaryKey() {
- //对所有字段进行更新,需要先查询出来再更新
- Items items = itemsMapper.selectByPrimaryKey(1);
- items.setName("水杯");
- itemsMapper.updateByPrimaryKey(items);
- //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新
- //itemsMapper.updateByPrimaryKeySelective(record);
- }
- }
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逆向工程注意事项的更多相关文章
- mybatis由浅入深day02_9逆向工程
9 逆向工程 9.1 什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mappe ...
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
什么是MyBatis Generator MyBatis Generator (MBG) 是一个Mybatis的代码生成器,可以自动生成一些简单的CRUD(插入,查询,更新,删除)操作代码,model ...
- myBatis自动生成相关代码文件配置(Maven)
pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...
- Mybatis上路_06-使用Java自动生成[转]
Mybatis上路_06-使用Java自动生成 11人收藏此文章, 我要收藏发表于1个月前(2013-04-24 23:05) , 已有151次阅读 ,共0个评论 目录:[ - ] 1.编写Gener ...
- mybatis自动生成java代码
SSM框架没有DB+Record模式,写起来特别费劲,只能用下面的方法勉强凑合. 上图中,*.jar为下载的,src为新建的空白目录,.xml配置如下. <?xml version=" ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...
- (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00 博客园-所有随笔区 ...
- 使用mybatis-generator生成自动代码
2019-02-22 配置文件: pom.xml 添加 dependency plugin 基于mybatis-plus <dependency> <groupId>o ...
随机推荐
- Zookeeper运维的一些经验[转]
Zookeeper是一个分布式协调框架,有不错的性能,也经过许多公司的验证,所以在很多场景都有使用.大家一般用Zookeeper来实现服务发现(类似DNS),配置管理,分布式锁,leader选举等.在 ...
- singer页面点击歌手singer是跳转到singer-detail的设置
1.创建components/singer-detail/singer-detail.vue 2.配置动态路由: { path: ':id', name:'singer-detail', compon ...
- 在开发JavaBean的过程中打开Tomcat的reloadable
这样可以方便调试,就不用每次修改JavaBean都要重启服务器了,但是要记得,项目deploy阶段的时候要关闭这个选项(考虑服务器的性能问题) 配置Tomcat的/conf/server.xml即可 ...
- Linux Shell sort排序常用命令(转载)
转载自:http://blog.csdn.net/monkeyduck/article/details/10097829 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则 ...
- ajaxupload 异步上传工具
基于jquery库异步上传的jquery插件 $.ajaxFileUpload({ url:(baseURL+'/common/fileUploadAct!fileUpload.action?clas ...
- 数据库——SQL中EXISTS怎么用1(转)
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返 ...
- MySql 生成日期随机数
select DATE_ADD(sd, INTERVAL FLOOR(1+ RAND() * ((ABS(UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd))) - 1)) ...
- Hibernate- QBC-基本查询
01.环境搭建 02.基本查询 1.方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt &g ...
- java-request与response编码问题
一.request.setCharacterEncoding("utf-8") 二.response.setContentType("text/html;charset= ...
- Windoows窗口程序七
WM_QUIT--用于结束消息循环处理 wParam - PostQuitMessage函数传递的参数 lParam - 不使用 当GetMessage收到这个消息后,会返回false,结束while ...