当前位置: 首页 > news >正文

【MyBatis】| MyBatis的逆向⼯程

目录

一:MyBatis的逆向⼯程

1. 逆向⼯程配置与⽣成

2. 测试生成的逆向⼯程


一: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 generatorConfigurationPUBLIC "-//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接口的包名和位置 --><javaClientGeneratortype="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;@Overridepublic 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_carwhere 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_carset 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_typefrom t_carwhere id = #{id,jdbcType=BIGINT}</select><select id="selectAll" resultMap="BaseResultMap">select id, car_num, brand, guide_price, produce_time, car_typefrom 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 {@Testpublic 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;@Overridepublic 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_carwhere id = #{id,jdbcType=BIGINT}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Long">delete from t_carwhere 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_carset 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_carset 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 {@Testpublic 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));// 添加orcarExample.or().andCarTypeEqualTo("燃油车");// 执行查询List<Car> cars1 = mapper.selectByExample(carExample);cars1.forEach(car1 -> System.out.println(car1));sqlSession.close();}}

以上的SQL语句就等价于:

相关文章:

【MyBatis】| MyBatis的逆向⼯程

目录 一&#xff1a;MyBatis的逆向⼯程 1. 逆向⼯程配置与⽣成 2. 测试生成的逆向⼯程 一&#xff1a;MyBatis的逆向⼯程 &#xff08;1&#xff09;所谓的逆向⼯程是&#xff1a;根据数据库表逆向⽣成Java的pojo类&#xff0c;SqlMapper.xml⽂件&#xff0c;以及Mapper接⼝…...

Python|每日一练|哈希表|罗马数字|图算法|圆周率|单选记录:给定数列和|罗马数字转整数|计算圆周率

1、要求编写函数fn(a,n) 求aaaaaa⋯aa⋯aa(n个a&#xff09;之和&#xff0c;fn须返回的是数列和&#xff08;算法初阶&#xff09; 要求编写函数fn(a,n) 求aaaaaa⋯aa⋯aa(n个a&#xff09;之和&#xff0c;fn须返回的是数列和。 从控制台输入正整数a和n的值&#xff08;两…...

分布式之分布式事务V2

写在前面 本文一起来看下分布式环境下的事务问题&#xff0c;即我们经常听到的分布式事务问题。想要解决分布式事务问题&#xff0c;需要使用到分布式事务相关的协议&#xff0c;主要有2PC即两阶段提交协议&#xff0c;TCC&#xff08;try-confirm-cancel&#xff09;&#xf…...

算法笔记(二)—— 认识N(logN)的排序算法

递归行为的时间复杂度估算 整个递归过程是一棵多叉树&#xff0c;递归过程相当于利用栈做了一次后序遍历。 对于master公式&#xff0c;T(N)表明母问题的规模为N&#xff0c;T(N/b)表明每次子问题的规模&#xff0c;a为调用次数&#xff0c;加号后面表明&#xff0c;除去调用之…...

最长湍流子数组——滚动窗口,双指针,暴力求解

978. 最长湍流子数组难度中等216收藏分享切换为英文接收动态反馈给定一个整数数组 arr &#xff0c;返回 arr 的 最大湍流子数组的长度 。如果比较符号在子数组中的每个相邻元素对之间翻转&#xff0c;则该子数组是 湍流子数组 。更正式地来说&#xff0c;当 arr 的子数组 A[i]…...

45.在ROS中实现global planner(1)

前文move_base介绍&#xff08;4&#xff09;简单介绍move_base的全局路径规划配置&#xff0c;接下来我们自己实现一个全局的路径规划 1. move_base规划配置 ROS1的move_base可以配置选取不同的global planner和local planner&#xff0c; 默认move_base.cpp#L70中可以看到是…...

Java中导入、导出Excel——HSSFWorkbook 使用

一、介绍 当前B/S模式已成为应用开发的主流&#xff0c;而在企业办公系统中&#xff0c;常常有客户这样子要求&#xff1a;你要把我们的报表直接用Excel打开(电信系统、银行系统)。或者是&#xff1a;我们已经习惯用Excel打印。这样在我们实际的开发中&#xff0c;很多时候需要…...

c#数据结构-列表

列表 数组可以管理大量数组&#xff0c;但缺点是无法更变容量。 创建小了不够用&#xff0c;创建大了浪费空间。 无法预测需要多少大小的时候&#xff0c;可能范围越大&#xff0c;就会浪费越多的空间。 所以&#xff0c;你可能会想要一种可以扩容的东西&#xff0c;代替数组…...

Sa-Token实现分布式登录鉴权(Redis集成 前后端分离)

文章目录1. Sa-Token 介绍2. 登录认证2.1 登录与注销2.2 会话查询2.3 Token 查询3. 权限认证3.1 获取当前账号权限码集合3.2 权限校验3.3 角色校验4. 前后台分离&#xff08;无Cookie模式&#xff09;5. Sa-Token 集成 Redis6. SpringBoot 集成 Sa-Token6.1 创建项目6.2 添加依…...

leaflet显示高程

很多地图软件都能随鼠标移动动态显示高程。这里介绍一种方法&#xff0c;我所得出的。1 下载高程数据一般有12.5m数据下载&#xff0c;可惜精度根本不够&#xff0c;比如mapbox的免费在线的&#xff0c;或者91卫图提供百度网盘打包下载的&#xff0c;没法用&#xff0c;差距太大…...

电子学会2022年12月青少年软件编程(图形化)等级考试试卷(三级)答案解析

目录 一、单选题(共25题&#xff0c;共50分) 二、判断题(共10题&#xff0c;共20分) 三、编程题(共3题&#xff0c;共30分) 青少年软件编程&#xff08;图形化&#xff09;等级考试试卷&#xff08;三级&#xff09; 一、单选题(共25题&#xff0c;共50分) 1. 默认小猫角色…...

ubuntu 驱动更新后导致无法进入界面

**问题描述&#xff1a; **安装新ubuntu系统后未禁止驱动更新导致无法进入登录界面。 解决办法&#xff1a; 首先在进入BIOS中&#xff0c;修改设置以进行命令行操作&#xff0c;然后卸载已有的系统驱动&#xff0c;最后安装新的驱动即可。 开机按F11进入启动菜单栏&#xf…...

解决访问GitHub时出现的“您的连接不是私密连接”的问题!

Content问题描述解决办法问题描述 访问github出现您的连接不是私密连接问题&#xff0c;无法正常访问&#xff0c;如下图所示&#xff1a; 解决办法 修改hosts文件。hosts文件位于&#xff1a;C:\Windows\System32\drivers\etc\hosts 首先在https://www.ipaddress.com/查找两…...

初识数据仓库

一、什么是数据仓库数据库 --> OLTP&#xff1a;&#xff08;on-line transaction processing&#xff09;翻译为联机事务处理记录某类业务事件的发生&#xff0c;如购买行为&#xff0c;银行交易行为&#xff0c;当行为产生后&#xff0c;系统会记录是谁在何时何地做了何事…...

FilenameUtils工具类部分源码自研

FilenameUtils工具类部分源码自研getExtension(orgFileName)源码如下逐行分析getExtension(orgFileName)源码如下 public class FilenameUtils {public static int indexOfExtension(String fileName) throws IllegalArgumentException {if (fileName null) {return -1;} els…...

【前端领域】3D旋转超美相册(HTML+CSS)

世界上总有一半人不理解另一半人的快乐。 ——《爱玛》 目录 一、前言 二、本期作品介绍 3D旋转相册 三、效果展示 四、详细介绍 五、编码实现 index.html style.css img 六、获取源码 公众号获取源码 获取源码&#xff1f;私信&#xff1f;关注&#xff1f;点赞&…...

Java——聊聊JUC中的原子变量类

文章目录&#xff1a; 1.什么是原子变量类&#xff1f; 2.AtomicInteger&#xff08;基本类型原子变量类&#xff09; 3.AtomicIntegerArray&#xff08;数组类型原子变量类&#xff09; 4.AtomicMarkableReference&#xff08;引用类型原子变量类&#xff09; 5.AtomicInteger…...

elasticsearch索引与搜索初步

ES支持cURL交互&#xff0c;使用http请求完成索引和搜索操作&#xff0c;最基本的格式如下&#xff1a;创建索引我们可以使用PUT方法创建索引&#xff0c;通过指定“索引”、“类型”、“文档ID”锁定文档&#xff0c;通过参数指定文档的数据。红色部分的路由分别指定了“索引”…...

【Python】多线程与多进程学习笔记

本文是一篇学习笔记&#xff0c;学习内容主要来源于莫凡python的文档&#xff1a;https://mofanpy.com/tutorials/python-basic/threading/thread 多线程 线程基本结构 开启子线程的简单方式如下&#xff1a; import threadingdef thread_job():print(This is a thread of %…...

MySQL基础知识点

1.在Linux上安装好MySQL8.0之后&#xff0c;默认数据目录的具体位置是什么&#xff1f;该目录下都保存哪些数据库组件&#xff1f;在目录/usr/sbin、/usr/bin、/etc、/var/log 分别保存哪些组件&#xff1f; 答&#xff1a;默认数据目录&#xff1a;/var/lib/mysql。保存有mysq…...

代码随想录算法训练营第五十九天| 583. 两个字符串的删除操作、72. 编辑距离

Leetcode - 583dp[i][j]代表以i-1结尾的words1的子串 要变成以j-1结尾的words2的子串所需要的次数。初始化&#xff1a; "" 变成"" 所需0次 dp[0][0] 0, ""变成words2的子串 需要子串的长度的次数,所以dp[0][j] j, 同理&#xff0c;dp[i][0] …...

指针引用字符串问题(详解)

通过指针引用字符串可以更加方便灵活的使用字符串。 字符串的引用方式有两种&#xff0c;下面简单介绍一下这两种方法。 1.用字符数组来存放一个字符串。 1.1 可以通过数组名和下标来引用字符串中的一个字符。 1.2 还可以通过数组名和格式声明符%s输出整个字符串。 具体实…...

数据结构——哈夫曼树编程,输入权值实现流程图代码

一、须知 本代码是在数据结构——哈夫曼树编程上建立的&#xff0c;使用时需将代码剪切到C等软件中。需要输入权值方可实现流程图&#xff0c;但是还需要按照编程换算出的结果自己用笔画出流程图。 下面将代码粘贴到文章中&#xff0c;同时举一个例子&#xff1a;二、代…...

【MySQL】 事务

&#x1f60a;&#x1f60a;作者简介&#x1f60a;&#x1f60a; &#xff1a; 大家好&#xff0c;我是南瓜籽&#xff0c;一个在校大二学生&#xff0c;我将会持续分享Java相关知识。 &#x1f389;&#x1f389;个人主页&#x1f389;&#x1f389; &#xff1a; 南瓜籽的主页…...

Java测试——selenium常见操作(2)

这篇博客继续讲解一些selenium的常见操作 selenium的下载与准备工作请看之前的博客&#xff1a;Java测试——selenium的安装与使用教程 先创建驱动 ChromeDriver driver new ChromeDriver();等待操作 我们上一篇博客讲到&#xff0c;有些时候代码执行过快&#xff0c;页面…...

【三维点云】01-激光雷达原理与应用

文章目录内容概要1 激光雷达原理1.1 什么是激光雷达&#xff1f;1.2 激光雷达原理1.3 激光雷达分类三角法TOF法脉冲间隔测量法幅度调制的相位测量法相干法激光雷达用途2 激光雷达安装、标定与同步2.1 激光雷达安装方式考虑因素2.2 激光雷达点云用途2.3 数据融合多激光雷达数据融…...

自动驾驶感知——物体检测与跟踪算法|4D毫米波雷达

文章目录1. 物体检测与跟踪算法1.1 DBSCAN1.2 卡尔曼滤波2. 毫米波雷达公开数据库的未来发展方向3. 4D毫米波雷达特点及发展趋势3.1 4D毫米波雷达特点3.1.1 FMCW雷达角度分辨率3.1.2 MIMO ( Multiple Input Multiple Output)技术3.2 4D毫米波雷达发展趋势3.2.1 芯片级联3.2.2 专…...

C语言(内联函数(C99)和_Noreturn)

1.内联函数 通常&#xff0c;函数调用都有一定的开销&#xff0c;因为函数的调用过程包含建立调用&#xff0c;传递参数&#xff0c;跳转到函数代码并返回。而使用宏是代码内联&#xff0c;可以避开这样的开销。 内联函数&#xff1a;使用内联diamagnetic代替函数调用。把函数…...

图卷积神经网络(GCN)理解与tensorflow2.0 代码实现 附完整代码

图(Graph),一般用 $G=(V,E)$ 表示,这里的$V$是图中节点的集合,$E$ 为边的集合,节点的个数用$N$表示。在一个图中,有三个比较重要的矩阵: 特征矩阵$X$:维度为 $N\times D$ ,表示图中有 N 个节点,每个节点的特征个数是 D。邻居矩阵$A$:维度为 $N\times N$ ,表示图中 N…...

模电学习6. 常用的三极管放大电路

模电学习6. 常用的三极管放大电路一、判断三极管的工作状态1. 正偏与反偏的概念2. 工作状态的简单判断二、三种重要的放大电路1. 共射电路2. 共集电极放大电路3. 共基极放大电路一、判断三极管的工作状态 1. 正偏与反偏的概念 晶体管分P区和N区&#xff0c; 当P区电压大于N区…...

网络游戏网站网址大全/seo网站优化知识

缓存雪崩&#xff0c;缓存穿透&#xff0c;缓存预热&#xff0c;缓存热备是在做缓存设计或者缓存应用时经常遇到的概念&#xff0c;也是缓存应用过程中必须熟知及知道 的东西。 缓存雪崩 当缓存处于单点情况下&#xff0c;一旦缓存服务器崩溃&#xff0c;所有的请求就会落到数…...

wordpress高级版破解版/域名查询官网

你好,欢迎来到第 24 课时,本课时主要讲解 Flink 消费 Kafka 数据开发。 在上一课时中我们提过在实时计算的场景下,绝大多数的数据源都是消息系统,而 Kafka 从众多的消息中间件中脱颖而出,主要是因为高吞吐、低延迟的特点;同时也讲了 Flink 作为生产者像 Kafka 写入数据的…...

做网站好还是做程序员好/网络营销主要内容

2019独角兽企业重金招聘Python工程师标准>>> #在Window2008上搭建 Apache FTPServer windows2003马上要停止维护了&#xff0c;时间好像是在2015年5月7日。阿里云希望用户能够升级服务器&#xff0c;刚好公司的新项目马上要上线了&#xff0c;所以就尝试了一下将云服…...

做公司网站需要几个域名/百度访问量统计

目录第一章 AngularJS简介1.1、AngularJS简介1.2、AngularJS版本介绍1.3、AngularJS官方地址1.4、AngularJS代码体验第二章 AngularJS方法2.1、字符串大小写转换2.2、对象/数组深度拷贝2.3、对象/数组迭代函数2.4、常见数据类型判断2.5、序列化与反序列化第三章 AngularJS指令3…...

建设好学校网站/网络推广怎么推广

为了完成推送功能&#xff0c;在了解了业务&#xff0c;研究了需求之后&#xff0c;开始在网上搜索。最值得参考的是 文章&#xff1a;http://blog.csdn.net/zhuqilin0/article/details/6527113 源代码是&#xff1a;https://github.com/Redth/APNS-Sharp 只要将源代码修改少许…...

漯河网站优化/成功营销案例分享

一、分析什么是顺序表&#xff1f;顺序表是指用一组地址连续的存储单元依次存储各个元素&#xff0c;使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表。一个标准的顺序表需要实现以下基本操作&#xff1a;1、初始化顺序表2、销毁顺序表3、清空顺序表4、检测…...