myBatis

package myBatis;

public class TestOne {

    public static void main(String[] args) {
        
        /**
         * 
         * Mybatis:
         * 
         * 1.
         * collection   一对多
         * association  多对一
         * 
         * 
         * 2.
         * javaType  pojo中属性的类型
         * 
         * ofType     映射到list集合属性中pojo的类型
         * 
         * 
         * 3.  #{}  ${}
         * #{} 传入的数据会加'' 
         * 
         * 
         * 
         * 4.  Map 映射     Type : java类型
         * resultMap  resultType  parameterMap  parameterType
         * 
         * resultMap  resultType
         * java查询结果集和java对象的映射
         * 
         * resultMap : 查询结果集中的列一一映射到bean对象的各个属性上
         * 
         * resultType :bean中的对象类,此时可以省略resultMap,结果集中的属性必须和必须和bean的属性是一一对应的,此时大小写不敏感
         * 此时将值设置对应的java类即可。不需要上述resultMap的对应关系。
         * 
         * 
         * http://blog.csdn.net/u010235716/article/details/51698422
         * parameterMap 传入多个参数,不推荐使用
         * parameterType 传入参数分两种类型
         * 基本数据类型
         * 复杂数据类型 类和Map
         * 
         * 5.refid
         * 一个标签定义多个字段,下面可以直接引入
         * 
         * 
         * 6.数组中的元素  foreach
         * <foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")">
         * 
         * 
         
         publicclass  User  {
         
            private int  id;
            
            private String  username;
            
            private String  mobile;
            
            private List<Post> posts;
        }
        
        ------------------  resultMap    resultType
    <resultMap  type="User"  id="resultUserMap">
         <result  property="id" javaType="int"  column="user_id"  />
         <result  property="username" javaType="string"  column="username"  />
         <result  property="mobile"   column="mobile"  />                       
         <!--javatype指定的是user对象的属性的类型(例如id,posts),而oftype指定的是映射到list集合属性中pojo的类型(本例指的是post类型)-->
         <collection  property="posts"  ofType="com.spenglu.Post"  javaType="java.util.ArrayList"  column="userid">
             <id  property="id"  column="post_id"  javaType="int"  jdbcType="INTEGER"/>   
             <result  property="title"  column="title"  javaType="string"  jdbcType="VARCHAR"/>
             <result  property="content"  column="content"  javaType="string"  jdbcType="VARCHAR"/>
         </collection>
    </resultMap>
    
    -------------------------------parameterType
    基本类型
   <sql id="Base_Column_List" >
        id, car_dept_name, car_maker_name, icon,car_maker_py,hot_type
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
        select 
        <include refid="Base_Column_List" />
        from common_car_make
        where id = #{id,jdbcType=BIGINT}
  </select>
    
    类类型
 <select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="java.util.Map">
    select
    <include refid="Base_Column_List" />
    from common_car_make cm
    where 1=1
    <if test="id != null">
        and  cm.id = #{id,jdbcType=DECIMAL}
    </if>
    <if test="carDeptName != null">
        and  cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR}
    </if>
    <if test="carMakerName != null">
        and  cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR}
    </if>
    <if test="hotType != null" >
       and  cm.hot_type = #{hotType,jdbcType=BIGINT}
    </if>
    ORDER BY cm.id
</select>
    
    map中包含数组的情况
<select id="selectProOrderByOrderId" resultType="com.epeit.api.model.ProOrder" parameterType="java.util.HashMap" >
    select sum(pro_order_num) proOrderNum,product_id productId,promotion_id promotionId
    from pro_order
    where 1=1
    <if test="orderIds != null">
      and
        <foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")">
            #{item,jdbcType=BIGINT}
        </foreach>
    </if>
    GROUP BY product_id,promotion_id
</select>
         * 
         * 
         * 
         */

    }

}

 

 

posted @ 2018-02-28 18:35  aLa神灯  阅读(103)  评论(0编辑  收藏  举报