逆向工程

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

企业实际开发中,常用的逆向工程方式:
由于数据库的表生成java代码

使用方法:

逆向工程_第1张图片
2016-11-26_231848.png

generatorConfig.xml





    
        
            
            
        
        
        
        
        

        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        

GeneratorSqlmap.java



import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

    public void generator() throws Exception{

        List warnings = new ArrayList();
        boolean overwrite = true;
        
        File configFile = new File("generatorConfig.xml"); 
        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);

    } 
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

执行main方法生成对应文件

测试类:

package cn.ztc.ssm.mapper;

import static org.junit.Assert.*;

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.ztc.ssm.po.Items;
import cn.ztc.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 testDeleteByPrimaryKey() {
        fail("Not yet implemented");
    }

    @Test
    public void testInsert() {
        Items items = new Items();
        items.setName("手机");
        items.setPrice(999f);
        itemsMapper.insert(items);
    }

    @Test
    public void testSelectByExample() {
        ItemsExample itemsExample = new ItemsExample();
        ItemsExample.Criteria criteria = itemsExample.createCriteria();
        criteria.andNameEqualTo("台式机");
        //可能返回多条记录
        List list = itemsMapper.selectByExample(itemsExample);
        System.out.println(list);
    }

    @Test
    public void testSelectByPrimaryKey() {
        Items items = itemsMapper.selectByPrimaryKey(1);
        System.out.println(items);
    }

    @Test
    public void testUpdateByPrimaryKey() {
        //将所有字段字段跟新,需要先查询出来在更新
        Items items = itemsMapper.selectByPrimaryKey(1);
        items.setName("水杯");
        itemsMapper.updateByPrimaryKey(items);
        //批量更新,不需要先查询
        //itemsMapper.updateByPrimaryKeySelective(record)
    }

}

你可能感兴趣的:(逆向工程)