首页 > 学院 > 开发设计 > 正文

总结一下工作中用到的Mybatis业务逻辑

2019-11-14 15:29:16
字体:
来源:转载
供稿:网友

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.VEVb.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 <mapper namespace="org.iMybatis.abc.dao.UserDao">   4     <cache type="PERPETUAL" eviction="LRU" flushInterval="60000"   5         size="512" readOnly="true" />   6     <resultMap id="userResultMap" type="UserDto">   7         <id PRoperty="userid" column="userid" />   8         <result property="username" column="username" />   9         <result property="passWord" column="password" />  10     </resultMap>  11     <sql id="userColumns"> userid,username,password </sql>  12     <select id="queryUsers" parameterType="UserDto" resultType="UserDto"  13         useCache="false">  14         select <include refid="userColumns" />  15         from t_user t where t.username = #{username}  16     </select>  17     <insert id="insertUser" parameterType="UserDto"  18         useGeneratedKeys="true" keyProperty="userid">  19         insert into t_user (userid,username,password)  20         values (#{userid},#{username},#{password})  21     </insert>  22     <update id="updateUser" parameterType="UserDto">  23         update t_user set  24         username= #{username},  25         password = #{password},  26         where userid = #{userid}  27     </update>  28     <delete id="deleteUser" parameterType="UserDto">  29         delete 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。

<resultMap id="userResultMap" type="User">      <id property=" userid " column="userid" />      <result property="username" column="username" />      <result property="password" column="password" />  </resultMap> 
resultMap 配置

  resultMap提供了从数据库表列名到java对象属性的映射管理,示例只是提供了最简单的情况。在mapper配置文件中可以配置多个resultMap,不同的resultMap用id加以区分。type属性标记java类型(别名)。子元素中的property指带java中的属性,column指带数据库表的列名。

  Example类,用于构造复杂的筛选条件,详细分析如下:

上面的sql语句,创建了表'order_detail',以及各个字段。

package redcliff.cobara.entity;import java.math.BigDecimal;public class CobaraOrderDetail {    private Long id;    private Integer cityId;    private Integer orderId;    private String name;    private Byte type;    private BigDecimal cost;    private Integer shardx;    private Integer shardy;    private Integer shardz;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public Integer getCityId() {        return cityId;    }    public void setCityId(Integer cityId) {        this.cityId = cityId;    }    public Integer getOrderId() {        return orderId;    }    public void setOrderId(Integer orderId) {        this.orderId = orderId;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.trim();    }    public Byte getType() {        return type;    }    public void setType(Byte type) {        this.type = type;    }    public BigDecimal getCost() {        return cost;    }    public void setCost(BigDecimal cost) {        this.cost = cost;    }    public Integer getShardx() {        return shardx;    }    public void setShardx(Integer shardx) {        this.shardx = shardx;    }    public Integer getShardy() {        return shardy;    }    public void setShardy(Integer shardy) {        this.shardy = shardy;    }    public Integer getShardz() {        return shardz;    }    public void setShardz(Integer shardz) {        this.shardz = shardz;    }}
redcliff.cobara.entity.CobaraOrderDetail.java

上面的entitiy包下的CobaraOrderDetail.java文件定义了实体类,是数据库的各个字段在Java web项目中一模一样的写照。当然了它是mybatis的generator生成器自动生成的。注意到,结构public class CobaraOrderDetail;里面的变量定义为private类型,方法定义为public类型

package redcliff.cobara.entity;import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;public class CobaraOrderDetailExample {    protected String orderByClause;    protected boolean distinct;    protected List<Criteria> oredCriteria;    public CobaraOrderDetailExample() {        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(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 andCityIdIsNull() {            addCriterion("city_id is null");            return (Criteria) this;        }        public Criteria andCityIdIsNotNull() {            addCriterion("city_id is not null");            return (Criteria) this;        }        public Criteria andCityIdEqualTo(Integer value) {            addCriterion("city_id =", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdNotEqualTo(Integer value) {            addCriterion("city_id <>", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdGreaterThan(Integer value) {            addCriterion("city_id >", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdGreaterThanOrEqualTo(Integer value) {            addCriterion("city_id >=", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdLessThan(Integer value) {            addCriterion("city_id <", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdLessThanOrEqualTo(Integer value) {            addCriterion("city_id <=", value, "cityId");            return (Criteria) this;        }        public Criteria andCityIdIn(List<Integer> values) {            addCriterion("city_id in", values, "cityId");            return (Criteria) this;        }        public Criteria andCityIdNotIn(List<Integer> values) {            addCriterion("city_id not in", values, "cityId");            return (Criteria) this;        }        public Criteria andCityIdBetween(Integer value1, Integer value2) {            addCriterion("city_id between", value1, value2, "cityId");            return (Criteria) this;        }        public Criteria andCityIdNotBetween(Integer value1, Integer value2) {            addCriterion("city_id not between", value1, value2, "cityId");            return (Criteria) this;        }        public Criteria andOrderIdIsNull() {            addCriterion("order_id is null");            return (Criteria) this;        }        public Criteria andOrderIdIsNotNull() {            addCriterion("order_id is not null");            return (Criteria) this;        }        public Criteria andOrderIdEqualTo(Integer value) {            addCriterion("order_id =", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdNotEqualTo(Integer value) {            addCriterion("order_id <>", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdGreaterThan(Integer value) {            addCriterion("order_id >", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdGreaterThanOrEqualTo(Integer value) {            addCriterion("order_id >=", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdLessThan(Integer value) {            addCriterion("order_id <", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdLessThanOrEqualTo(Integer value) {            addCriterion("order_id <=", value, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdIn(List<Integer> values) {            addCriterion("order_id in", values, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdNotIn(List<Integer> values) {            addCriterion("order_id not in", values, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdBetween(Integer value1, Integer value2) {            addCriterion("order_id between", value1, value2, "orderId");            return (Criteria) this;        }        public Criteria andOrderIdNotBetween(Integer value1, Integer value2) {            addCriterion("order_id not between", value1, value2, "orderId");            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 andTypeIsNull() {            addCriterion("type is null");            return (Criteria) this;        }        public Criteria andTypeIsNotNull() {            addCriterion("type is not null");            return (Criteria) this;        }        public Criteria andTypeEqualTo(Byte value) {            addCriterion("type =", value, "type");            return (Criteria) this;        }        public Criteria andTypeNotEqualTo(Byte value) {            addCriterion("type <>", value, "type");            return (Criteria) this;        }        public Criteria andTypeGreaterThan(Byte value) {            addCriterion("type >", value, "type");            return (Criteria) this;        }        public Criteria andTypeGreaterThanOrEqualTo(Byte value) {            addCriterion("type >=", value, "type");            return (Criteria) this;        }        public Criteria andTypeLessThan(Byte value) {            addCriterion("type <", value, "type");            return (Criteria) this;        }        public Criteria andTypeLessThanOrEqualTo(Byte value) {            addCriterion("type <=", value, "type");            return (Criteria) this;        }        public Criteria andTypeIn(List<Byte> values) {            addCriterion("type in", values, "type");            return (Criteria) this;        }        public Criteria andTypeNotIn(List<Byte> values) {            addCriterion("type not in", values, "type");            return (Criteria) this;        }        public Criteria andTypeBetween(Byte value1, Byte value2) {            addCriterion("type between", value1, value2, "type");            return (Criteria) this;        }        public Criteria andTypeNotBetween(Byte value1, Byte value2) {            addCriterion("type not between", value1, value2, "type");            return (Criteria) this;        }        public Criteria andCostIsNull() {            addCriterion("cost is null");            return (Criteria) this;        }        public Criteria andCostIsNotNull() {            addCriterion("cost is not null");            return (Criteria) this;        }        public Criteria andCostEqualTo(BigDecimal value) {            addCriterion("cost =", value, "cost");            return (Criteria) this;        }        public Criteria andCostNotEqualTo(BigDecimal value) {            addCriterion("cost <>", value, "cost");            return (Criteria) this;        }        public Criteria andCostGreaterThan(BigDecimal value) {            addCriterion("cost >", value, "cost");            return (Criteria) this;        }        public Criteria andCostGreaterThanOrEqualTo(BigDecimal value) {            addCriterion("cost >=", value, "cost");            return (Criteria) this;        }        public Criteria andCostLessThan(BigDecimal value) {            addCriterion("cost <", value, "cost");            return (Criteria) this;        }        public Criteria andCostLessThanOrEqualTo(BigDecimal value) {            addCriterion("cost <=", value, "cost");            return (Criteria) this;        }        public Criteria andCostIn(List<BigDecimal> values) {            addCriterion("cost in", values, "cost");            return (Criteria) this;        }        public Criteria andCostNotIn(List<BigDecimal> values) {            addCriterion("cost not in", values, "cost");            return (Criteria) this;        }        public Criteria andCostBetween(BigDecimal value1, BigDecimal value2) {            addCriterion("cost between", value1, value2, "cost");            return (Criteria) this;        }        public Criteria andCostNotBetween(BigDecimal value1, BigDecimal value2) {            addCriterion("cost not between", value1, value2, "cost");            return (Criteria) this;        }        public Criteria andShardxIsNull() {            addCriterion("shardx is null");            return (Criteria) this;        }        public Criteria andShardxIsNotNull() {            addCriterion("shardx is not null");            return (Criteria) this;        }        public Criteria andShardxEqualTo(Integer value) {            addCriterion("shardx =", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxNotEqualTo(Integer value) {            addCriterion("shardx <>", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxGreaterThan(Integer value) {            addCriterion("shardx >", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxGreaterThanOrEqualTo(Integer value) {            addCriterion("shardx >=", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxLessThan(Integer value) {            addCriterion("shardx <", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxLessThanOrEqualTo(Integer value) {            addCriterion("shardx <=", value, "shardx");            return (Criteria) this;        }        public Criteria andShardxIn(List<Integer> values) {            addCriterion("shardx in", values, "shardx");            return (Criteria) this;        }        public Criteria andShardxNotIn(List<Integer> values) {            addCriterion("shardx not in", values, "shardx");            return (Criteria) this;        }        public Criteria andShardxBetween(Integer value1, Integer value2) {            addCriterion("shardx between", value1, value2, "shardx");            return (Criteria) this;        }        public Criteria andShardxNotBetween(Integer value1, Integer value2) {            addCriterion("shardx not between", value1, value2, "shardx");            return (Criteria) this;        }        public Criteria andShardyIsNull() {            addCriterion("shardy is null");            return (Criteria) this;        }        public Criteria andShardyIsNotNull() {            addCriterion("shardy is not null");            return (Criteria) this;        }        public Criteria andShardyEqualTo(Integer value) {            addCriterion("shardy =", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyNotEqualTo(Integer value) {            addCriterion("shardy <>", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyGreaterThan(Integer value) {            addCriterion("shardy >", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyGreaterThanOrEqualTo(Integer value) {            addCriterion("shardy >=", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyLessThan(Integer value) {            addCriterion("shardy <", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyLessThanOrEqualTo(Integer value) {            addCriterion("shardy <=", value, "shardy");            return (Criteria) this;        }        public Criteria andShardyIn(List<Integer> values) {            addCriterion("shardy in", values, "shardy");            return (Criteria) this;        }        public Criteria andShardyNotIn(List<Integer> values) {            addCriterion("shardy not in", values, "shardy");            return (Criteria) this;        }        public Criteria andShardyBetween(Integer value1, Integer value2) {            addCriterion("shardy between", value1, value2, "shardy");            return (Criteria) this;        }        public Criteria andShardyNotBetween(Integer value1, Integer value2) {            addCriterion("shardy not between", value1, value2, "shardy");            return (Criteria) this;        }        public Criteria andShardzIsNull() {            addCriterion("shardz is null");            return (Criteria) this;        }        public Criteria andShardzIsNotNull() {            addCriterion("shardz is not null");            return (Criteria) this;        }        public Criteria andShardzEqualTo(Integer value) {            addCriterion("shardz =", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzNotEqualTo(Integer value) {            addCriterion("shardz <>", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzGreaterThan(Integer value) {            addCriterion("shardz >", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzGreaterThanOrEqualTo(Integer value) {            addCriterion("shardz >=", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzLessThan(Integer value) {            addCriterion("shardz <", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzLessThanOrEqualTo(Integer value) {            addCriterion("shardz <=", value, "shardz");            return (Criteria) this;        }        public Criteria andShardzIn(List<Integer> values) {            addCriterion("shardz in", values, "shardz");            return (Criteria) this;        }        public Criteria andShardzNotIn(List<Integer> values) {            addCriterion("shardz not in", values, "shardz");            return (Criteria) this;        }        public Criteria andShardzBetween(Integer value1, Integer value2) {            addCriterion("shardz between", value1, value2, "shardz");            return (Criteria) this;        }        public Criteria andShardzNotBetween(Integer value1, Integer value2) {            addCriterion("shardz not between", value1, value2, "shardz");            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);        }    }}
redcliff.cobara.entity.CobaraOrderDetailExample.java

上面的entity包下的CobaraOrderDetailExample.java文件同样也是用于映射的。只不过它用于构造复杂的筛选条件。注意到,里面的变量定义为protected类型,方法定义为public类型;结构public class CobaraOrderDetailExample。两个Java类独立存在,不相互依存。

package redcliff.cobara.mapper;import com.dianwoba.redcliff.cobara.entity.CobaraOrderDetail;import com.dianwoba.redcliff.cobara.entity.CobaraOrderDetailExample;import java.util.List;import org.apache.ibatis.annotations.Param;public interface CobaraOrderDetailMapper {    int countByExample(CobaraOrderDetailExample example);    int deleteByExample(CobaraOrderDetailExample example);    int deleteByPrimaryKey(Long id);    int insert(CobaraOrderDetail record);    int insertSelective(CobaraOrderDetail record);    List<CobaraOrderDetail> selectByExample(CobaraOrderDetailExample example);    CobaraOrderDetail selectByPrimaryKey(Long id);    int updateByExampleSelective(@Param("record") CobaraOrderDetail record, @Param("example") CobaraOrderDetailExample example);    int updateByExample(@Param("record") CobaraOrderDetail record, @Param("example") CobaraOrderDetailExample example);    int updateByPrimaryKeySelective(CobaraOrderDetail record);    int updateByPrimaryKey(CobaraOrderDetail record);}
redcliff.cobara.mapper.CobaraOrderDetailMapper.java

上面的mapper包下的CobaraOrderDetailMapper.java类中,定义了基于类CobaraOrderDetailExample的CRUD方法。从结构public interface CobarOrderDetailExample上来看,当然是没有实现的。

package redcliff.cobara.mapper;public interface CobaraOrderDetailMapperExt extends CobaraOrderDetailMapper {}
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" ><mapper namespace="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类,意思是我对你的方法进行了实现。

附录,补充

 学习资料参考地址


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表