目录
一:MyBatis的逆向⼯程
1. 逆向⼯程配置与⽣成
2. 测试生成的逆向⼯程
(1)所谓的逆向⼯程是:根据数据库表逆向⽣成Java的pojo类,SqlMapper.xml⽂件,以及Mapper接⼝类等,这真是一个很强大的功能。 要完成这个⼯作,需要借助别⼈写好的逆向⼯程插件。
(2)思考:使⽤这个插件的话,需要给这个插件配置哪些信息?
①pojo类名、包名以及⽣成位置。
②SqlMapper.xml⽂件名以及⽣成位置。
③Mapper接⼝名以及⽣成位置。
④连接数据库的信息。
⑤指定哪些表参与逆向⼯程。
......
(1)基础环境准备
新建一个普通的Maven模块:mybatis-012-generator
打包⽅式:jar
(2)在pom.xml中添加逆向⼯程插件
①先引入mybatis逆向⼯程的插件,引入仓库中对应的插件坐标。
②允许覆盖:表示原来这些文件存在,true就以覆盖的方式生成文件,false就以追加的方式生成文件。
③引入插件的依赖:我们需要根据数据库表逆向生成pojo类、SqlMapper接口和里面的方法、SqlMapper.xml配置文件等,所以肯定需要mysql的驱动。
4.0.0
com.bjpowernode
mybatis-012-generator
1.0-SNAPSHOT
jar
org.mybatis.generator
mybatis-generator-maven-plugin
1.4.1
true
mysql
mysql-connector-java
5.1.23
(3)配置generatorConfig.xml(基础版)
该⽂件名必须叫做:generatorConfig.xml。
并且该⽂件必须放在类的根路径下。
(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 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配置文件
delete from t_car
where id = #{id,jdbcType=BIGINT}
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}
)
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}
(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 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 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 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 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 criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List getAllCriteria() {
return criteria;
}
public List 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 values) {
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List 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 values) {
addCriterion("car_num in", values, "carNum");
return (Criteria) this;
}
public Criteria andCarNumNotIn(List 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 values) {
addCriterion("brand in", values, "brand");
return (Criteria) this;
}
public Criteria andBrandNotIn(List 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 values) {
addCriterion("guide_price in", values, "guidePrice");
return (Criteria) this;
}
public Criteria andGuidePriceNotIn(List 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 values) {
addCriterion("produce_time in", values, "produceTime");
return (Criteria) this;
}
public Criteria andProduceTimeNotIn(List 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 values) {
addCriterion("car_type in", values, "carType");
return (Criteria) this;
}
public Criteria andCarTypeNotIn(List 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配置文件
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, car_num, brand, guide_price, produce_time, car_type
delete from t_car
where id = #{id,jdbcType=BIGINT}
delete from t_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 into t_car
id,
car_num,
brand,
guide_price,
produce_time,
car_type,
#{id,jdbcType=BIGINT},
#{carNum,jdbcType=VARCHAR},
#{brand,jdbcType=VARCHAR},
#{guidePrice,jdbcType=DECIMAL},
#{produceTime,jdbcType=CHAR},
#{carType,jdbcType=VARCHAR},
update t_car
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},
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}
update t_car
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 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}
(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 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 cars1 = mapper.selectByExample(carExample);
cars1.forEach(car1 -> System.out.println(car1));
sqlSession.close();
}
}
以上的SQL语句就等价于: