mybatis逆向工程

网友投稿 624 2022-09-01

mybatis逆向工程

mybatis逆向工程

​​什么是逆向工程​​​​-逆向工程​​​​创建逆向工程​​​​添加generatorConfigxml​​​​创建Generatorjava​​​​将逆向工程生成的代码拷贝到指定项目中​​​​使用生成的代码进行测试​​​​注意事项​​

什么是逆向工程

mybatis提供了一个逆向工程工具,通过逆向工程,可以帮助程序员根据单表来生成po类、mapper映射文件、mapper接口。

-逆向工程

​​version="1.0" encoding="UTF-8"?>

创建Generator.java

public class Generator { public static void main(String[] args) throws Exception { List warnings = new ArrayList(); boolean overwrite = true; File configFile = new File("src/generatorConfig.xml"); System.out.println(configFile); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }}

将逆向工程生成的代码拷贝到指定项目中

将逆向工程生成的po,maper接口,mapper映射文件全部copy到我们的项目中就可以进行使用了

使用生成的代码进行测试

import java.util.Date;import java.util.List;import javax.annotation.Resource;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.scx.dao.ItemsMapper;import com.scx.domain.Items;import com.scx.domain.ItemsExample;import com.scx.domain.ItemsExample.Criteria;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Test { @Resource ItemsMapper itemsMapper; //根据主键查询商品 @org.junit.Test public void find() { Items item = itemsMapper.selectByPrimaryKey(1); System.out.println(item); } //添加商品 @org.junit.Test public void add(){ Items item=new Items(); item.setName("汽车"); item.setPrice(66666f); item.setCreatetime(new Date()); itemsMapper.insert(item); } //修改商品 @org.junit.Test public void update(){ Items item=itemsMapper.selectByPrimaryKey(4); item.setPrice(77777f); itemsMapper.updateByPrimaryKey(item); } //删除商品 @org.junit.Test public void delete(){ itemsMapper.deleteByPrimaryKey(4); } /* * mybatis反向工程会为我们自动生成example类 * example类与实体类对应,用作动态查询 */ @org.junit.Test public void myFind(){ ItemsExample example=new ItemsExample(); Criteria criteria = example.createCriteria(); criteria.andNameLike("汽车"); List items = itemsMapper.selectByExample(example); for (Items item : items) { System.out.println(item);

注意事项

Mapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件时,内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。 解决方法:删除原来已经生成的mapper xml文件再进行生成。 Mybatis自动生成的po及mapper.java文件不是内容而是直接覆盖没有此问题。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:php伪静态验证码不显示的解决方案(php验证码错误)
下一篇:如何为JLabel添加背景色和设置其imageicon的位置和大小
相关文章

 发表评论

暂时没有评论,来抢沙发吧~