`MyBatis`的逆向工程

`MyBatis`的逆向工程,第1张

MyBatis的逆向工程

概念:

1.正向工程:先创建java实体类,由框架负责根据实体类生成数据库表。Hibernate是支持正向工程的

2.逆向工程:先创建数据库表,由框架负责根据数据库表,反向生成如下资源:

  • Java实体类
  • Mapper接口
  • Mapper映射文件

使用步骤:

1.导入依赖Jar和插件

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.7version>
        dependency>
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.7version>
            dependency>
            
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
                <scope>testscope>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.3version>
            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>log4jgroupId>
                        <artifactId>log4jartifactId>
                        <version>1.2.12version>
                    dependency>
                    
                    
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.8version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>
2.创建MyBatis核心配置文件

主要创建的是:

mybatis-config的核心配置文件和jdbc.properties及添加log4j.xml

3.创建逆向工程的配置文件

文件名必须是:generatorConfig.xml

注意相关的配置


DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    
    <context id="DB2Tables" targetRuntime="MyBatis3">
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="root">
        jdbcConnection>
        
        <javaModelGenerator targetPackage="com.haikang.mybatis.pojo" targetProject=".\src\main\java">
        
            <property name="enableSubPackages" value="true" />
        
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="com.haikang.mybatis.mapper"  targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.haikang.mybatis.mapper"  targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>
        
        
        
        <table tableName="emp" domainObjectName="Emp"/>
        <table tableName="dept" domainObjectName="Dept"/>
    context>
generatorConfiguration>
4.打开maven找到对应的项目,再找到对应的mybatis-generatorgenerate插件

双击就可以

运行后结果:

重点注意的是:选择性添加和修改,当传入的参数为null时,选择性添加和修改,则不对该字段进行添加或修改,如果不是选择性添加和修改,如果当传入的参数为null时,该是会被添加为null或修改为null

选择性条件使用案例:【重点】

  EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").andAgeEqualTo(23);
逆工程在开发中使用是 MyBatis3: 生成带条件的CRUD(奢华尊享版)

方法:

逆工程使用的是QBC风格查询
public class BQCTest {
    // BQC删除功能
    @Test
    public void delete(){
        SqlSession sqlSession = SqlSessionUtils.sqlSession;
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        // 根据ID删除用户信息
        int i = mapper.deleteByPrimaryKey(21);
        System.out.println(i);
        // 根据条件进行删除
        EmpExample example = new EmpExample();
        EmpExample.Criteria criteria = example.createCriteria().andEmpNameLike("%a%");
        int result = mapper.deleteByExample(example);
        System.out.println(result);
    }

    @Test
    public void QBCInsert(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        // 普通插入
        int result = mapper.insert(new Emp(null,"大理",23,"男","123qq.com",null));
        System.out.println(result);
        // 选择性插入:
        int result2 = mapper.insertSelective(new Emp(null,"大理",23,"男","123qq.com",null));
        System.out.println(result2);
    }

    @Test
    public void QBCSelect(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        // 查询所有数据
        List<Emp> emps = mapper.selectByExample(null);
        emps.forEach(emp -> System.out.println(emp));

        // 根据条件查询
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").andAgeEqualTo(23);
        List<Emp> empList = mapper.selectByExample(empExample);
        empList.forEach(emp -> System.out.println(emp));

        // 根据ID查询
        Emp emp = mapper.selectByPrimaryKey(1);
        System.out.println(emp);
    }

    @Test
    public void BQCUpdate(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        //根据条件修改
        EmpExample empExample = new EmpExample();
        EmpExample.Criteria criteria = empExample.createCriteria().andEmpNameEqualTo("大理").
                andAgeEqualTo(23).andDidEqualTo(2);

        int result = mapper.updateByExampleSelective(new Emp(25,"大理",23,"男","123qq.com",null),empExample);
        System.out.println(result);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/874432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存