Mybatis学习(8)逆向工程
什么是逆向工程:
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)
1)下载逆向工程:
mybatis-generator-core-1.3.2-bundle.zip
2)使用java程序和xml配置生成代码:
其中GeneratorSqlmap.java:
import java.io.File;
import java.util.ArrayList;
import java.util.List; import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback; public class GeneratorSqlmap { public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
} public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成代码配置文件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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="mysql">
</jdbcConnection>
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> --> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.ssm.po"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
<!-- <table schema="" tableName="sys_user"></table>
<table schema="" tableName="sys_role"></table>
<table schema="" tableName="sys_permission"></table>
<table schema="" tableName="sys_user_role"></table>
<table schema="" tableName="sys_role_permission"></table> --> <!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
执行GeneratorSqlmap.java生成代码:
生成后代码:
3)使用生成的代码:
将生成工程中所生成的代码拷贝到自己的工程中。
举以下的例子:
将ItemsMapper.java/ItemsMapper.xml Items.java/ItemsExample.java拷贝进项目中;使用这些mapper来做试验:
ItemsMapper.java接口:和ItemsMapper.xml:
package com.ssm.mapper; import com.ssm.po.Items;
import com.ssm.po.ItemsExample;
import java.util.List;
import org.apache.ibatis.annotations.Param; public interface ItemsMapper {
int countByExample(ItemsExample example); int deleteByExample(ItemsExample example); int deleteByPrimaryKey(Integer id); int insert(Items record); int insertSelective(Items record); List<Items> selectByExampleWithBLOBs(ItemsExample example); List<Items> selectByExample(ItemsExample example); Items selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example); int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example); int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example); int updateByPrimaryKeySelective(Items record); int updateByPrimaryKeyWithBLOBs(Items record); int updateByPrimaryKey(Items record);
}
<?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.ssm.mapper.ItemsMapper" >
<resultMap id="BaseResultMap" type="com.ssm.po.Items" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="REAL" />
<result column="pic" property="pic" jdbcType="VARCHAR" />
<result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
</resultMap>
<resultMap id="ResultMapWithBLOBs" type="com.ssm.po.Items" extends="BaseResultMap" >
<result column="detail" property="detail" jdbcType="LONGVARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<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 collection="criterion.value" item="listItem" open="(" close=")" 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="(" suffix=")" prefixOverrides="and" >
<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 collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
id, name, price, pic, createtime
</sql>
<sql id="Blob_Column_List" >
detail
</sql>
<select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="com.ssm.po.ItemsExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from items
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.ssm.po.ItemsExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from items
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from items
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from items
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.ssm.po.ItemsExample" >
delete from items
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.ssm.po.Items" >
insert into items (id, name, price,
pic, createtime, detail
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL},
#{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.ssm.po.Items" >
insert into items
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="price != null" >
price,
</if>
<if test="pic != null" >
pic,
</if>
<if test="createtime != null" >
createtime,
</if>
<if test="detail != null" >
detail,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=REAL},
</if>
<if test="pic != null" >
#{pic,jdbcType=VARCHAR},
</if>
<if test="createtime != null" >
#{createtime,jdbcType=TIMESTAMP},
</if>
<if test="detail != null" >
#{detail,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.ssm.po.ItemsExample" resultType="java.lang.Integer" >
select count(*) from items
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update items
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null" >
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.price != null" >
price = #{record.price,jdbcType=REAL},
</if>
<if test="record.pic != null" >
pic = #{record.pic,jdbcType=VARCHAR},
</if>
<if test="record.createtime != null" >
createtime = #{record.createtime,jdbcType=TIMESTAMP},
</if>
<if test="record.detail != null" >
detail = #{record.detail,jdbcType=LONGVARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExampleWithBLOBs" parameterType="map" >
update items
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=REAL},
pic = #{record.pic,jdbcType=VARCHAR},
createtime = #{record.createtime,jdbcType=TIMESTAMP},
detail = #{record.detail,jdbcType=LONGVARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update items
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=REAL},
pic = #{record.pic,jdbcType=VARCHAR},
createtime = #{record.createtime,jdbcType=TIMESTAMP}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.ssm.po.Items" >
update items
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=REAL},
</if>
<if test="pic != null" >
pic = #{pic,jdbcType=VARCHAR},
</if>
<if test="createtime != null" >
createtime = #{createtime,jdbcType=TIMESTAMP},
</if>
<if test="detail != null" >
detail = #{detail,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.ssm.po.Items" >
update items
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
pic = #{pic,jdbcType=VARCHAR},
createtime = #{createtime,jdbcType=TIMESTAMP},
detail = #{detail,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ssm.po.Items" >
update items
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
pic = #{pic,jdbcType=VARCHAR},
createtime = #{createtime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Items.java 和 ItemsExample.java:
package com.ssm.po; import java.util.Date; public class Items {
private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name == null ? null : name.trim();
} public Float getPrice() {
return price;
} public void setPrice(Float price) {
this.price = price;
} public String getPic() {
return pic;
} public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
} public Date getCreatetime() {
return createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} public String getDetail() {
return detail;
} public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}
package com.ssm.po; import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class ItemsExample {
protected String orderByClause; protected boolean distinct; protected List<Criteria> oredCriteria; public ItemsExample() {
oredCriteria = new ArrayList<Criteria>();
} 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<Criterion>();
} 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(Integer value) {
addCriterion("id =", value, "id");
return (Criteria) this;
} public Criteria andIdNotEqualTo(Integer value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
} public Criteria andIdGreaterThan(Integer value) {
addCriterion("id >", value, "id");
return (Criteria) this;
} public Criteria andIdGreaterThanOrEqualTo(Integer value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
} public Criteria andIdLessThan(Integer value) {
addCriterion("id <", value, "id");
return (Criteria) this;
} public Criteria andIdLessThanOrEqualTo(Integer value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
} public Criteria andIdIn(List<Integer> values) {
addCriterion("id in", values, "id");
return (Criteria) this;
} public Criteria andIdNotIn(List<Integer> values) {
addCriterion("id not in", values, "id");
return (Criteria) this;
} public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
} public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
} public Criteria andNameIsNull() {
addCriterion("name is null");
return (Criteria) this;
} public Criteria andNameIsNotNull() {
addCriterion("name is not null");
return (Criteria) this;
} public Criteria andNameEqualTo(String value) {
addCriterion("name =", value, "name");
return (Criteria) this;
} public Criteria andNameNotEqualTo(String value) {
addCriterion("name <>", value, "name");
return (Criteria) this;
} public Criteria andNameGreaterThan(String value) {
addCriterion("name >", value, "name");
return (Criteria) this;
} public Criteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("name >=", value, "name");
return (Criteria) this;
} public Criteria andNameLessThan(String value) {
addCriterion("name <", value, "name");
return (Criteria) this;
} public Criteria andNameLessThanOrEqualTo(String value) {
addCriterion("name <=", value, "name");
return (Criteria) this;
} public Criteria andNameLike(String value) {
addCriterion("name like", value, "name");
return (Criteria) this;
} public Criteria andNameNotLike(String value) {
addCriterion("name not like", value, "name");
return (Criteria) this;
} public Criteria andNameIn(List<String> values) {
addCriterion("name in", values, "name");
return (Criteria) this;
} public Criteria andNameNotIn(List<String> values) {
addCriterion("name not in", values, "name");
return (Criteria) this;
} public Criteria andNameBetween(String value1, String value2) {
addCriterion("name between", value1, value2, "name");
return (Criteria) this;
} public Criteria andNameNotBetween(String value1, String value2) {
addCriterion("name not between", value1, value2, "name");
return (Criteria) this;
} public Criteria andPriceIsNull() {
addCriterion("price is null");
return (Criteria) this;
} public Criteria andPriceIsNotNull() {
addCriterion("price is not null");
return (Criteria) this;
} public Criteria andPriceEqualTo(Float value) {
addCriterion("price =", value, "price");
return (Criteria) this;
} public Criteria andPriceNotEqualTo(Float value) {
addCriterion("price <>", value, "price");
return (Criteria) this;
} public Criteria andPriceGreaterThan(Float value) {
addCriterion("price >", value, "price");
return (Criteria) this;
} public Criteria andPriceGreaterThanOrEqualTo(Float value) {
addCriterion("price >=", value, "price");
return (Criteria) this;
} public Criteria andPriceLessThan(Float value) {
addCriterion("price <", value, "price");
return (Criteria) this;
} public Criteria andPriceLessThanOrEqualTo(Float value) {
addCriterion("price <=", value, "price");
return (Criteria) this;
} public Criteria andPriceIn(List<Float> values) {
addCriterion("price in", values, "price");
return (Criteria) this;
} public Criteria andPriceNotIn(List<Float> values) {
addCriterion("price not in", values, "price");
return (Criteria) this;
} public Criteria andPriceBetween(Float value1, Float value2) {
addCriterion("price between", value1, value2, "price");
return (Criteria) this;
} public Criteria andPriceNotBetween(Float value1, Float value2) {
addCriterion("price not between", value1, value2, "price");
return (Criteria) this;
} public Criteria andPicIsNull() {
addCriterion("pic is null");
return (Criteria) this;
} public Criteria andPicIsNotNull() {
addCriterion("pic is not null");
return (Criteria) this;
} public Criteria andPicEqualTo(String value) {
addCriterion("pic =", value, "pic");
return (Criteria) this;
} public Criteria andPicNotEqualTo(String value) {
addCriterion("pic <>", value, "pic");
return (Criteria) this;
} public Criteria andPicGreaterThan(String value) {
addCriterion("pic >", value, "pic");
return (Criteria) this;
} public Criteria andPicGreaterThanOrEqualTo(String value) {
addCriterion("pic >=", value, "pic");
return (Criteria) this;
} public Criteria andPicLessThan(String value) {
addCriterion("pic <", value, "pic");
return (Criteria) this;
} public Criteria andPicLessThanOrEqualTo(String value) {
addCriterion("pic <=", value, "pic");
return (Criteria) this;
} public Criteria andPicLike(String value) {
addCriterion("pic like", value, "pic");
return (Criteria) this;
} public Criteria andPicNotLike(String value) {
addCriterion("pic not like", value, "pic");
return (Criteria) this;
} public Criteria andPicIn(List<String> values) {
addCriterion("pic in", values, "pic");
return (Criteria) this;
} public Criteria andPicNotIn(List<String> values) {
addCriterion("pic not in", values, "pic");
return (Criteria) this;
} public Criteria andPicBetween(String value1, String value2) {
addCriterion("pic between", value1, value2, "pic");
return (Criteria) this;
} public Criteria andPicNotBetween(String value1, String value2) {
addCriterion("pic not between", value1, value2, "pic");
return (Criteria) this;
} public Criteria andCreatetimeIsNull() {
addCriterion("createtime is null");
return (Criteria) this;
} public Criteria andCreatetimeIsNotNull() {
addCriterion("createtime is not null");
return (Criteria) this;
} public Criteria andCreatetimeEqualTo(Date value) {
addCriterion("createtime =", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeNotEqualTo(Date value) {
addCriterion("createtime <>", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeGreaterThan(Date value) {
addCriterion("createtime >", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeGreaterThanOrEqualTo(Date value) {
addCriterion("createtime >=", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeLessThan(Date value) {
addCriterion("createtime <", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeLessThanOrEqualTo(Date value) {
addCriterion("createtime <=", value, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeIn(List<Date> values) {
addCriterion("createtime in", values, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeNotIn(List<Date> values) {
addCriterion("createtime not in", values, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeBetween(Date value1, Date value2) {
addCriterion("createtime between", value1, value2, "createtime");
return (Criteria) this;
} public Criteria andCreatetimeNotBetween(Date value1, Date value2) {
addCriterion("createtime not between", value1, value2, "createtime");
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);
}
}
}
4)测试ItemsMapper中的方法:
package com.ssm.mapper; import static org.junit.Assert.*;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssm.po.Items;
import com.ssm.po.ItemsExample; public class ItemsMapperTest { private ApplicationContext applicationContext;
private ItemsMapper itemsMapper; @Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
} //根据主键删除
@Test
public void testDeleteByPrimaryKey() {
fail("Not yet implemented");
} //插入
@Test
public void testInsert() {
//构造 items对象
Items items = new Items();
items.setName("手机");
items.setPrice(999f);
items.setCreatetime(new Date());
itemsMapper.insert(items);
} //自定义条件查询
@Test
public void testSelectByExample() {
ItemsExample itemsExample = new ItemsExample();
//通过criteria构造查询条件
ItemsExample.Criteria criteria = itemsExample.createCriteria();
criteria.andNameEqualTo("手机");
//可能返回多条记录
List<Items> list = itemsMapper.selectByExample(itemsExample);
System.out.println(list);
} //根据主键查询
@Test
public void testSelectByPrimaryKey() {
Items items = itemsMapper.selectByPrimaryKey(1);
System.out.println(items);
} //更新数据
@Test
public void testUpdateByPrimaryKey() {
//对所有字段进行更新,需要先查询出来再更新
Items items = itemsMapper.selectByPrimaryKey(4);
items.setName("水杯");
itemsMapper.updateByPrimaryKey(items); //如果传入字段不空为才更新,在批量更新中使用此方法,不需要先查询再更新
//itemsMapper.updateByPrimaryKeySelective(record);
}
}
主要是看下逆向工程生成的mapper接口、mapper.xml配置文件;
Mybatis学习(8)逆向工程的更多相关文章
- mybatis学习系列--逆向工程简单使用及mybatis原理
2逆向工程简单测试(68-70) SqlSessionFactory sqlSessionFactory=getSqlSessionFactory(); SqlSession session = sq ...
- mybatis学习笔记(五):mybatis 逆向工程
mybatis学习笔记(五):mybatis 逆向工程 在日常开发中,如果数据库中存在多张表,自己手动创建 多个pojo 类和编写 SQL 语法配置文件,未免太过繁琐,mybatis 也提供了一键式生 ...
- MyBatis学习--逆向工程
简介 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po..).企业实际 ...
- Mybatis学习笔记二
本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...
- SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查
SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
- Mybatis 学习总结
1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 public static void main(String[] args) { Connection conn ...
- Mybatis学习笔记汇总(包括源码和jar包)
博客整理 Mybatis学习笔记(一)--对原生jdbc中问题的总结 Mybatis学习笔记(二)--Mybatis框架 Mybatis学习笔记(三)--入门程序 MyBatis学习笔记(四)--入门 ...
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...
随机推荐
- wince天气代码
#ifndef COMMON_H #define COMMON_H #include <Windows.h> typedef struct _operateInfo { HANDLE hS ...
- redmine配置邮件
作为一个项目管理平台,必须能够通知项目成员有关项目和任务的各种状态变化.这也是一种典型的观察者模式.开发人员可以跟踪某个任务,当任务状态.备注.文档.优先级等变化时开发人员就可以得到通知. 对于新配置 ...
- C#入门经典第四章-流程控制-1
布尔类型:
- JAVA和C++区别
1.指针 JAVA语言让编程者无法找到指针来直接访问内存无指针,并且增添了自动的内存管理功能,从而有效地防止了c/c++语言中指针操作失误,如野指针所造成的系统崩溃.但也不是说JAVA没有指针,虚拟机 ...
- ARM汇编指令的一些总结-转
ARM汇编指令的一些总结ARM汇编指令很多,但是真正常用的不是很多,而且需要认真琢磨的又更少了.比较有用的是MOV B BL LDR STR还是通过具体汇编代码来学习吧.@ disable watch ...
- 原创:运行loadtest时报错the load test results repository was created with a previous version and is not compatible
如果run setting中的Storage Type设置为DataBase,则需要设置数据库来保存loadtest的运行结果,如下图所示 图:Storage Type设置为DataBase 图:在M ...
- Combox下拉绑定DataGridView
这个Combox下拉很多人都在用 但其中绘制的语句如下: #endregion #region 方法 #region 绘制DataGridView以及下拉DataGridView private v ...
- JS监听input框的回车事件、属性值改变事件
一.介绍 在程序猿门的工作中,经常会遇见一些表单数据的提交,对于有表单的东西而言,input输入框是非常常见的,所以对于一些input的基本事件,我们需要去掌握 二.input的监听enter事件 比 ...
- JS——基础知识
1.alert语句 弹出一个对话框. 2.console 这个是在浏览器的控制台打印输出,也有一些公司将招聘信息打印到浏览器的console窗口,比如百度首页. 3.typeof 用这个关键字可以测试 ...
- unity3d之在屏幕上画线
如何在屏幕上画线,简单的代码如下: using UnityEngine; public class Test : MonoBehaviour { void OnGUI() { GL.LoadOrtho ...