MyBatis 逆向工程

1、逆向工程简介
  1) MyBatis Generator: 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写
  官方文档地址
    http://www.mybatis.org/generator/
  官方工程地址
    https://github.com/mybatis/generator/releases

2、逆向工程的配置
  1) 导入逆向工程的jar包
    mybatis-generator-core-1.3.2.jar
  2) 编写MBG的配置文件mbg.xml(重要几处配置),可参考官方手册

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <!-- 
          targetRuntime: 执行生成的逆向工程的版本
                         MyBatis3Simple: 生成基本的CRUD
                         MyBatis3: 生成带条件的CRUD
   -->
  <context id="DB2Tables" targetRuntime="MyBatis3">
  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/mybatis_1129"
        userId="root"
        password="1234">
    </jdbcConnection>
    <!-- javaBean的生成策略-->
    <javaModelGenerator targetPackage="com.atguigu.mybatis.beans" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- SQL映射文件的生成策略 -->
    <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"  targetProject=".\conf">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    
    <!-- Mapper接口的生成策略 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 逆向分析的表 -->
    <table tableName="tbl_dept" domainObjectName="Department"></table>
    <table tableName="tbl_employee" domainObjectName="Employee"></table>
  </context>
</generatorConfiguration>
复制代码

    3) 运行代码生成器生成代码

复制代码
@Test
public void testMBG() throws Exception {
           List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("mbg.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);
}
复制代码

 

 逆向工程的使用
  1) 基本查询的测试

复制代码
@Test
    public void testSelect() throws Exception {
        SqlSessionFactory ssf = getSqlSessionFactory();
        SqlSession session = ssf.openSession();
        
        try {
            EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
            List<Employee> emps = mapper.selectAll();
            for (Employee employee : emps) {
                System.out.println(employee);
            }
        } finally {
            session.close();
        }
}
复制代码

  2) 带条件查询的测试

复制代码
@Test
    public void testSelect() throws Exception {
        SqlSessionFactory ssf = getSqlSessionFactory();
        SqlSession session = ssf.openSession();
        try {
            EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
            //条件查询: 名字中带有'张' 并且 email中'j'  或者 did = 2 
            EmployeeExample example =  new EmployeeExample();
            Criteria criteria = example.createCriteria();
            criteria.andLastNameLike("%张%");
            criteria.andEmailLike("%j%");
            //or 
            Criteria criteriaOr = example.createCriteria();
            criteriaOr.andDIdEqualTo(2);
            example.or(criteriaOr);
            List<Employee> emps = mapper.selectByExample(example);
            for (Employee employee : emps) {
                System.out.println(employee);
            }
        } finally {
            session.close();
        }
}
复制代码

 

posted @   kkzhang  阅读(344)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示