Mybatis使用(3)

本文详细介绍了在MyBatis3中example类的使用,包括在generatorConfig.xml中设置targetRuntime为"MyBatis3",启用enableCountByExample选项,以及生成的TUserExample.java、TUserMapper.java、TUserMapper.xml文件的作用。还提到了Criteria作为条件集合的功能,并提及了相关的测试代码。
摘要由CSDN通过智能技术生成

1.example类的使用

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>
	<!-- 引入配置文件 -->
	<properties resource="db.properties" />

	<!-- 加载数据库驱动 -->
	<classPathEntry location="${class_path}" />
	
	<!-- context:生成一组对象的环境 
			id:必选,上下文id,用于在生成错误时提示 
			defaultModelType:指定生成对象的样式 
				 1,conditional:类似hierarchical;
				 2,flat:所有内容(主键,blob)等全部生成在一个对象中,推荐使用; 
		  		 3,hierarchical:主键生成一个XXKey对象(key class),Blob等单独生成一个对象,其他简单属性在一个对象中(record class) 
		  	targetRuntime: 
		  		 1,MyBatis3:默认的值,生成基于MyBatis3.x以上版本的内容,包括XXXBySample; 
		         2,MyBatis3Simple:类似MyBatis3,只是不生成XXXBySample; 
     -->
	<context id="context1" targetRuntime="MyBatis3"	defaultModelType="flat">
	
	    <!-- 生成的Java文件的编码 -->
    	<property name="javaFileEncoding" value="UTF-8"/>
	
	
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:-->
			<property name="suppressAllComments" value="false" />
			<!-- 阻止注释中包含时间戳 true:是 : false:-->
			<property name="suppressDate" value="true" />
			<!--  注释是否包含数据库表的注释信息  true:是 : false:-->
			<property name="addRemarkComments" value="true" />
		</commentGenerator>
		
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="${jdbc_driver}"
			connectionURL="${jdbc_url}" userId="${jdbc_username}" password="${jdbc_password}" />


        <!-- java模型创建器,是必须要的元素   负责:1,key类(见context的defaultModelType);2,java类;3,查询类
        	targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
        	targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
     	-->
		<javaModelGenerator targetPackage="com.enjoylearning.mybatis.entity" targetProject="${project_src}">
			<!-- 设置一个根对象,
	                      如果设置了这个根对象,那么生成的keyClass或者recordClass会继承这个类;在Table的rootClass属性中可以覆盖该选项
	                      注意:如果在key class或者record class中有root class相同的属性,MBG就不会重新生成这些属性了,包括:
	                1,属性名相同,类型相同,有相同的getter/setter方法;
	         -->
			<property name="rootClass" value="com.enjoylearning.mybatis.entity.BaseEntity" />
		</javaModelGenerator>


		<!-- 生成SQL map的XML文件生成器,
            targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
        	targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,MBG不会自动建目录
         -->
		<sqlMapGenerator targetPackage="." targetProject="${project_mapper_xml}">
		</sqlMapGenerator>
		
		
		 <!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口 
		        type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
		            1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
		            2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
		            3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
		        注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
		    -->		
    	<javaClientGenerator  targetPackage="com.enjoylearning.mybatis.mapper"	targetProject="${project_src}" type="XMLMAPPER" />




		<!-- shema 数据库 tableName表明 -->
		<table schema="${jdbc_username}" tableName="%" >
			<generatedKey column="id" sqlStatement="MySql"/>
		</table>


	</context>
</generatorConfiguration>

context标签中,targetRuntime=“MyBatis3”

<table schema="${jdbc_username}" tableName="t_job_history"
			 enableCountByExample="false"
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>

将配置改为enableCountByExample = “true”,或者不写(默认为true)

生成的文件:

1)TUserExample.java

package com.enjoylearning.mybatis.entity;

import java.util.ArrayList;
import java.util.List;

public class TUserExample {
   
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_user
     *
     * @mbg.generated
     */
    protected String orderByClause;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_user
     *
     * @mbg.generated
     */
    protected boolean distinct;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database table t_user
     *
     * @mbg.generated
     */
    protected List<Criteria> oredCriteria;

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public TUserExample() {
   
        oredCriteria = new ArrayList<Criteria>();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public void setOrderByClause(String orderByClause) {
   
        this.orderByClause = orderByClause;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public String getOrderByClause() {
   
        return orderByClause;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public void setDistinct(boolean distinct) {
   
        this.distinct = distinct;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public boolean isDistinct() {
   
        return distinct;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public List<Criteria> getOredCriteria() {
   
        return oredCriteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public void or(Criteria criteria) {
   
        oredCriteria.add(criteria);
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public Criteria or() {
   
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public Criteria createCriteria() {
   
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
   
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    protected Criteria createCriteriaInternal() {
   
        Criteria criteria = new Criteria();
        return criteria;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table t_user
     *
     * @mbg.generated
     */
    public void clear() {
   
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }

    /**
     * This class was generated by MyBatis Generator.
     * This class corresponds to the database table t_user
     *
     * @mbg.generated
     */
    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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值