Mybatis学习(8)逆向工程

什么是逆向工程:

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

1)下载逆向工程:

mybatis-generator-core-1.3.2-bundle.zip

2)使用java程序和xml配置生成代码:

其中GeneratorSqlmap.java:

 1 import java.io.File;
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 
 5 import org.mybatis.generator.api.MyBatisGenerator;
 6 import org.mybatis.generator.config.Configuration;
 7 import org.mybatis.generator.config.xml.ConfigurationParser;
 8 import org.mybatis.generator.internal.DefaultShellCallback;
 9 
10 public class GeneratorSqlmap {
11 
12     public void generator() throws Exception{
13         List<String> warnings = new ArrayList<String>();
14         boolean overwrite = true;
15         //指定 逆向工程配置文件
16         File configFile = new File("generatorConfig.xml"); 
17         ConfigurationParser cp = new ConfigurationParser(warnings);
18         Configuration config = cp.parseConfiguration(configFile);
19         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
20         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
21                 callback, warnings);
22         myBatisGenerator.generate(null);
23     } 
24     
25     public static void main(String[] args) throws Exception {
26         try {
27             GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
28             generatorSqlmap.generator();
29         } catch (Exception e) {
30             e.printStackTrace();
31         }
32     }
33 }
View Code

生成代码配置文件generatorConfig.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7     <context id="testTables" targetRuntime="MyBatis3">
 8         <commentGenerator>
 9             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
10             <property name="suppressAllComments" value="true" />
11         </commentGenerator>
12         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
13         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
14             connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
15             password="mysql">
16         </jdbcConnection>
17         <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
18             connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
19             userId="yycg"
20             password="yycg">
21         </jdbcConnection> -->
22 
23         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
24             NUMERIC 类型解析为java.math.BigDecimal -->
25         <javaTypeResolver>
26             <property name="forceBigDecimals" value="false" />
27         </javaTypeResolver>
28 
29         <!-- targetProject:生成PO类的位置 -->
30         <javaModelGenerator targetPackage="com.ssm.po"
31             targetProject=".\src">
32             <!-- enableSubPackages:是否让schema作为包的后缀 -->
33             <property name="enableSubPackages" value="false" />
34             <!-- 从数据库返回的值被清理前后的空格 -->
35             <property name="trimStrings" value="true" />
36         </javaModelGenerator>
37         <!-- targetProject:mapper映射文件生成的位置 -->
38         <sqlMapGenerator targetPackage="com.ssm.mapper" 
39             targetProject=".\src">
40             <!-- enableSubPackages:是否让schema作为包的后缀 -->
41             <property name="enableSubPackages" value="false" />
42         </sqlMapGenerator>
43         <!-- targetPackage:mapper接口生成的位置 -->
44         <javaClientGenerator type="XMLMAPPER"
45             targetPackage="com.ssm.mapper" 
46             targetProject=".\src">
47             <!-- enableSubPackages:是否让schema作为包的后缀 -->
48             <property name="enableSubPackages" value="false" />
49         </javaClientGenerator>
50         <!-- 指定数据库表 -->
51         <table tableName="items"></table>
52         <table tableName="orders"></table>
53         <table tableName="orderdetail"></table>
54         <table tableName="user"></table>
55         <!-- <table schema="" tableName="sys_user"></table>
56         <table schema="" tableName="sys_role"></table>
57         <table schema="" tableName="sys_permission"></table>
58         <table schema="" tableName="sys_user_role"></table>
59         <table schema="" tableName="sys_role_permission"></table> -->
60         
61         <!-- 有些表的字段需要指定java类型
62          <table schema="" tableName="">
63             <columnOverride column="" javaType="" />
64         </table> -->
65     </context>
66 </generatorConfiguration>
View Code

执行GeneratorSqlmap.java生成代码:

生成后代码:

3)使用生成的代码:

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

举以下的例子:

将ItemsMapper.java/ItemsMapper.xml   Items.java/ItemsExample.java拷贝进项目中;使用这些mapper来做试验:

ItemsMapper.java接口:和ItemsMapper.xml:

 1 package com.ssm.mapper;
 2 
 3 import com.ssm.po.Items;
 4 import com.ssm.po.ItemsExample;
 5 import java.util.List;
 6 import org.apache.ibatis.annotations.Param;
 7 
 8 public interface ItemsMapper {
 9     int countByExample(ItemsExample example);
10 
11     int deleteByExample(ItemsExample example);
12 
13     int deleteByPrimaryKey(Integer id);
14 
15     int insert(Items record);
16 
17     int insertSelective(Items record);
18 
19     List<Items> selectByExampleWithBLOBs(ItemsExample example);
20 
21     List<Items> selectByExample(ItemsExample example);
22 
23     Items selectByPrimaryKey(Integer id);
24 
25     int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
26 
27     int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
28 
29     int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
30 
31     int updateByPrimaryKeySelective(Items record);
32 
33     int updateByPrimaryKeyWithBLOBs(Items record);
34 
35     int updateByPrimaryKey(Items record);
36 }
View Code
  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="com.ssm.mapper.ItemsMapper" >
  4   <resultMap id="BaseResultMap" type="com.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="com.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="com.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="com.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="com.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="com.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="com.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="com.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="com.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="com.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="com.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>
View Code

Items.java 和 ItemsExample.java:

 1 package com.ssm.po;
 2 
 3 import java.util.Date;
 4 
 5 public class Items {
 6     private Integer id;
 7 
 8     private String name;
 9 
10     private Float price;
11 
12     private String pic;
13 
14     private Date createtime;
15 
16     private String detail;
17 
18     public Integer getId() {
19         return id;
20     }
21 
22     public void setId(Integer id) {
23         this.id = id;
24     }
25 
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name == null ? null : name.trim();
32     }
33 
34     public Float getPrice() {
35         return price;
36     }
37 
38     public void setPrice(Float price) {
39         this.price = price;
40     }
41 
42     public String getPic() {
43         return pic;
44     }
45 
46     public void setPic(String pic) {
47         this.pic = pic == null ? null : pic.trim();
48     }
49 
50     public Date getCreatetime() {
51         return createtime;
52     }
53 
54     public void setCreatetime(Date createtime) {
55         this.createtime = createtime;
56     }
57 
58     public String getDetail() {
59         return detail;
60     }
61 
62     public void setDetail(String detail) {
63         this.detail = detail == null ? null : detail.trim();
64     }
65 }
View Code
  1 package com.ssm.po;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Date;
  5 import java.util.List;
  6 
  7 public class ItemsExample {
  8     protected String orderByClause;
  9 
 10     protected boolean distinct;
 11 
 12     protected List<Criteria> oredCriteria;
 13 
 14     public ItemsExample() {
 15         oredCriteria = new ArrayList<Criteria>();
 16     }
 17 
 18     public void setOrderByClause(String orderByClause) {
 19         this.orderByClause = orderByClause;
 20     }
 21 
 22     public String getOrderByClause() {
 23         return orderByClause;
 24     }
 25 
 26     public void setDistinct(boolean distinct) {
 27         this.distinct = distinct;
 28     }
 29 
 30     public boolean isDistinct() {
 31         return distinct;
 32     }
 33 
 34     public List<Criteria> getOredCriteria() {
 35         return oredCriteria;
 36     }
 37 
 38     public void or(Criteria criteria) {
 39         oredCriteria.add(criteria);
 40     }
 41 
 42     public Criteria or() {
 43         Criteria criteria = createCriteriaInternal();
 44         oredCriteria.add(criteria);
 45         return criteria;
 46     }
 47 
 48     public Criteria createCriteria() {
 49         Criteria criteria = createCriteriaInternal();
 50         if (oredCriteria.size() == 0) {
 51             oredCriteria.add(criteria);
 52         }
 53         return criteria;
 54     }
 55 
 56     protected Criteria createCriteriaInternal() {
 57         Criteria criteria = new Criteria();
 58         return criteria;
 59     }
 60 
 61     public void clear() {
 62         oredCriteria.clear();
 63         orderByClause = null;
 64         distinct = false;
 65     }
 66 
 67     protected abstract static class GeneratedCriteria {
 68         protected List<Criterion> criteria;
 69 
 70         protected GeneratedCriteria() {
 71             super();
 72             criteria = new ArrayList<Criterion>();
 73         }
 74 
 75         public boolean isValid() {
 76             return criteria.size() > 0;
 77         }
 78 
 79         public List<Criterion> getAllCriteria() {
 80             return criteria;
 81         }
 82 
 83         public List<Criterion> getCriteria() {
 84             return criteria;
 85         }
 86 
 87         protected void addCriterion(String condition) {
 88             if (condition == null) {
 89                 throw new RuntimeException("Value for condition cannot be null");
 90             }
 91             criteria.add(new Criterion(condition));
 92         }
 93 
 94         protected void addCriterion(String condition, Object value, String property) {
 95             if (value == null) {
 96                 throw new RuntimeException("Value for " + property + " cannot be null");
 97             }
 98             criteria.add(new Criterion(condition, value));
 99         }
100 
101         protected void addCriterion(String condition, Object value1, Object value2, String property) {
102             if (value1 == null || value2 == null) {
103                 throw new RuntimeException("Between values for " + property + " cannot be null");
104             }
105             criteria.add(new Criterion(condition, value1, value2));
106         }
107 
108         public Criteria andIdIsNull() {
109             addCriterion("id is null");
110             return (Criteria) this;
111         }
112 
113         public Criteria andIdIsNotNull() {
114             addCriterion("id is not null");
115             return (Criteria) this;
116         }
117 
118         public Criteria andIdEqualTo(Integer value) {
119             addCriterion("id =", value, "id");
120             return (Criteria) this;
121         }
122 
123         public Criteria andIdNotEqualTo(Integer value) {
124             addCriterion("id <>", value, "id");
125             return (Criteria) this;
126         }
127 
128         public Criteria andIdGreaterThan(Integer value) {
129             addCriterion("id >", value, "id");
130             return (Criteria) this;
131         }
132 
133         public Criteria andIdGreaterThanOrEqualTo(Integer value) {
134             addCriterion("id >=", value, "id");
135             return (Criteria) this;
136         }
137 
138         public Criteria andIdLessThan(Integer value) {
139             addCriterion("id <", value, "id");
140             return (Criteria) this;
141         }
142 
143         public Criteria andIdLessThanOrEqualTo(Integer value) {
144             addCriterion("id <=", value, "id");
145             return (Criteria) this;
146         }
147 
148         public Criteria andIdIn(List<Integer> values) {
149             addCriterion("id in", values, "id");
150             return (Criteria) this;
151         }
152 
153         public Criteria andIdNotIn(List<Integer> values) {
154             addCriterion("id not in", values, "id");
155             return (Criteria) this;
156         }
157 
158         public Criteria andIdBetween(Integer value1, Integer value2) {
159             addCriterion("id between", value1, value2, "id");
160             return (Criteria) this;
161         }
162 
163         public Criteria andIdNotBetween(Integer value1, Integer value2) {
164             addCriterion("id not between", value1, value2, "id");
165             return (Criteria) this;
166         }
167 
168         public Criteria andNameIsNull() {
169             addCriterion("name is null");
170             return (Criteria) this;
171         }
172 
173         public Criteria andNameIsNotNull() {
174             addCriterion("name is not null");
175             return (Criteria) this;
176         }
177 
178         public Criteria andNameEqualTo(String value) {
179             addCriterion("name =", value, "name");
180             return (Criteria) this;
181         }
182 
183         public Criteria andNameNotEqualTo(String value) {
184             addCriterion("name <>", value, "name");
185             return (Criteria) this;
186         }
187 
188         public Criteria andNameGreaterThan(String value) {
189             addCriterion("name >", value, "name");
190             return (Criteria) this;
191         }
192 
193         public Criteria andNameGreaterThanOrEqualTo(String value) {
194             addCriterion("name >=", value, "name");
195             return (Criteria) this;
196         }
197 
198         public Criteria andNameLessThan(String value) {
199             addCriterion("name <", value, "name");
200             return (Criteria) this;
201         }
202 
203         public Criteria andNameLessThanOrEqualTo(String value) {
204             addCriterion("name <=", value, "name");
205             return (Criteria) this;
206         }
207 
208         public Criteria andNameLike(String value) {
209             addCriterion("name like", value, "name");
210             return (Criteria) this;
211         }
212 
213         public Criteria andNameNotLike(String value) {
214             addCriterion("name not like", value, "name");
215             return (Criteria) this;
216         }
217 
218         public Criteria andNameIn(List<String> values) {
219             addCriterion("name in", values, "name");
220             return (Criteria) this;
221         }
222 
223         public Criteria andNameNotIn(List<String> values) {
224             addCriterion("name not in", values, "name");
225             return (Criteria) this;
226         }
227 
228         public Criteria andNameBetween(String value1, String value2) {
229             addCriterion("name between", value1, value2, "name");
230             return (Criteria) this;
231         }
232 
233         public Criteria andNameNotBetween(String value1, String value2) {
234             addCriterion("name not between", value1, value2, "name");
235             return (Criteria) this;
236         }
237 
238         public Criteria andPriceIsNull() {
239             addCriterion("price is null");
240             return (Criteria) this;
241         }
242 
243         public Criteria andPriceIsNotNull() {
244             addCriterion("price is not null");
245             return (Criteria) this;
246         }
247 
248         public Criteria andPriceEqualTo(Float value) {
249             addCriterion("price =", value, "price");
250             return (Criteria) this;
251         }
252 
253         public Criteria andPriceNotEqualTo(Float value) {
254             addCriterion("price <>", value, "price");
255             return (Criteria) this;
256         }
257 
258         public Criteria andPriceGreaterThan(Float value) {
259             addCriterion("price >", value, "price");
260             return (Criteria) this;
261         }
262 
263         public Criteria andPriceGreaterThanOrEqualTo(Float value) {
264             addCriterion("price >=", value, "price");
265             return (Criteria) this;
266         }
267 
268         public Criteria andPriceLessThan(Float value) {
269             addCriterion("price <", value, "price");
270             return (Criteria) this;
271         }
272 
273         public Criteria andPriceLessThanOrEqualTo(Float value) {
274             addCriterion("price <=", value, "price");
275             return (Criteria) this;
276         }
277 
278         public Criteria andPriceIn(List<Float> values) {
279             addCriterion("price in", values, "price");
280             return (Criteria) this;
281         }
282 
283         public Criteria andPriceNotIn(List<Float> values) {
284             addCriterion("price not in", values, "price");
285             return (Criteria) this;
286         }
287 
288         public Criteria andPriceBetween(Float value1, Float value2) {
289             addCriterion("price between", value1, value2, "price");
290             return (Criteria) this;
291         }
292 
293         public Criteria andPriceNotBetween(Float value1, Float value2) {
294             addCriterion("price not between", value1, value2, "price");
295             return (Criteria) this;
296         }
297 
298         public Criteria andPicIsNull() {
299             addCriterion("pic is null");
300             return (Criteria) this;
301         }
302 
303         public Criteria andPicIsNotNull() {
304             addCriterion("pic is not null");
305             return (Criteria) this;
306         }
307 
308         public Criteria andPicEqualTo(String value) {
309             addCriterion("pic =", value, "pic");
310             return (Criteria) this;
311         }
312 
313         public Criteria andPicNotEqualTo(String value) {
314             addCriterion("pic <>", value, "pic");
315             return (Criteria) this;
316         }
317 
318         public Criteria andPicGreaterThan(String value) {
319             addCriterion("pic >", value, "pic");
320             return (Criteria) this;
321         }
322 
323         public Criteria andPicGreaterThanOrEqualTo(String value) {
324             addCriterion("pic >=", value, "pic");
325             return (Criteria) this;
326         }
327 
328         public Criteria andPicLessThan(String value) {
329             addCriterion("pic <", value, "pic");
330             return (Criteria) this;
331         }
332 
333         public Criteria andPicLessThanOrEqualTo(String value) {
334             addCriterion("pic <=", value, "pic");
335             return (Criteria) this;
336         }
337 
338         public Criteria andPicLike(String value) {
339             addCriterion("pic like", value, "pic");
340             return (Criteria) this;
341         }
342 
343         public Criteria andPicNotLike(String value) {
344             addCriterion("pic not like", value, "pic");
345             return (Criteria) this;
346         }
347 
348         public Criteria andPicIn(List<String> values) {
349             addCriterion("pic in", values, "pic");
350             return (Criteria) this;
351         }
352 
353         public Criteria andPicNotIn(List<String> values) {
354             addCriterion("pic not in", values, "pic");
355             return (Criteria) this;
356         }
357 
358         public Criteria andPicBetween(String value1, String value2) {
359             addCriterion("pic between", value1, value2, "pic");
360             return (Criteria) this;
361         }
362 
363         public Criteria andPicNotBetween(String value1, String value2) {
364             addCriterion("pic not between", value1, value2, "pic");
365             return (Criteria) this;
366         }
367 
368         public Criteria andCreatetimeIsNull() {
369             addCriterion("createtime is null");
370             return (Criteria) this;
371         }
372 
373         public Criteria andCreatetimeIsNotNull() {
374             addCriterion("createtime is not null");
375             return (Criteria) this;
376         }
377 
378         public Criteria andCreatetimeEqualTo(Date value) {
379             addCriterion("createtime =", value, "createtime");
380             return (Criteria) this;
381         }
382 
383         public Criteria andCreatetimeNotEqualTo(Date value) {
384             addCriterion("createtime <>", value, "createtime");
385             return (Criteria) this;
386         }
387 
388         public Criteria andCreatetimeGreaterThan(Date value) {
389             addCriterion("createtime >", value, "createtime");
390             return (Criteria) this;
391         }
392 
393         public Criteria andCreatetimeGreaterThanOrEqualTo(Date value) {
394             addCriterion("createtime >=", value, "createtime");
395             return (Criteria) this;
396         }
397 
398         public Criteria andCreatetimeLessThan(Date value) {
399             addCriterion("createtime <", value, "createtime");
400             return (Criteria) this;
401         }
402 
403         public Criteria andCreatetimeLessThanOrEqualTo(Date value) {
404             addCriterion("createtime <=", value, "createtime");
405             return (Criteria) this;
406         }
407 
408         public Criteria andCreatetimeIn(List<Date> values) {
409             addCriterion("createtime in", values, "createtime");
410             return (Criteria) this;
411         }
412 
413         public Criteria andCreatetimeNotIn(List<Date> values) {
414             addCriterion("createtime not in", values, "createtime");
415             return (Criteria) this;
416         }
417 
418         public Criteria andCreatetimeBetween(Date value1, Date value2) {
419             addCriterion("createtime between", value1, value2, "createtime");
420             return (Criteria) this;
421         }
422 
423         public Criteria andCreatetimeNotBetween(Date value1, Date value2) {
424             addCriterion("createtime not between", value1, value2, "createtime");
425             return (Criteria) this;
426         }
427     }
428 
429     public static class Criteria extends GeneratedCriteria {
430 
431         protected Criteria() {
432             super();
433         }
434     }
435 
436     public static class Criterion {
437         private String condition;
438 
439         private Object value;
440 
441         private Object secondValue;
442 
443         private boolean noValue;
444 
445         private boolean singleValue;
446 
447         private boolean betweenValue;
448 
449         private boolean listValue;
450 
451         private String typeHandler;
452 
453         public String getCondition() {
454             return condition;
455         }
456 
457         public Object getValue() {
458             return value;
459         }
460 
461         public Object getSecondValue() {
462             return secondValue;
463         }
464 
465         public boolean isNoValue() {
466             return noValue;
467         }
468 
469         public boolean isSingleValue() {
470             return singleValue;
471         }
472 
473         public boolean isBetweenValue() {
474             return betweenValue;
475         }
476 
477         public boolean isListValue() {
478             return listValue;
479         }
480 
481         public String getTypeHandler() {
482             return typeHandler;
483         }
484 
485         protected Criterion(String condition) {
486             super();
487             this.condition = condition;
488             this.typeHandler = null;
489             this.noValue = true;
490         }
491 
492         protected Criterion(String condition, Object value, String typeHandler) {
493             super();
494             this.condition = condition;
495             this.value = value;
496             this.typeHandler = typeHandler;
497             if (value instanceof List<?>) {
498                 this.listValue = true;
499             } else {
500                 this.singleValue = true;
501             }
502         }
503 
504         protected Criterion(String condition, Object value) {
505             this(condition, value, null);
506         }
507 
508         protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
509             super();
510             this.condition = condition;
511             this.value = value;
512             this.secondValue = secondValue;
513             this.typeHandler = typeHandler;
514             this.betweenValue = true;
515         }
516 
517         protected Criterion(String condition, Object value, Object secondValue) {
518             this(condition, value, secondValue, null);
519         }
520     }
521 }
View Code

4)测试ItemsMapper中的方法:

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

 

主要是看下逆向工程生成的mapper接口、mapper.xml配置文件;

posted on 2017-01-16 02:32  有点懒惰的大青年  阅读(260)  评论(0编辑  收藏  举报