当前位置: 首页 > 图灵资讯 > 技术篇> mybatis逆向工程基础版

mybatis逆向工程基础版

来源:图灵教育
时间:2023-06-09 10:14:25

1.基本介绍120

介绍所谓的逆向⼯程是:数据库表逆向⽣pojo类成为Java,SqlMapper.xml⽂以及Mapper连接⼝等等。完成这一点⼯工作,需要别人的帮助⼈写好的逆向⼯程插件。

思考:使⽤这个插件需要配置哪些信息?

●pojo类名、包名、⽣成位置。

●SqlMapper.xml⽂件名以及⽣成位置。

●Mapper连接⼝名以及⽣成位置。

●连接数据库的信息。

●指定哪些表参与逆向⼯程。

●...

2.逆向⼯程配置与⽣成1212.1第⼀步骤:准备基本环境

新建模块

打包⽅式:jar

2.2第⼆步骤:在pom中添加逆向⼯程插件

<!--配置mybatis逆向工程插件-->    <!--定制施工过程-->    <build>        <!--可配置多个插件-->        <plugins>            <!--其中一个插件:mybatis逆向工程插件-->            <plugin>                <!--GAV坐标插件-->                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.4.1</version>                <!--允许覆盖-->                <configuration>                    <overwrite>true</overwrite>                </configuration>                <!--插件的依赖-->                <dependencies>                    <!--mysql驱动依赖-->                    <dependency>                        <groupId>mysql</groupId>                        <artifactId>mysql-connector-java</artifactId>                        <version>8.0.30</version>                    </dependency>                </dependencies>            </plugin>        </plugins>    </build>

2.3第三步:配置generatorconfig.xml

该⽂名称必须称为:generatorConfig.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:生成的是基本版,只有基本的增删改查。--        targetruntime有两个值:            MyBatis3Simple:生成的是基本版,只有基本的增删改查。            MyBatis3:除了基本的增删改查外,还生成了增删改查的复杂版本。    -->    <context id="DB2Tables" targetRuntime=“MyBatis3Simple">        <!--防止生成重复代码-->        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>        <commentGenerator>            <!--生成日期是否去除?-->            <property name="suppressDate" value="true"/>            <!--是否去除注释-->            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--连接数据库信息-->        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"                        connectionURL="jdbc:mysql://localhost:3306/powernode"                        userId="root"                        password="root">        </jdbcConnection>        <!-- 生成pojo包名和位置 -->        <javaModelGenerator targetPackage="com.powernode.mybatis.pojo" targetProject="src/main/java">            <!--子包是否打开-->            <property name="enableSubPackages" value="true"/>            <!--字段名的前后空白是否去除?-->            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!-- 生成SQL映射文件的包名和位置 -->        <sqlMapGenerator targetPackage="com.powernode.mybatis.mapper" targetProject="src/main/resources">            <!--子包是否打开-->            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!-- 生成Mapper接口的包名和位置 -->        <javaClientGenerator                type="xmlMapper"                targetPackage="com.powernode.mybatis.mapper"                targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!-- 表名和相应的实体类名-->        <table tableName="t_car" domainObjectName="Car"/>    </context></generatorConfiguration>

第四步:运⾏插件

双击红框选项生成CarMaperSQL映射文件CarMaperper.xml

mybatis逆向工程基础版_mybatis

mybatis逆向工程基础版_sql_02

3.测试逆向⼯程⽣成的是否好⽤1223.1准备环境121

依赖:mybatis依赖,mysql驱动依赖,junit依赖,logback依赖

jdbc.properties

mybatis-config.xml

logback.xml

3.2编写测试程序1222

//按主键删除    @Test    public void testDeleteByPrimaryKey(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        int count = mapper.deleteByPrimaryKey(38L);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }

mybatis逆向工程基础版_逆向工程_03

///查询一切    @Test    public void testSelectAll(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        List<Car> cars = mapper.selectAll();        cars.forEach(car -> System.out.println(car));        sqlSession.close();    }

mybatis逆向工程基础版_逆向工程_04

4.代码汇总main中com.powernode.mybatis.mapperCarMapper

package com.powernode.mybatis.mapper;import com.powernode.mybatis.pojo.Car;import java.util.List;public interface CarMapper {    int deleteByPrimaryKey(Long id);    int insert(Car row);    Car selectByPrimaryKey(Long id);    List<Car> selectAll();    int updateByPrimaryKey(Car row);}

CarMapper.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.powernode.mybatis.mapper.CarMapper">  <resultMap id="BaseResultMap" type="com.powernode.mybatis.pojo.Car">    <id column="id" jdbcType="BIGINT" property="id" />    <result column="car_num" jdbcType="VARCHAR" property="carNum" />    <result column="brand" jdbcType="VARCHAR" property="brand" />    <result column="guide_price" jdbcType="DECIMAL" property="guidePrice" />    <result column="produce_time" jdbcType="CHAR" property="produceTime" />    <result column="car_type" jdbcType="VARCHAR" property="carType" />  </resultMap>  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">    delete from t_car    where id = #{id,jdbcType=BIGINT}  </delete>  <insert id="insert" parameterType="com.powernode.mybatis.pojo.Car">    insert into t_car (id, car_num, brand,       guide_price, produce_time, car_type      )    values (#{id,jdbcType=BIGINT}, #{carNum,jdbcType=VARCHAR}, #{brand,jdbcType=VARCHAR},       #{guidePrice,jdbcType=DECIMAL}, #{produceTime,jdbcType=CHAR}, #{carType,jdbcType=VARCHAR}      )  </insert>  <update id="updateByPrimaryKey" parameterType="com.powernode.mybatis.pojo.Car">    update t_car    set car_num = #{carNum,jdbcType=VARCHAR},      brand = #{brand,jdbcType=VARCHAR},      guide_price = #{guidePrice,jdbcType=DECIMAL},      produce_time = #{produceTime,jdbcType=CHAR},      car_type = #{carType,jdbcType=VARCHAR}    where id = #{id,jdbcType=BIGINT}  </update>  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">    select id, car_num, brand, guide_price, produce_time, car_type    from t_car    where id = #{id,jdbcType=BIGINT}  </select>  <select id="selectAll" resultMap="BaseResultMap">    select id, car_num, brand, guide_price, produce_time, car_type    from t_car  </select></mapper>

test中com.powernode.mybatis.testCarMapperTest

package com.powernode.mybatis.test;import com.powernode.mybatis.mapper.CarMapper;import com.powernode.mybatis.pojo.Car;import com.powernode.mybatis.utils.SqlSessionUtil;import org.apache.ibatis.session.SqlSession;import org.junit.Test;import java.util.List;//测试mybatis逆向工程  122public class CarMapperTest {    ///查询一切    @Test    public void testSelectAll(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        List<Car> cars = mapper.selectAll();        cars.forEach(car -> System.out.println(car));        sqlSession.close();    }    //按主键删除    @Test    public void testDeleteByPrimaryKey(){        SqlSession sqlSession = SqlSessionUtil.openSession();        CarMapper mapper = sqlSession.getMapper(CarMapper.class);        int count = mapper.deleteByPrimaryKey(38L);        System.out.println(count);        sqlSession.commit();        sqlSession.close();    }}

generatorConfig.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:生成的是基本版,只有基本的增删改查。--        targetruntime有两个值:            MyBatis3Simple:生成的是基本版,只有基本的增删改查。            MyBatis3:除了基本的增删改查外,还生成了增删改查的复杂版本。    -->    <context id="DB2Tables" targetRuntime=“MyBatis3Simple">        <!--防止生成重复代码-->        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>        <commentGenerator>            <!--生成日期是否去除?-->            <property name="suppressDate" value="true"/>            <!--是否去除注释-->            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <!--连接数据库信息-->        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"                        connectionURL="jdbc:mysql://localhost:3306/powernode"                        userId="root"                        password="lzl">        </jdbcConnection>        <!-- 生成pojo包名和位置 -->        <javaModelGenerator targetPackage="com.powernode.mybatis.pojo" targetProject="src/main/java">            <!--子包是否打开-->            <property name="enableSubPackages" value="true"/>            <!--字段名的前后空白是否去除?-->            <property name="trimStrings" value="true"/>        </javaModelGenerator>        <!-- 生成SQL映射文件的包名和位置 -->        <sqlMapGenerator targetPackage="com.powernode.mybatis.mapper" targetProject="src/main/resources">            <!--子包是否打开-->            <property name="enableSubPackages" value="true"/>        </sqlMapGenerator>        <!-- 生成Mapper接口的包名和位置 -->        <javaClientGenerator                type="xmlMapper"                targetPackage="com.powernode.mybatis.mapper"                targetProject="src/main/java">            <property name="enableSubPackages" value="true"/>        </javaClientGenerator>        <!-- 表名和相应的实体类名-->        <table tableName="t_car" domainObjectName="Car"/>    </context></generatorConfiguration>

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.powernode</groupId>    <artifactId>course23</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <dependencies>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.5.10</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.13.2</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>1.2.11</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.30</version>        </dependency>    </dependencies>    <properties>        <!--    jdk版本用于编译代码-->        <maven.compiler.source>1.8</maven.compiler.source>        <!--    使用jdk版本的操作程序-->        <maven.compiler.target>1.8</maven.compiler.target>    </properties>    <!--配置mybatis逆向工程插件-->    <!--配置mybatis逆向工程插件-->    <!--定制施工过程-->    <build>        <!--可配置多个插件-->        <plugins>            <!--其中一个插件:mybatis逆向工程插件-->            <plugin>                <!--GAV坐标插件-->                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.4.1</version>                <!--允许覆盖-->                <configuration>                    <overwrite>true</overwrite>                </configuration>                <!--插件的依赖-->                <dependencies>                    <!--mysql驱动依赖-->                    <dependency>                        <groupId>mysql</groupId>                        <artifactId>mysql-connector-java</artifactId>                        <version>8.0.30</version>                    </dependency>                </dependencies>            </plugin>        </plugins>    </build></project>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <properties resource="jdbc.properties"/>    <settings>        <setting name="mapUnderscoreToCamelCase" value="true"/>    </settings>    <typeAliases>        <package name="com.powernode.mybatis.pojo"/>    </typeAliases>    <environments default="dev">        <environment id="dev">            <transactionManager type="JDBC"/>            <dataSource type="POOLED">                <property name="driver" value="${jdbc.driver}"/>                <property name="url" value="${jdbc.url}"/>                <property name="username" value="${jdbc.username}"/>                <property name="password" value="${jdbc.password}"/>            </dataSource>        </environment>    </environments>    <mappers>        <package name="com.powernode.mybatis.mapper"/>    </mappers></configuration>

剩余的

pojo

utils

logback.xml

jdbc.properties

不做赘述

上一篇:

回顾注解开发

下一篇:

mybatis的缓存