1.简单说明。
MyBatis 可以使用简单的xml或注解用于配置和原始映射,将接口和java的 POJO( Plain Old Java Objects,普通的Java 对象)映射成数据库中的记录。
每一个MyBatis项目中都以一个对象sqlsessionFactory为核心,它可以通过sqlSessionFactoryBuilder来获得,它产生了一个个的sqlSession。
2.关于项目中的使用做详尽说明。
什么是Mapper对象?根据Mybatis的官方手册,应用程序除了要初始并启动Mybatis之外,还需要定义一些接口,接口里定义访问数据库的方法,存放接口的包路径下需要放置同名的XML配置文件。每个mapper元素对应一个mapper配置文件。
3.Mybatis中参数传递的四种方式讲解
http://edu.51cto.com/index.php?do=lesson&id=69884
4.使用mybatis generator自动生成映射文件详解
http://www.cnblogs.com/mikelij/p/3841716.html 跟这篇一模一样。
5.项目实例分析
1<?xml version="1.0" encoding="UTF-8"?>2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">3<mappernamespace="org.iMybatis.abc.dao.UserDao">4<cachetype="PERPETUAL"eviction="LRU"flushInterval="60000"5size="512"readOnly="true"/>6<resultMapid="userResultMap"type="UserDto">7<idPRoperty="userid"column="userid"/>8<resultproperty="username"column="username"/>9<resultproperty="passWord"column="password"/>10</resultMap>11<sqlid="userColumns">userid,username,password</sql>12<selectid="queryUsers"parameterType="UserDto"resultType="UserDto"13useCache="false">14select<includerefid="userColumns"/>15from t_user t where t.username = #{username}16</select>17<insertid="insertUser"parameterType="UserDto"18useGeneratedKeys="true"keyProperty="userid">19insert into t_user (userid,username,password)20values (#{userid},#{username},#{password})21</insert>22<updateid="updateUser"parameterType="UserDto">23update t_user set24username= #{username},25password = #{password},26where userid = #{userid}27</update>28<deleteid="deleteUser"parameterType="UserDto">29delete from t_user where userid = #{userid}30</delete>31</mapper>
一段热乎乎的mapper的 XML 的配置文件
看上面代码,Mapper元素只有一个属性namespace,它有两个作用:一是用于区分不同的mapper(在不同的mapper文件里,子元素的id可以相同,mybatis通过namespace和子元素的id联合区分),二是与接口关联(应用程序通过接口访问mybatis时,mybatis通过接口的完整名称查找对应的mapper配置,因此namespace的命名务必小心一定要某接口同名)。此外,mapper配置文件还有几个顶级子元素(它们须按照顺序定义):cache -配置本定命名空间的缓存。resultMap –结果映射,用来描述如何从数据库结果集映射到你想要的对象。接下来详解一下resultMap。
<resultMapid="userResultMap"type="User"><idproperty=" userid "column="userid"/><resultproperty="username"column="username"/><resultproperty="password"column="password"/></resultMap>
resultMap 配置
resultMap提供了从数据库表列名到java对象属性的映射管理,示例只是提供了最简单的情况。在mapper配置文件中可以配置多个resultMap,不同的resultMap用id加以区分。type属性标记java类型(别名)。子元素中的property指带java中的属性,column指带数据库表的列名。
Example类,用于构造复杂的筛选条件,详细分析如下:
上面的sql语句,创建了表'order_detail',以及各个字段。
packageredcliff.cobara.entity;importjava.math.BigDecimal;publicclassCobaraOrderDetail {privateLong id;privateInteger cityId;privateInteger orderId;privateString name;privateByte type;privateBigDecimal cost;privateInteger shardx;privateInteger shardy;privateInteger shardz;publicLong getId() {returnid;
}publicvoidsetId(Long id) {this.id =id;
}publicInteger getCityId() {returncityId;
}publicvoidsetCityId(Integer cityId) {this.cityId =cityId;
}publicInteger getOrderId() {returnorderId;
}publicvoidsetOrderId(Integer orderId) {this.orderId =orderId;
}publicString getName() {returnname;
}publicvoidsetName(String name) {this.name = name ==null?null: name.trim();
}publicByte getType() {returntype;
}publicvoidsetType(Byte type) {this.type =type;
}publicBigDecimal getCost() {returncost;
}publicvoidsetCost(BigDecimal cost) {this.cost =cost;
}publicInteger getShardx() {returnshardx;
}publicvoidsetShardx(Integer shardx) {this.shardx =shardx;
}publicInteger getShardy() {returnshardy;
}publicvoidsetShardy(Integer shardy) {this.shardy =shardy;
}publicInteger getShardz() {returnshardz;
}publicvoidsetShardz(Integer shardz) {this.shardz =shardz;
}
}
redcliff.cobara.entity.CobaraOrderDetail.java
上面的entitiy包下的CobaraOrderDetail.java文件定义了实体类,是数据库的各个字段在Java web项目中一模一样的写照。当然了它是mybatis的generator生成器自动生成的。注意到,结构public class CobaraOrderDetail;里面的变量定义为private类型,方法定义为public类型。
packageredcliff.cobara.entity;importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;publicclassCobaraOrderDetailExample {protectedString orderByClause;protectedbooleandistinct;protectedList<Criteria>oredCriteria;publicCobaraOrderDetailExample() {
oredCriteria=newArrayList<Criteria>();
}publicvoidsetOrderByClause(String orderByClause) {this.orderByClause =orderByClause;
}publicString getOrderByClause() {returnorderByClause;
}publicvoidsetDistinct(booleandistinct) {this.distinct =distinct;
}publicbooleanisDistinct() {returndistinct;
}publicList<Criteria>getOredCriteria() {returnoredCriteria;
}publicvoidor(Criteria criteria) {
oredCriteria.add(criteria);
}publicCriteria or() {
Criteria criteria=createCriteriaInternal();
oredCriteria.add(criteria);returncriteria;
}publicCriteria createCriteria() {
Criteria criteria=createCriteriaInternal();if(oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}returncriteria;
}protectedCriteria createCriteriaInternal() {
Criteria criteria=newCriteria();returncriteria;
}publicvoidclear() {
oredCriteria.clear();
orderByClause=null;
distinct=false;
}protectedabstractstaticclassGeneratedCriteria {protectedList<Criterion>criteria;protectedGeneratedCriteria() {super();
criteria=newArrayList<Criterion>();
}publicbooleanisValid() {returncriteria.size() > 0;
}publicList<Criterion>getAllCriteria() {returncriteria;
}publicList<Criterion>getCriteria() {returncriteria;
}protectedvoidaddCriterion(String condition) {if(condition ==null) {thrownewRuntimeException("Value for condition cannot be null");
}
criteria.add(newCriterion(condition));
}protectedvoidaddCriterion(String condition, Object value, String property) {if(value ==null) {thrownewRuntimeException("Value for " + property + " cannot be null");
}
criteria.add(newCriterion(condition, value));
}protectedvoidaddCriterion(String condition, Object value1, Object value2, String property) {if(value1 ==null|| value2 ==null) {thrownewRuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(newCriterion(condition, value1, value2));
}publicCriteria andIdIsNull() {
addCriterion("id is null");return(Criteria)this;
}publicCriteria andIdIsNotNull() {
addCriterion("id is not null");return(Criteria)this;
}publicCriteria andIdEqualTo(Long value) {
addCriterion("id =", value, "id");return(Criteria)this;
}publicCriteria andIdNotEqualTo(Long value) {
addCriterion("id <>", value, "id");return(Criteria)this;
}publicCriteria andIdGreaterThan(Long value) {
addCriterion("id >", value, "id");return(Criteria)this;
}publicCriteria andIdGreaterThanOrEqualTo(Long value) {
addCriterion("id >=", value, "id");return(Criteria)this;
}publicCriteria andIdLessThan(Long value) {
addCriterion("id <", value, "id");return(Criteria)this;
}publicCriteria andIdLessThanOrEqualTo(Long value) {
addCriterion("id <=", value, "id");return(Criteria)this;
}publicCriteria andIdIn(List<Long>values) {
addCriterion("id in", values, "id");return(Criteria)this;
}publicCriteria andIdNotIn(List<Long>values) {
addCriterion("id not in", values, "id");return(Criteria)this;
}publicCriteria andIdBetween(Long value1, Long value2) {
addCriterion("id between", value1, value2, "id");return(Criteria)this;
}publicCriteria andIdNotBetween(Long value1, Long value2) {
addCriterion("id not between", value1, value2, "id");return(Criteria)this;
}publicCriteria andCityIdIsNull() {
addCriterion("city_id is null");return(Criteria)this;
}publicCriteria andCityIdIsNotNull() {
addCriterion("city_id is not null");return(Criteria)this;
}publicCriteria andCityIdEqualTo(Integer value) {
addCriterion("city_id =", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdNotEqualTo(Integer value) {
addCriterion("city_id <>", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdGreaterThan(Integer value) {
addCriterion("city_id >", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdGreaterThanOrEqualTo(Integer value) {
addCriterion("city_id >=", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdLessThan(Integer value) {
addCriterion("city_id <", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdLessThanOrEqualTo(Integer value) {
addCriterion("city_id <=", value, "cityId");return(Criteria)this;
}publicCriteria andCityIdIn(List<Integer>values) {
addCriterion("city_id in", values, "cityId");return(Criteria)this;
}publicCriteria andCityIdNotIn(List<Integer>values) {
addCriterion("city_id not in", values, "cityId");return(Criteria)this;
}publicCriteria andCityIdBetween(Integer value1, Integer value2) {
addCriterion("city_id between", value1, value2, "cityId");return(Criteria)this;
}publicCriteria andCityIdNotBetween(Integer value1, Integer value2) {
addCriterion("city_id not between", value1, value2, "cityId");return(Criteria)this;
}publicCriteria andOrderIdIsNull() {
addCriterion("order_id is null");return(Criteria)this;
}publicCriteria andOrderIdIsNotNull() {
addCriterion("order_id is not null");return(Criteria)this;
}publicCriteria andOrderIdEqualTo(Integer value) {
addCriterion("order_id =", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdNotEqualTo(Integer value) {
addCriterion("order_id <>", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdGreaterThan(Integer value) {
addCriterion("order_id >", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdGreaterThanOrEqualTo(Integer value) {
addCriterion("order_id >=", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdLessThan(Integer value) {
addCriterion("order_id <", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdLessThanOrEqualTo(Integer value) {
addCriterion("order_id <=", value, "orderId");return(Criteria)this;
}publicCriteria andOrderIdIn(List<Integer>values) {
addCriterion("order_id in", values, "orderId");return(Criteria)this;
}publicCriteria andOrderIdNotIn(List<Integer>values) {
addCriterion("order_id not in", values, "orderId");return(Criteria)this;
}publicCriteria andOrderIdBetween(Integer value1, Integer value2) {
addCriterion("order_id between", value1, value2, "orderId");return(Criteria)this;
}publicCriteria andOrderIdNotBetween(Integer value1, Integer value2) {
addCriterion("order_id not between", value1, value2, "orderId");return(Criteria)this;
}publicCriteria andNameIsNull() {
addCriterion("name is null");return(Criteria)this;
}publicCriteria andNameIsNotNull() {
addCriterion("name is not null");return(Criteria)this;
}publicCriteria andNameEqualTo(String value) {
addCriterion("name =", value, "name");return(Criteria)this;
}publicCriteria andNameNotEqualTo(String value) {
addCriterion("name <>", value, "name");return(Criteria)this;
}publicCriteria andNameGreaterThan(String value) {
addCriterion("name >", value, "name");return(Criteria)this;
}publicCriteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("name >=", value, "name");return(Criteria)this;
}publicCriteria andNameLessThan(String value) {
addCriterion("name <", value, "name");return(Criteria)this;
}publicCriteria andNameLessThanOrEqualTo(String value) {
addCriterion("name <=", value, "name");return(Criteria)this;
}publicCriteria andNameLike(String value) {
addCriterion("name like", value, "name");return(Criteria)this;
}publicCriteria andNameNotLike(String value) {
addCriterion("name not like", value, "name");return(Criteria)this;
}publicCriteria andNameIn(List<String>values) {
addCriterion("name in", values, "name");return(Criteria)this;
}publicCriteria andNameNotIn(List<String>values) {
addCriterion("name not in", values, "name");return(Criteria)this;
}publicCriteria andNameBetween(String value1, String value2) {
addCriterion("name between", value1, value2, "name");return(Criteria)this;
}publicCriteria andNameNotBetween(String value1, String value2) {
addCriterion("name not between", value1, value2, "name");return(Criteria)this;
}publicCriteria andTypeIsNull() {
addCriterion("type is null");return(Criteria)this;
}publicCriteria andTypeIsNotNull() {
addCriterion("type is not null");return(Criteria)this;
}publicCriteria andTypeEqualTo(Byte value) {
addCriterion("type =", value, "type");return(Criteria)this;
}publicCriteria andTypeNotEqualTo(Byte value) {
addCriterion("type <>", value, "type");return(Criteria)this;
}publicCriteria andTypeGreaterThan(Byte value) {
addCriterion("type >", value, "type");return(Criteria)this;
}publicCriteria andTypeGreaterThanOrEqualTo(Byte value) {
addCriterion("type >=", value, "type");return(Criteria)this;
}publicCriteria andTypeLessThan(Byte value) {
addCriterion("type <", value, "type");return(Criteria)this;
}publicCriteria andTypeLessThanOrEqualTo(Byte value) {
addCriterion("type <=", value, "type");return(Criteria)this;
}publicCriteria andTypeIn(List<Byte>values) {
addCriterion("type in", values, "type");return(Criteria)this;
}publicCriteria andTypeNotIn(List<Byte>values) {
addCriterion("type not in", values, "type");return(Criteria)this;
}publicCriteria andTypeBetween(Byte value1, Byte value2) {
addCriterion("type between", value1, value2, "type");return(Criteria)this;
}publicCriteria andTypeNotBetween(Byte value1, Byte value2) {
addCriterion("type not between", value1, value2, "type");return(Criteria)this;
}publicCriteria andCostIsNull() {
addCriterion("cost is null");return(Criteria)this;
}publicCriteria andCostIsNotNull() {
addCriterion("cost is not null");return(Criteria)this;
}publicCriteria andCostEqualTo(BigDecimal value) {
addCriterion("cost =", value, "cost");return(Criteria)this;
}publicCriteria andCostNotEqualTo(BigDecimal value) {
addCriterion("cost <>", value, "cost");return(Criteria)this;
}publicCriteria andCostGreaterThan(BigDecimal value) {
addCriterion("cost >", value, "cost");return(Criteria)this;
}publicCriteria andCostGreaterThanOrEqualTo(BigDecimal value) {
addCriterion("cost >=", value, "cost");return(Criteria)this;
}publicCriteria andCostLessThan(BigDecimal value) {
addCriterion("cost <", value, "cost");return(Criteria)this;
}publicCriteria andCostLessThanOrEqualTo(BigDecimal value) {
addCriterion("cost <=", value, "cost");return(Criteria)this;
}publicCriteria andCostIn(List<BigDecimal>values) {
addCriterion("cost in", values, "cost");return(Criteria)this;
}publicCriteria andCostNotIn(List<BigDecimal>values) {
addCriterion("cost not in", values, "cost");return(Criteria)this;
}publicCriteria andCostBetween(BigDecimal value1, BigDecimal value2) {
addCriterion("cost between", value1, value2, "cost");return(Criteria)this;
}publicCriteria andCostNotBetween(BigDecimal value1, BigDecimal value2) {
addCriterion("cost not between", value1, value2, "cost");return(Criteria)this;
}publicCriteria andShardxIsNull() {
addCriterion("shardx is null");return(Criteria)this;
}publicCriteria andShardxIsNotNull() {
addCriterion("shardx is not null");return(Criteria)this;
}publicCriteria andShardxEqualTo(Integer value) {
addCriterion("shardx =", value, "shardx");return(Criteria)this;
}publicCriteria andShardxNotEqualTo(Integer value) {
addCriterion("shardx <>", value, "shardx");return(Criteria)this;
}publicCriteria andShardxGreaterThan(Integer value) {
addCriterion("shardx >", value, "shardx");return(Criteria)this;
}publicCriteria andShardxGreaterThanOrEqualTo(Integer value) {
addCriterion("shardx >=", value, "shardx");return(Criteria)this;
}publicCriteria andShardxLessThan(Integer value) {
addCriterion("shardx <", value, "shardx");return(Criteria)this;
}publicCriteria andShardxLessThanOrEqualTo(Integer value) {
addCriterion("shardx <=", value, "shardx");return(Criteria)this;
}publicCriteria andShardxIn(List<Integer>values) {
addCriterion("shardx in", values, "shardx");return(Criteria)this;
}publicCriteria andShardxNotIn(List<Integer>values) {
addCriterion("shardx not in", values, "shardx");return(Criteria)this;
}publicCriteria andShardxBetween(Integer value1, Integer value2) {
addCriterion("shardx between", value1, value2, "shardx");return(Criteria)this;
}publicCriteria andShardxNotBetween(Integer value1, Integer value2) {
addCriterion("shardx not between", value1, value2, "shardx");return(Criteria)this;
}publicCriteria andShardyIsNull() {
addCriterion("shardy is null");return(Criteria)this;
}publicCriteria andShardyIsNotNull() {
addCriterion("shardy is not null");return(Criteria)this;
}publicCriteria andShardyEqualTo(Integer value) {
addCriterion("shardy =", value, "shardy");return(Criteria)this;
}publicCriteria andShardyNotEqualTo(Integer value) {
addCriterion("shardy <>", value, "shardy");return(Criteria)this;
}publicCriteria andShardyGreaterThan(Integer value) {
addCriterion("shardy >", value, "shardy");return(Criteria)this;
}publicCriteria andShardyGreaterThanOrEqualTo(Integer value) {
addCriterion("shardy >=", value, "shardy");return(Criteria)this;
}publicCriteria andShardyLessThan(Integer value) {
addCriterion("shardy <", value, "shardy");return(Criteria)this;
}publicCriteria andShardyLessThanOrEqualTo(Integer value) {
addCriterion("shardy <=", value, "shardy");return(Criteria)this;
}publicCriteria andShardyIn(List<Integer>values) {
addCriterion("shardy in", values, "shardy");return(Criteria)this;
}publicCriteria andShardyNotIn(List<Integer>values) {
addCriterion("shardy not in", values, "shardy");return(Criteria)this;
}publicCriteria andShardyBetween(Integer value1, Integer value2) {
addCriterion("shardy between", value1, value2, "shardy");return(Criteria)this;
}publicCriteria andShardyNotBetween(Integer value1, Integer value2) {
addCriterion("shardy not between", value1, value2, "shardy");return(Criteria)this;
}publicCriteria andShardzIsNull() {
addCriterion("shardz is null");return(Criteria)this;
}publicCriteria andShardzIsNotNull() {
addCriterion("shardz is not null");return(Criteria)this;
}publicCriteria andShardzEqualTo(Integer value) {
addCriterion("shardz =", value, "shardz");return(Criteria)this;
}publicCriteria andShardzNotEqualTo(Integer value) {
addCriterion("shardz <>", value, "shardz");return(Criteria)this;
}publicCriteria andShardzGreaterThan(Integer value) {
addCriterion("shardz >", value, "shardz");return(Criteria)this;
}publicCriteria andShardzGreaterThanOrEqualTo(Integer value) {
addCriterion("shardz >=", value, "shardz");return(Criteria)this;
}publicCriteria andShardzLessThan(Integer value) {
addCriterion("shardz <", value, "shardz");return(Criteria)this;
}publicCriteria andShardzLessThanOrEqualTo(Integer value) {
addCriterion("shardz <=", value, "shardz");return(Criteria)this;
}publicCriteria andShardzIn(List<Integer>values) {
addCriterion("shardz in", values, "shardz");return(Criteria)this;
}publicCriteria andShardzNotIn(List<Integer>values) {
addCriterion("shardz not in", values, "shardz");return(Criteria)this;
}publicCriteria andShardzBetween(Integer value1, Integer value2) {
addCriterion("shardz between", value1, value2, "shardz");return(Criteria)this;
}publicCriteria andShardzNotBetween(Integer value1, Integer value2) {
addCriterion("shardz not between", value1, value2, "shardz");return(Criteria)this;
}
}publicstaticclassCriteriaextendsGeneratedCriteria {protectedCriteria() {super();
}
}publicstaticclassCriterion {privateString condition;privateObject value;privateObject secondValue;privatebooleannoValue;privatebooleansingleValue;privatebooleanbetweenValue;privatebooleanlistValue;privateString typeHandler;publicString getCondition() {returncondition;
}publicObject getValue() {returnvalue;
}publicObject getSecondValue() {returnsecondValue;
}publicbooleanisNoValue() {returnnoValue;
}publicbooleanisSingleValue() {returnsingleValue;
}publicbooleanisBetweenValue() {returnbetweenValue;
}publicbooleanisListValue() {returnlistValue;
}publicString getTypeHandler() {returntypeHandler;
}protectedCriterion(String condition) {super();this.condition =condition;this.typeHandler =null;this.noValue =true;
}protectedCriterion(String condition, Object value, String typeHandler) {super();this.condition =condition;this.value =value;this.typeHandler =typeHandler;if(valueinstanceofList<?>) {this.listValue =true;
}else{this.singleValue =true;
}
}protectedCriterion(String condition, Object value) {this(condition, value,null);
}protectedCriterion(String condition, Object value, Object secondValue, String typeHandler) {super();this.condition =condition;this.value =value;this.secondValue =secondValue;this.typeHandler =typeHandler;this.betweenValue =true;
}protectedCriterion(String condition, Object value, Object secondValue) {this(condition, value, secondValue,null);
}
}
}
redcliff.cobara.entity.CobaraOrderDetailExample.java
上面的entity包下的CobaraOrderDetailExample.java文件同样也是用于映射的。只不过它用于构造复杂的筛选条件。注意到,里面的变量定义为protected类型,方法定义为public类型;结构public class CobaraOrderDetailExample。两个Java类独立存在,不相互依存。
packageredcliff.cobara.mapper;importcom.dianwoba.redcliff.cobara.entity.CobaraOrderDetail;importcom.dianwoba.redcliff.cobara.entity.CobaraOrderDetailExample;importjava.util.List;importorg.apache.ibatis.annotations.Param;publicinterfaceCobaraOrderDetailMapper {intcountByExample(CobaraOrderDetailExample example);intdeleteByExample(CobaraOrderDetailExample example);intdeleteByPrimaryKey(Long id);intinsert(CobaraOrderDetail record);intinsertSelective(CobaraOrderDetail record);
List<CobaraOrderDetail>selectByExample(CobaraOrderDetailExample example);
CobaraOrderDetail selectByPrimaryKey(Long id);intupdateByExampleSelective(@Param("record") CobaraOrderDetail record, @Param("example") CobaraOrderDetailExample example);intupdateByExample(@Param("record") CobaraOrderDetail record, @Param("example") CobaraOrderDetailExample example);intupdateByPrimaryKeySelective(CobaraOrderDetail record);intupdateByPrimaryKey(CobaraOrderDetail record);
}
redcliff.cobara.mapper.CobaraOrderDetailMapper.java
上面的mapper包下的CobaraOrderDetailMapper.java类中,定义了基于类CobaraOrderDetailExample的CRUD方法。从结构public interface CobarOrderDetailExample上来看,当然是没有实现的。
packageredcliff.cobara.mapper;publicinterfaceCobaraOrderDetailMapperExtextendsCobaraOrderDetailMapper {
}
redcliff.cobara.mapper.CobaraOrderDetailMapperExt.java
上面的mapper包下的CobaraOrderDetailMapperExt.java类中,是一个空的接口。从结构上来看public interface CobaraOrderDetailMapperExt extends CobaraOrderDetailMapper。从名字上来看Ext是英文单词external的简写,'external'的本意是外部的,外面的简写。可见此类是用于扩展的。
redcliff.cobara.mapper.CobaraOrderDetailMapper.xml
上面的redcliff.cobara.mapper.CobaraOrderDetailMapperExt.xml映射文件定义了基本的CRUD四个方法。
<?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"><mappernamespace="redcliff.cobara.mapper.CobaraOrderDetailMapperExt"></mapper>
redcliff.cobara.mapper.CobaraOrderDetailMapperExt.xml
上面的redcliff.cobara.mapper.CobaraOrderDetailMapperExt.xml映射文件,扩展了CRUD操作,因为从mapper标签的唯一属性namespace两个xml是完全一样的。
以上的这些代码,用一句话描述就是:“entity.java建立了与数据库的映射,mapper.java中定义了CRUD的方法,mapper.xml中实现了方法”,那么他们是怎么联系起来的呢?mapper.java中的方法接收的参数是entityExample(半成品的sql语句)。mapper标签的namespace指示到了mapper.java类,意思是我对你的方法进行了实现。
附录,补充
学习资料参考地址