返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis逆向⼯程的生成过程
  • 544
分享到

MyBatis逆向⼯程的生成过程

MyBatis逆向⼯程MyBatis逆向生成 2023-02-15 12:02:39 544人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录一、mybatis的逆向⼯程1.逆向⼯程配置与⽣成2.测试生成的逆向⼯程一、MyBatis的逆向⼯程 (1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,sqlMa

一、MyBatis的逆向⼯程

(1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,sqlMapper.xml⽂件,以及Mapper接⼝类等,这真是一个很强大的功能。 要完成这个⼯作,需要借助别⼈写好的逆向⼯程插件

(2)思考:使⽤这个插件的话,需要给这个插件配置哪些信息?

①pojo类名、包名以及⽣成位置。

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

③Mapper接⼝名以及⽣成位置。

④连接数据库的信息。

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

......

1.逆向⼯程配置与⽣成

(1)基础环境准备

新建一个普通的Maven模块:mybatis-012-generator

打包⽅式:jar

(2)在pom.xml中添加逆向⼯程插件

①先引入mybatis逆向⼯程的插件,引入仓库中对应的插件坐标。

②允许覆盖:表示原来这些文件存在,true就以覆盖的方式生成文件,false就以追加的方式生成文件。

③引入插件的依赖:我们需要根据数据库表逆向生成pojo类、SqlMapper接口和里面的方法、SqlMapper.xml配置文件等,所以肯定需要Mysql的驱动。

<?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.bjpowernode</groupId>
    <artifactId>mybatis-012-generator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <!--定制构建过程-->
    <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>5.1.23</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

(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:生成的是基础版,只有基本的增删改查。
            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.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        passWord="123">
        </jdbcConnection>
        <!-- 生成pojo包名和位置 -->
        <javaModelGenerator targetPackage="com.bjpowernode.mybatis.pojo" targetProject="src/main/java">
            <!--是否开启子包-->
            <property name="enableSubPackages" value="true"/>
            <!--是否去除字段名的前后空白-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成SQL映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.bjpowernode.mybatis.mapper" targetProject="src/main/resources">
            <!--是否开启子包-->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成Mapper接口的包名和位置 -->
        <javaClientGenerator
                type="xmlMapper"
                targetPackage="com.bjpowernode.mybatis.mapper"
                targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 表名和对应的实体类名-->
        <table tableName="t_car" domainObjectName="Car"/>
    </context>
</generatorConfiguration>

(4)双击运行插件,就可以生成基础版的配置信息

自动生成的CarMapper接口、pojo类Car、CarMapper.xml配置文件

CarMapper接口

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.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);
}

pojo类Car

注:生成的pojo类并没有重写toString方法

package com.bjpowernode.mybatis.pojo;
import java.math.BigDecimal;
public class Car {
    private Long id;
    private String carNum;
    private String brand;
    private BigDecimal guidePrice;
    private String produceTime;
    private String carType;
    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", carNum='" + carNum + '\'' +
                ", brand='" + brand + '\'' +
                ", guidePrice=" + guidePrice +
                ", produceTime='" + produceTime + '\'' +
                ", carType='" + carType + '\'' +
                '}';
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCarNum() {
        return carNum;
    }
    public void setCarNum(String carNum) {
        this.carNum = carNum == null ? null : carNum.trim();
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand == null ? null : brand.trim();
    }
    public BigDecimal getGuidePrice() {
        return guidePrice;
    }
    public void setGuidePrice(BigDecimal guidePrice) {
        this.guidePrice = guidePrice;
    }
    public String getProduceTime() {
        return produceTime;
    }
    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime == null ? null : produceTime.trim();
    }
    public String getCarType() {
        return carType;
    }
    public void setCarType(String carType) {
        this.carType = carType == null ? null : carType.trim();
    }
}

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.bjpowernode.mybatis.mapper.CarMapper">
  <resultMap id="BaseResultMap" type="com.bjpowernode.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.bjpowernode.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.bjpowernode.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>

2.测试生成的逆向⼯程

(1)环境准备

①依赖:mybatis依赖、mysql驱动依赖、junit依赖、logback依赖

②jdbc.properties、mybatis-config.xml、logback.xml

③拷贝工具类:SqlSessionUtil

(2)编写测试程序(基础版)

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public 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();
    }
}

执行结果:

(3)配置generatorConfig.xml(增强版)

生成了两个pojo类,并且对于接口中的方法也变多了

CarMapper接口

package com.bjpowernode.mybatis.mapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.pojo.CarExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface CarMapper {
    long countByExample(CarExample example);
    int deleteByExample(CarExample example);
    int deleteByPrimaryKey(Long id);
    int insert(Car row);
    int insertSelective(Car row);
    List<Car> selectByExample(CarExample example);
    Car selectByPrimaryKey(Long id);
    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);
    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);
    int updateByPrimaryKeySelective(Car row);
    int updateByPrimaryKey(Car row);
}

pojo类Car

package com.bjpowernode.mybatis.pojo;
import java.math.BigDecimal;
public class Car {
    private Long id;
    private String carNum;
    private String brand;
    private BigDecimal guidePrice;
    private String produceTime;
    private String carType;
    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", carNum='" + carNum + '\'' +
                ", brand='" + brand + '\'' +
                ", guidePrice=" + guidePrice +
                ", produceTime='" + produceTime + '\'' +
                ", carType='" + carType + '\'' +
                '}';
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCarNum() {
        return carNum;
    }
    public void setCarNum(String carNum) {
        this.carNum = carNum == null ? null : carNum.trim();
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand == null ? null : brand.trim();
    }
    public BigDecimal getGuidePrice() {
        return guidePrice;
    }
    public void setGuidePrice(BigDecimal guidePrice) {
        this.guidePrice = guidePrice;
    }
    public String getProduceTime() {
        return produceTime;
    }
    public void setProduceTime(String produceTime) {
        this.produceTime = produceTime == null ? null : produceTime.trim();
    }
    public String getCarType() {
        return carType;
    }
    public void setCarType(String carType) {
        this.carType = carType == null ? null : carType.trim();
    }
}

pojo类CarExample:封装查询条件的类

package com.bjpowernode.mybatis.pojo;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class CarExample {
    protected String orderByClause;
    protected boolean distinct;
    protected List<Criteria> oredCriteria;
    public CarExample() {
        oredCriteria = new ArrayList<>();
    }
    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }
    public String getOrderByClause() {
        return orderByClause;
    }
    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }
    public boolean isDistinct() {
        return distinct;
    }
    public List<Criteria> getOredCriteria() {
        return oredCriteria;
    }
    public void or(Criteria criteria) {
        oredCriteria.add(criteria);
    }
    public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }
    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }
    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }
    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }
    protected abstract static class GeneratedCriteria {
        protected List<Criterion> criteria;
        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<>();
        }
        public boolean isValid() {
            return criteria.size() > 0;
        }
        public List<Criterion> getAllCriteria() {
            return criteria;
        }
        public List<Criterion> getCriteria() {
            return criteria;
        }
        protected void addCriterion(String condition) {
            if (condition == null) {
                throw new RuntimeException("Value for condition cannot be null");
            }
            criteria.add(new Criterion(condition));
        }
        protected void addCriterion(String condition, Object value, String property) {
            if (value == null) {
                throw new RuntimeException("Value for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value));
        }
        protected void addCriterion(String condition, Object value1, Object value2, String property) {
            if (value1 == null || value2 == null) {
                throw new RuntimeException("Between values for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value1, value2));
        }
        public Criteria andIdIsNull() {
            addCriterion("id is null");
            return (Criteria) this;
        }
        public Criteria andIdIsNotNull() {
            addCriterion("id is not null");
            return (Criteria) this;
        }
        public Criteria andIdEqualTo(Long value) {
            addCriterion("id =", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdNotEqualTo(Long value) {
            addCriterion("id <>", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdGreaterThan(Long value) {
            addCriterion("id >", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdGreaterThanOrEqualTo(Long value) {
            addCriterion("id >=", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdLessThan(Long value) {
            addCriterion("id <", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdLessThanOrEqualTo(Long value) {
            addCriterion("id <=", value, "id");
            return (Criteria) this;
        }
        public Criteria andIdIn(List<Long> values) {
            addCriterion("id in", values, "id");
            return (Criteria) this;
        }
        public Criteria andIdNotIn(List<Long> values) {
            addCriterion("id not in", values, "id");
            return (Criteria) this;
        }
        public Criteria andIdBetween(Long value1, Long value2) {
            addCriterion("id between", value1, value2, "id");
            return (Criteria) this;
        }
        public Criteria andIdNotBetween(Long value1, Long value2) {
            addCriterion("id not between", value1, value2, "id");
            return (Criteria) this;
        }
        public Criteria andCarNumIsNull() {
            addCriterion("car_num is null");
            return (Criteria) this;
        }
        public Criteria andCarNumIsNotNull() {
            addCriterion("car_num is not null");
            return (Criteria) this;
        }
        public Criteria andCarNumEqualTo(String value) {
            addCriterion("car_num =", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumNotEqualTo(String value) {
            addCriterion("car_num <>", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumGreaterThan(String value) {
            addCriterion("car_num >", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumGreaterThanOrEqualTo(String value) {
            addCriterion("car_num >=", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumLessThan(String value) {
            addCriterion("car_num <", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumLessThanOrEqualTo(String value) {
            addCriterion("car_num <=", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumLike(String value) {
            addCriterion("car_num like", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumNotLike(String value) {
            addCriterion("car_num not like", value, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumIn(List<String> values) {
            addCriterion("car_num in", values, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumNotIn(List<String> values) {
            addCriterion("car_num not in", values, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumBetween(String value1, String value2) {
            addCriterion("car_num between", value1, value2, "carNum");
            return (Criteria) this;
        }
        public Criteria andCarNumNotBetween(String value1, String value2) {
            addCriterion("car_num not between", value1, value2, "carNum");
            return (Criteria) this;
        }
        public Criteria andBrandIsNull() {
            addCriterion("brand is null");
            return (Criteria) this;
        }
        public Criteria andBrandIsNotNull() {
            addCriterion("brand is not null");
            return (Criteria) this;
        }
        public Criteria andBrandEqualTo(String value) {
            addCriterion("brand =", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandNotEqualTo(String value) {
            addCriterion("brand <>", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandGreaterThan(String value) {
            addCriterion("brand >", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandGreaterThanOrEqualTo(String value) {
            addCriterion("brand >=", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandLessThan(String value) {
            addCriterion("brand <", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandLessThanOrEqualTo(String value) {
            addCriterion("brand <=", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandLike(String value) {
            addCriterion("brand like", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandNotLike(String value) {
            addCriterion("brand not like", value, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandIn(List<String> values) {
            addCriterion("brand in", values, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandNotIn(List<String> values) {
            addCriterion("brand not in", values, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandBetween(String value1, String value2) {
            addCriterion("brand between", value1, value2, "brand");
            return (Criteria) this;
        }
        public Criteria andBrandNotBetween(String value1, String value2) {
            addCriterion("brand not between", value1, value2, "brand");
            return (Criteria) this;
        }
        public Criteria andGuidePriceIsNull() {
            addCriterion("guide_price is null");
            return (Criteria) this;
        }
        public Criteria andGuidePriceIsNotNull() {
            addCriterion("guide_price is not null");
            return (Criteria) this;
        }
        public Criteria andGuidePriceEqualTo(BigDecimal value) {
            addCriterion("guide_price =", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceNotEqualTo(BigDecimal value) {
            addCriterion("guide_price <>", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceGreaterThan(BigDecimal value) {
            addCriterion("guide_price >", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceGreaterThanOrEqualTo(BigDecimal value) {
            addCriterion("guide_price >=", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceLessThan(BigDecimal value) {
            addCriterion("guide_price <", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceLessThanOrEqualTo(BigDecimal value) {
            addCriterion("guide_price <=", value, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceIn(List<BigDecimal> values) {
            addCriterion("guide_price in", values, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceNotIn(List<BigDecimal> values) {
            addCriterion("guide_price not in", values, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceBetween(BigDecimal value1, BigDecimal value2) {
            addCriterion("guide_price between", value1, value2, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andGuidePriceNotBetween(BigDecimal value1, BigDecimal value2) {
            addCriterion("guide_price not between", value1, value2, "guidePrice");
            return (Criteria) this;
        }
        public Criteria andProduceTimeIsNull() {
            addCriterion("produce_time is null");
            return (Criteria) this;
        }
        public Criteria andProduceTimeIsNotNull() {
            addCriterion("produce_time is not null");
            return (Criteria) this;
        }
        public Criteria andProduceTimeEqualTo(String value) {
            addCriterion("produce_time =", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeNotEqualTo(String value) {
            addCriterion("produce_time <>", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeGreaterThan(String value) {
            addCriterion("produce_time >", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeGreaterThanOrEqualTo(String value) {
            addCriterion("produce_time >=", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeLessThan(String value) {
            addCriterion("produce_time <", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeLessThanOrEqualTo(String value) {
            addCriterion("produce_time <=", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeLike(String value) {
            addCriterion("produce_time like", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeNotLike(String value) {
            addCriterion("produce_time not like", value, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeIn(List<String> values) {
            addCriterion("produce_time in", values, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeNotIn(List<String> values) {
            addCriterion("produce_time not in", values, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeBetween(String value1, String value2) {
            addCriterion("produce_time between", value1, value2, "produceTime");
            return (Criteria) this;
        }
        public Criteria andProduceTimeNotBetween(String value1, String value2) {
            addCriterion("produce_time not between", value1, value2, "produceTime");
            return (Criteria) this;
        }
        public Criteria andCarTypeIsNull() {
            addCriterion("car_type is null");
            return (Criteria) this;
        }
        public Criteria andCarTypeIsNotNull() {
            addCriterion("car_type is not null");
            return (Criteria) this;
        }
        public Criteria andCarTypeEqualTo(String value) {
            addCriterion("car_type =", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeNotEqualTo(String value) {
            addCriterion("car_type <>", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeGreaterThan(String value) {
            addCriterion("car_type >", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeGreaterThanOrEqualTo(String value) {
            addCriterion("car_type >=", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeLessThan(String value) {
            addCriterion("car_type <", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeLessThanOrEqualTo(String value) {
            addCriterion("car_type <=", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeLike(String value) {
            addCriterion("car_type like", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeNotLike(String value) {
            addCriterion("car_type not like", value, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeIn(List<String> values) {
            addCriterion("car_type in", values, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeNotIn(List<String> values) {
            addCriterion("car_type not in", values, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeBetween(String value1, String value2) {
            addCriterion("car_type between", value1, value2, "carType");
            return (Criteria) this;
        }
        public Criteria andCarTypeNotBetween(String value1, String value2) {
            addCriterion("car_type not between", value1, value2, "carType");
            return (Criteria) this;
        }
    }
    public static class Criteria extends GeneratedCriteria {
        protected Criteria() {
            super();
        }
    }
    public static class Criterion {
        private String condition;
        private Object value;
        private Object secondValue;
        private boolean noValue;
        private boolean singleValue;
        private boolean betweenValue;
        private boolean listValue;
        private String typeHandler;
        public String getCondition() {
            return condition;
        }
        public Object getValue() {
            return value;
        }
        public Object getSecondValue() {
            return secondValue;
        }
        public boolean isNoValue() {
            return noValue;
        }
        public boolean isSingleValue() {
            return singleValue;
        }
        public boolean isBetweenValue() {
            return betweenValue;
        }
        public boolean isListValue() {
            return listValue;
        }
        public String getTypeHandler() {
            return typeHandler;
        }
        protected Criterion(String condition) {
            super();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
        }
        protected Criterion(String condition, Object value, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
        }
        protected Criterion(String condition, Object value) {
            this(condition, value, null);
        }
        protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
        }
        protected Criterion(String condition, Object value, Object secondValue) {
            this(condition, value, secondValue, null);
        }
    }
}

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.bjpowernode.mybatis.mapper.CarMapper">
  <resultMap id="BaseResultMap" type="com.bjpowernode.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>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, car_num, brand, guide_price, produce_time, car_type
  </sql>
  <select id="selectByExample" parameterType="com.bjpowernode.mybatis.pojo.CarExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_car
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from t_car
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.bjpowernode.mybatis.pojo.CarExample">
    delete from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.bjpowernode.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>
  <insert id="insertSelective" parameterType="com.bjpowernode.mybatis.pojo.Car">
    insert into t_car
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="carNum != null">
        car_num,
      </if>
      <if test="brand != null">
        brand,
      </if>
      <if test="guidePrice != null">
        guide_price,
      </if>
      <if test="produceTime != null">
        produce_time,
      </if>
      <if test="carType != null">
        car_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="carNum != null">
        #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        #{carType,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.bjpowernode.mybatis.pojo.CarExample" resultType="java.lang.Long">
    select count(*) from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update t_car
    <set>
      <if test="row.id != null">
        id = #{row.id,jdbcType=BIGINT},
      </if>
      <if test="row.carNum != null">
        car_num = #{row.carNum,jdbcType=VARCHAR},
      </if>
      <if test="row.brand != null">
        brand = #{row.brand,jdbcType=VARCHAR},
      </if>
      <if test="row.guidePrice != null">
        guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="row.produceTime != null">
        produce_time = #{row.produceTime,jdbcType=CHAR},
      </if>
      <if test="row.carType != null">
        car_type = #{row.carType,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update t_car
    set id = #{row.id,jdbcType=BIGINT},
      car_num = #{row.carNum,jdbcType=VARCHAR},
      brand = #{row.brand,jdbcType=VARCHAR},
      guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      produce_time = #{row.produceTime,jdbcType=CHAR},
      car_type = #{row.carType,jdbcType=VARCHAR}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.bjpowernode.mybatis.pojo.Car">
    update t_car
    <set>
      <if test="carNum != null">
        car_num = #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        brand = #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        guide_price = #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        produce_time = #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        car_type = #{carType,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.bjpowernode.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>
</mapper>

(4)编写测试程序(增强版)

(1)增强版的查询方式就比较特殊,特别是根据条件查询,是QBC 风格:Query By Criteria 一种查询方式,比较面向对象,看不到sql语句!

(2)条件查询步骤:

①先封装条件,通过CarExample对象来封装查询条件

②调用carExample.createCriteria()方法来创建查询条件,后面通过" 点. "的方式跟上方法

package com.bjpowernode.mybatis.test;
import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.pojo.CarExample;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
public class CarMapperTest {
    @Test
    public void testSelect(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        // 执行查询
        // 1. 查询一个
        Car car = mapper.selectByPrimaryKey(34L);
        System.out.println(car);
        // 2. 查询所有(selectByExample,根据条件查询,如果条件是null表示没有条件)
        List<Car> cars = mapper.selectByExample(null);
        cars.forEach(car1 -> System.out.println(car1));
        System.out.println("==================");
        // 3.按照条件进行查询
        // QBC 风格:Query By Criteria 一种查询方式,比较面向对象,看不到sql语句。
        // 3.1 封装条件,通过CarExample对象来封装查询条件
        CarExample carExample = new CarExample();
        // 3.2调用carExample.createCriteria()方法来创建查询条件
        carExample.createCriteria().andBrandLike("帕萨特").
                                    andGuidePriceGreaterThan(new BigDecimal(20.0));
        // 添加or
        carExample.or().andCarTypeEqualTo("燃油车");
        // 执行查询
        List<Car> cars1 = mapper.selectByExample(carExample);
        cars1.forEach(car1 -> System.out.println(car1));
        sqlSession.close();
    }
}

以上的SQL语句就等价于:

到此这篇关于MyBatis逆向⼯程的生成过程的文章就介绍到这了,更多相关MyBatis逆向⼯程内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: MyBatis逆向⼯程的生成过程

本文链接: https://lsjlt.com/news/196169.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • MyBatis逆向⼯程的生成过程
    目录一、MyBatis的逆向⼯程1.逆向⼯程配置与⽣成2.测试生成的逆向⼯程一、MyBatis的逆向⼯程 (1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,SqlMa...
    99+
    2023-02-15
    MyBatis逆向⼯程 MyBatis逆向生成
  • IDEA 中生成 MyBatis 逆向工程实践
    1.搭建 MyBatis Generator 插件环境  a. 添加插件依赖 pom.xml        ...
    99+
    2024-04-02
  • MyBatis逆向工程生成dao层增删改查的操作
    目录MyBatis逆向工程生成dao层增删改查如下:Mybatis反向工程的使用首先,用eclipse 建一个maven 项目这时我们的项目结构是这个样子的现在要写一下代码了例如,我...
    99+
    2024-04-02
  • Mybatis Plus 逆向工程介绍
    目录一、创建数据库二、配置pom.xml 文件三、在项目同级目录建立mgb.xml四、在测试类中写入方法一、创建数据库 二、配置pom.xml 文件    <dependen...
    99+
    2024-04-02
  • MyBatis中的逆向工程是什么
    MyBatis中的逆向工程是一种自动化工具,用于根据数据库表结构生成对应的Java实体类、Mapper接口和Mapper.xml文件...
    99+
    2024-03-07
    MyBatis
  • MyBatis中逆向工程的示例分析
    这篇文章主要介绍了MyBatis中逆向工程的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。MyBatis的逆向工程一:什么是逆行工程...
    99+
    2024-04-02
  • Mybatis技术的使用一:逆向工程
    目录 一、什么是Mybatis的逆向工程: 1.正常流程: 2.加入Mybatis逆向工程后的流程: 二、优缺点: 三、使用步骤: 1、创建Maven工程,完善工程结构  2、引入pom依赖 3、创建逆向工程的配置文件 gener...
    99+
    2023-09-23
    数据库 database mysql maven java
  • Mybatis Plus逆向工程的示例分析
    这篇文章主要介绍Mybatis Plus逆向工程的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、创建数据库注:给tbl_emp的表创建外键数据库以及表的建立:二、配置pom.xml 文件&nbs...
    99+
    2023-06-26
  • mybatis根据表逆向自动化生成代码的实现
    目录1.引入插件2.修改generatorConfig.xml文件3.运行插件,生成代码.personSunflowerP { background: rgba(51, 153, 0...
    99+
    2024-04-02
  • 基于mybatis逆向工程的示例分析
    这篇文章给大家分享的是有关基于mybatis逆向工程的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。使用mybatis生成逆向工程的详细步骤,我个人感觉这个是最简单的一个了,虽然网上有很多种的方法来生成逆...
    99+
    2023-05-30
    mybatis
  • MyBatis逆向工程指的是什么意思
    MyBatis逆向工程指的是根据数据库表结构自动生成对应的实体类、Mapper接口和XML映射文件的过程。通过逆向工程工具,开发人员...
    99+
    2024-03-07
    MyBatis
  • 基于Spring整合mybatis的mapper生成过程
    目录Spring整合mybatis的mapper生成过程ClassPathMapperScanner#doScanClassPathBeanDefinitionScanner#doS...
    99+
    2023-03-07
    Spring整合mybatis mapper Spring mybatis mapper mybatis的mapper
  • mybatis-plus使用generator实现逆向工程
    目录1.背景2.引入jar包3.自动生成代码4.修改*Mapper.xml文件的生成位置4.1 默认*Mapper.xml文件生成位置4.2 修改*Mapper.xml文件生成位置1...
    99+
    2024-04-02
  • MyBatis命令行实现逆向工程的示例
    这篇文章主要介绍了MyBatis命令行实现逆向工程的示例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1.MyBatis逆向工程介绍在开发过...
    99+
    2024-04-02
  • mybatis如何根据表逆向自动化生成代码实例
    前言  自动生成代码总结(1)——(mybatis自动生成实体类、mapper文件、mapper.xml文件) 若采用mybatis框架,数据库新建表,手动编写的话,需要编...
    99+
    2024-04-02
  • SpringBoot项目使用mybatis-plus逆向自动生成全套代码
    目录1.在你的SpringBoot项目下新建子模块项目2.在此模块下新建一个包与一个java类 类名: CodeGenerator3.在 resources 下新建 文件夹,用来存放...
    99+
    2024-04-02
  • JavaScript逆向分析instagram登入过程
    目录一、流程分析二、逆向分析三、模拟请求一、流程分析 分析发现密码加密,且发送POST请求时header必须携带x-csrftoken,否则是报403。 而x-csrftoken是...
    99+
    2024-04-02
  • 基于mybatis的java代码生成存储过程
     问题:   项目中目前使用mybatis操作数据库,使用插件(mybatis-generator)自动生成代码,对于增改查,使用存储过程实现了一版本,方便使用。         insert代码生成器用法: insert_code_g...
    99+
    2020-03-08
    基于mybatis的java代码生成存储过程
  • idea怎么使用Mybatis逆向工程插件
    idea怎么使用Mybatis逆向工程插件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、使用mybatis连接数据库添加连接的mysql的信息,测试链接成功...
    99+
    2023-06-22
  • idea使用Mybatis逆向工程插件详情
    目录一、使用mybatis连接数据库二、安装Better-Mybatis-Generator插件三、关于example类详解1、example成员变量2、example使用一、使用m...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作