【MyBatis】的逆向工程

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

2.下载逆向工程
https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2
【MyBatis】的逆向工程_第1张图片

3.创建逆向工程
【MyBatis】的逆向工程_第2张图片

1.创建Generator.java
@Test
public void generator() throws Exception{
    List<String> warnings = new ArrayList<String>();
    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);
} 

2.添加generatorConfig.xml



<generatorConfiguration>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/taotao" userId="java"
            password="123456">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.taotao.pojo"
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.taotao.mapper" 
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.taotao.mapper" 
            targetProject=".\src">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        <table schema="" tableName="tb_content">table>
        <table schema="" tableName="tb_content_category">table>
        <table schema="" tableName="tb_item">table>
        <table schema="" tableName="tb_item_cat">table>
        <table schema="" tableName="tb_item_desc">table>
        <table schema="" tableName="tb_item_param">table>
        <table schema="" tableName="tb_item_param_item">table>
        <table schema="" tableName="tb_order">table>
        <table schema="" tableName="tb_order_item">table>
        <table schema="" tableName="tb_order_shipping">table>
        <table schema="" tableName="tb_user">table>
    context>
generatorConfiguration>

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

4.使用逆向工程生成的代码
@Autowired
private TbItemMapper itemMapper;    
@Override
public TbItem queryItemById(Long itemId) throws Exception {
//  TbItem item = itemMapper.selectByPrimaryKey(itemId);

    TbItemExample example = new TbItemExample();
    //创建查询条件
    Criteria criteria = example.createCriteria();
    criteria.andIdEqualTo(itemId);
    List<TbItem> list = itemMapper.selectByExample(example);
    TbItem item = null;
    if(list != null && list.size() > 0 ){
        item = list.get(0);
    }
    return item;
}

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

你可能感兴趣的:(mybatis)