MyBatis逆向?程的生成過程
一、MyBatis的逆向?程
(1)所謂的逆向?程是:根據(jù)數(shù)據(jù)庫表逆向?成Java的pojo類,SqlMapper.xml?件,以及Mapper接?類等,這真是一個(gè)很強(qiáng)大的功能。 要完成這個(gè)?作,需要借助別?寫好的逆向?程插件。
(2)思考:使?這個(gè)插件的話,需要給這個(gè)插件配置哪些信息?
①pojo類名、包名以及?成位置。
②SqlMapper.xml?件名以及?成位置。
③Mapper接?名以及?成位置。
④連接數(shù)據(jù)庫的信息。
⑤指定哪些表參與逆向?程。
......
1.逆向?程配置與?成
(1)基礎(chǔ)環(huán)境準(zhǔn)備
新建一個(gè)普通的Maven模塊:mybatis-012-generator
打包?式:jar
(2)在pom.xml中添加逆向?程插件
①先引入mybatis逆向?程的插件,引入倉庫中對應(yīng)的插件坐標(biāo)。
②允許覆蓋:表示原來這些文件存在,true就以覆蓋的方式生成文件,false就以追加的方式生成文件。
③引入插件的依賴:我們需要根據(jù)數(shù)據(jù)庫表逆向生成pojo類、SqlMapper接口和里面的方法、SqlMapper.xml配置文件等,所以肯定需要mysql的驅(qū)動(dòng)。
<?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> <!--定制構(gòu)建過程--> <build> <!--可配置多個(gè)插件--> <plugins> <!--其中的?個(gè)插件:mybatis逆向?程插件--> <plugin> <!--插件的GAV坐標(biāo)--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.1</version> <!--允許覆蓋--> <configuration> <overwrite>true</overwrite> </configuration> <!--插件的依賴--> <dependencies> <!--mysql驅(qū)動(dòng)依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.23</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
(3)配置generatorConfig.xml(基礎(chǔ)版)
該?件名必須叫做: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有兩個(gè)值: MyBatis3Simple:生成的是基礎(chǔ)版,只有基本的增刪改查。 MyBatis3:生成的是增強(qiáng)版,除了基本的增刪改查之外還有復(fù)雜的增刪改查。 --> <context id="DB2Tables" targetRuntime="MyBatis3Simple"> <!--防止生成重復(fù)代碼--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/> <commentGenerator> <!--是否去掉生成日期--> <property name="suppressDate" value="true"/> <!--是否去除注釋--> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--連接數(shù)據(jù)庫信息--> <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> <!-- 表名和對應(yīng)的實(shí)體類名--> <table tableName="t_car" domainObjectName="Car"/> </context> </generatorConfiguration>
(4)雙擊運(yùn)行插件,就可以生成基礎(chǔ)版的配置信息
自動(dòng)生成的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)環(huán)境準(zhǔn)備
①依賴:mybatis依賴、mysql驅(qū)動(dòng)依賴、junit依賴、logback依賴
②jdbc.properties、mybatis-config.xml、logback.xml
③拷貝工具類:SqlSessionUtil
(2)編寫測試程序(基礎(chǔ)版)
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(); } }
執(zhí)行結(jié)果:
(3)配置generatorConfig.xml(增強(qiáng)版)
生成了兩個(gè)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)編寫測試程序(增強(qiáng)版)
(1)增強(qiáng)版的查詢方式就比較特殊,特別是根據(jù)條件查詢,是QBC 風(fēng)格:Query By Criteria 一種查詢方式,比較面向?qū)ο?,看不到sql語句!
(2)條件查詢步驟:
①先封裝條件,通過CarExample對象來封裝查詢條件
②調(diào)用carExample.createCriteria()方法來創(chuàng)建查詢條件,后面通過" 點(diǎn). "的方式跟上方法
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); // 執(zhí)行查詢 // 1. 查詢一個(gè) Car car = mapper.selectByPrimaryKey(34L); System.out.println(car); // 2. 查詢所有(selectByExample,根據(jù)條件查詢,如果條件是null表示沒有條件) List<Car> cars = mapper.selectByExample(null); cars.forEach(car1 -> System.out.println(car1)); System.out.println("=================="); // 3.按照條件進(jìn)行查詢 // QBC 風(fēng)格:Query By Criteria 一種查詢方式,比較面向?qū)ο?,看不到sql語句。 // 3.1 封裝條件,通過CarExample對象來封裝查詢條件 CarExample carExample = new CarExample(); // 3.2調(diào)用carExample.createCriteria()方法來創(chuàng)建查詢條件 carExample.createCriteria().andBrandLike("帕薩特"). andGuidePriceGreaterThan(new BigDecimal(20.0)); // 添加or carExample.or().andCarTypeEqualTo("燃油車"); // 執(zhí)行查詢 List<Car> cars1 = mapper.selectByExample(carExample); cars1.forEach(car1 -> System.out.println(car1)); sqlSession.close(); } }
以上的SQL語句就等價(jià)于:
到此這篇關(guān)于MyBatis逆向?程的生成過程的文章就介紹到這了,更多相關(guān)MyBatis逆向?程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis詳解如何實(shí)現(xiàn)Dao層接口
MyBatis允許只聲明一個(gè)dao接口,而無需寫dao實(shí)現(xiàn)類的方式實(shí)現(xiàn)數(shù)據(jù)庫操作。前提是必須保證Mapper文件中的<mapper>標(biāo)簽的namespace屬性值必須要和dao接口的類路徑一致,MyBatis容器會自動(dòng)通過動(dòng)態(tài)代理生成接口的實(shí)現(xiàn)類2022-04-04在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作
這篇文章主要介紹了在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP)
這篇文章主要介紹了如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Java的Spring框架中DAO數(shù)據(jù)訪問對象的使用示例
這篇文章主要介紹了Java的Spring框架中DAO數(shù)據(jù)訪問對象的使用示例,分為在Spring中DOA與JDBC以及與Hibernate的配合使用兩種情況來進(jìn)行演示,需要的朋友可以參考下2016-03-03淺析java中 Spring MVC 攔截器作用及其實(shí)現(xiàn)
本篇文章主要介紹了java中SpringMVC 攔截器的使用及其實(shí)例,需要的朋友可以參考2017-04-04Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類示例詳解
這篇文章主要介紹了Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Mybatis實(shí)現(xiàn)查詢相冊數(shù)據(jù)列表流程講解
這篇文章主要介紹了Mybatis實(shí)現(xiàn)查詢相冊數(shù)據(jù)列表流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12