什么是逆向工程:
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 Listwarnings = new ArrayList ();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 }
生成代码配置文件generatorConfig.xml:
1 2 5 67 8 669 10 12 1311 16 17 22 23 2526 28 29 3027 32 33 37 3834 35 36 40 41 43 4442 47 48 50 5149 52
53
54
55 60 61 65
执行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 ListselectByExampleWithBLOBs(ItemsExample example);20 21 List 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 }
1 2 34 5 116 7 8 9 10 12 1413 15 4316 4217 4118 4019 3920 3821 3722 and ${criterion.condition} 23 2425 and ${criterion.condition} #{criterion.value} 26 2728 and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 29 3031 and ${criterion.condition} 32 3633 #{listItem} 34 3544 7245 7146 7047 6948 6849 6750 6651 and ${criterion.condition} 52 5354 and ${criterion.condition} #{criterion.value} 55 5657 and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} 58 5960 and ${criterion.condition} 61 6562 #{listItem} 63 6473 id, name, price, pic, createtime 74 7576 detail 77 78 94 108 116117 delete from items118 where id = #{id,jdbcType=INTEGER}119 120121 delete from items122 126123 125124 127 insert into items (id, name, price, 128 pic, createtime, detail129 )130 values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL}, 131 #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR}132 )133 134135 insert into items136 177 183137 156138 id,139 140141 name,142 143144 price,145 146147 pic,148 149150 createtime,151 152153 detail,154 155157 176158 #{id,jdbcType=INTEGER},159 160161 #{name,jdbcType=VARCHAR},162 163164 #{price,jdbcType=REAL},165 166167 #{pic,jdbcType=VARCHAR},168 169170 #{createtime,jdbcType=TIMESTAMP},171 172173 #{detail,jdbcType=LONGVARCHAR},174 175184 update items185 209186 205187 id = #{record.id,jdbcType=INTEGER},188 189190 name = #{record.name,jdbcType=VARCHAR},191 192193 price = #{record.price,jdbcType=REAL},194 195196 pic = #{record.pic,jdbcType=VARCHAR},197 198199 createtime = #{record.createtime,jdbcType=TIMESTAMP},200 201202 detail = #{record.detail,jdbcType=LONGVARCHAR},203 204206 208207 210 update items211 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 221218 220219 222 update items223 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 232229 231230 233 update items234 253235 251 where id = #{id,jdbcType=INTEGER}252236 name = #{name,jdbcType=VARCHAR},237 238239 price = #{price,jdbcType=REAL},240 241242 pic = #{pic,jdbcType=VARCHAR},243 244245 createtime = #{createtime,jdbcType=TIMESTAMP},246 247248 detail = #{detail,jdbcType=LONGVARCHAR},249 250254 update items255 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 262263 update items264 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 270
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 }
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 ListoredCriteria; 13 14 public ItemsExample() { 15 oredCriteria = new ArrayList (); 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 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 criteria; 69 70 protected GeneratedCriteria() { 71 super(); 72 criteria = new ArrayList (); 73 } 74 75 public boolean isValid() { 76 return criteria.size() > 0; 77 } 78 79 public List getAllCriteria() { 80 return criteria; 81 } 82 83 public List 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 values) {149 addCriterion("id in", values, "id");150 return (Criteria) this;151 }152 153 public Criteria andIdNotIn(List 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 values) {219 addCriterion("name in", values, "name");220 return (Criteria) this;221 }222 223 public Criteria andNameNotIn(List 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 values) {279 addCriterion("price in", values, "price");280 return (Criteria) this;281 }282 283 public Criteria andPriceNotIn(List 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 values) {349 addCriterion("pic in", values, "pic");350 return (Criteria) this;351 }352 353 public Criteria andPicNotIn(List 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 values) {409 addCriterion("createtime in", values, "createtime");410 return (Criteria) this;411 }412 413 public Criteria andCreatetimeNotIn(List 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 }
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 @Before19 public void setUp() throws Exception {20 applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");21 itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");22 }23 24 //根据主键删除 25 @Test26 public void testDeleteByPrimaryKey() {27 fail("Not yet implemented");28 }29 30 //插入31 @Test32 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 @Test43 public void testSelectByExample() {44 ItemsExample itemsExample = new ItemsExample();45 //通过criteria构造查询条件46 ItemsExample.Criteria criteria = itemsExample.createCriteria();47 criteria.andNameEqualTo("手机");48 //可能返回多条记录49 Listlist = itemsMapper.selectByExample(itemsExample);50 System.out.println(list);51 }52 53 //根据主键查询54 @Test55 public void testSelectByPrimaryKey() {56 Items items = itemsMapper.selectByPrimaryKey(1);57 System.out.println(items);58 }59 60 //更新数据61 @Test62 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 }
主要是看下逆向工程生成的mapper接口、mapper.xml配置文件;