mybatis 逆向工程的使用

十一,myBatis逆向工程

1.逆向工程简介

MyBatis Generator: 简称 MBG,是一个专门为 MyBatis 框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及 bean 类。支持基本的增删改查,以及 QBC 风格的条件查询。但是表连接、存储过程等这些复杂 sql 的定义需要我们手工编写。

官方文档地址
http://www.mybatis.org/generator/
官方工程地址
https://github.com/mybatis/generator/releases

2.逆向工程的配置

1)创建数据库

CREATE DATABASE `project_crowd` CHARACTER SET utf8;

use project_crowd;
drop table if exists t_admin;
create table t_admin
(
id int not null auto_increment, # 主键
login_acct varchar(255) not null, # 登录账号
user_pswd char(32) not null, # 登录密码
user_name varchar(255) not null, # 昵称
email varchar(255) not null, # 邮件地址
create_time char(19), # 创建时间
primary key (id)
);

2)添加pom文件坐标


<dependencies>
  <dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>3.2.8version>
  dependency>
dependencies>


<build>
  
  <plugins>
    
    <plugin>
      <groupId>org.mybatis.generatorgroupId>
      <artifactId>mybatis-generator-maven-pluginartifactId>
      <version>1.3.0version>
      
      <dependencies>
        
        <dependency>
          <groupId>org.mybatis.generatorgroupId>
          <artifactId>mybatis-generator-coreartifactId>
          <version>1.3.2version>
        dependency>
        
        <dependency>
          <groupId>com.mchangegroupId>
          <artifactId>c3p0artifactId>
          <version>0.9.2version>
        dependency>
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.8version>
        dependency>
      dependencies>
    plugin>
  plugins>
build>

3)编写generatorConfig.xml配置


<generatorConfiguration>
    
    <context id="atguiguTables" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/project_crowd"
                userId="root"
                password="123456">
        jdbcConnection>

        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>

        
        <javaModelGenerator targetProject=".\src\main\java"
                            targetPackage="com.atguigu.crowd.entity">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="trimStrings" value="true"/>
        javaModelGenerator>

        
        <sqlMapGenerator targetProject=".\src\main\java"
                         targetPackage="com.atguigu.crowd.mapper">
            
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER"
                             targetProject=".\src\main\java"
                             targetPackage="com.atguigu.crowd.mapper">
            
            <property name="enableSubPackages" value="false"/>
        javaClientGenerator>

        
        <table tableName="t_admin" domainObjectName="Admin"/>
    context>
generatorConfiguration>

4)生成对应代码

方式一:执行逆向生成操作的 Maven 命令

mybatis-generator:generate

方式二:代码生成器

public static void main(String[] args) throws Exception {
  // warnings 为用于放置生成过程中警告信息的集合对象
  List<String> warnings = new ArrayList<String>();
  // 指定是否覆盖重名文件
  boolean overwrite = true;
  // 加载配置文件
  File configFile = new File(MyBatisGenerator.class.getClassLoader().getResource("generatorConfig.xml").toURI());
  // 配置解析类
  ConfigurationParser cp = new ConfigurationParser(warnings);
  // 配置解析类解析配置文件并生成 Configuration 配置对象
  Configuration config = cp.parseConfiguration(configFile);
  // ShellCallback 负责如何处理重复文件
  ShellCallback callback = new DefaultShellCallback(overwrite);
  // 逆向工程对象
  MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  // 执行逆向文件生成操作
  myBatisGenerator.generate(null);
  // 打印提示信息
  System.out.println("MyBatis 逆向工程执行成功,刷新项目查看文件!");
}

3.逆向工程的使用

1)基本查询的测试

@Test
public void testSelect() throws Exception {
    SqlSessionFactory ssf = getSqlSessionFactory();
    SqlSession session = ssf.openSession();
  
    try {
        AdminMapper mapper =
        session.getMapper(AdminMapper.class);
        List<Admin> admins = mapper.selectAll();
        for (Admin admin : admins) {
        System.out.println(admin);
        }
    } finally {
    	session.close();
    }
}

2)带条件查询的测试

@Test
public void testSelect() throws Exception {
    SqlSessionFactory ssf = getSqlSessionFactory();
    SqlSession session = ssf.openSession();
    try {
        AdminMapper mapper =
        session.getMapper(AdminMapper.class);
        //条件查询: 昵称中带有'张' 并且 email中'j' 或者 id = 2
        AdminExample example = new AdminExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserNameLike("%张%");
        criteria.andEmailLike("%j%");
        //or
        Criteria criteriaOr = example.createCriteria();
        criteriaOr.andIdEqualTo(2);
        example.or(criteriaOr);
        List<Admin> admins = mapper.selectByExample(example);
        for (Admin admin : admins) {
        System.out.println(admin);
        }
    } finally {
    	session.close();
    }
}

Mybatis后续的学习:
mybatis 概述 | 配置文件详解:https://blog.csdn.net/weixin_45606067/article/details/107368570
mybatis 事务 | 动态SQL | 多表查询:https://blog.csdn.net/weixin_45606067/article/details/107368642
mybatis延迟加载 | 缓存机制详解:https://blog.csdn.net/weixin_45606067/article/details/107368706
mybatis 注解开发版:https://blog.csdn.net/weixin_45606067/article/details/107368743
pageHelper分页技术:https://blog.csdn.net/weixin_45606067/article/details/107368847


如果有收获!!! 希望老铁们来个三连,点赞、收藏、转发
创作不易,别忘点个赞,可以让更多的人看到这篇文章,顺便鼓励我写出更好的博客

你可能感兴趣的:(#,Mybatis,mybatis,mybatis逆向工程,逆向工程配置文件,逆向工程的使用)