1 2 3 4

SqlMapConfig.xml

mybatis的全局配置文件:

1.properties(属性)

将数据库连接参数单独配置在db.properties中,只需要在SqlMapConfig.xml中加载db.properties的属性值,在SqlMapConfig.xml中就不需要对数据库连接

参数硬编码

//或使用spring整合配置  <context:property-placeholder location="classpath:db.properties"/>

//加载属性文件,          <properties resource="db.properties">

 //还可继续配置属性名和属性值       <property name="" value=""/>

                  </properties>   

在db.properties文件中取参数:如${jdbc.url}

2.settings(全局配置参数)

mybatis框架在运行时调整一些运行参数,

比如:开启二级缓存,开启延迟加载等

<settings>

</settings >

3.typeAliases(类型别名)重点

在mapper.xml中,定义很多的statement,statement需要parameterType指定输入参数的类型,需要resultType指定输出结果的映射类型

如果在指定类型时输入类型全路径,不方便进行开发,可以针对parameterType或者resultType指定的类型定义一些别名 ,在mapper.xml

中通过别名来定义,方便开发

<typeAliases>

  <!--针对单个别名定义, type:类型的路径, alias:别名-->

  <typeAlias type="com.hes.entity.analysisModel.HeIndex" alias="HeIndex">

  <!--批量别名定义  指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名  常用-->

  <package name="com.hes.entity.analysisModel">

</typeAliases>

 

4.typeHandlers(类型处理器)

  mybatis中通过typeHandlers完成jdbc类型和java类型的转换

5.objectFactory(对象工厂)

6.plugins(插件)

7.environments(环境集合属性对象)

  environment(环境子属性对象)

    transactionManager(事务管理)

    dataSource(数据源)

8.mappers(映射器)

<mappers>

//resource,url单个映射文件的加载      通过mapper接口来加载,使用mapper代理的方法用class 

  <!--通过resource方法一次加载一个映射文件-->

  <mapper resource="sqlmap/UserMapper.xml">

<!--批量加载  指定mapper接口的包名 mybatis自动扫描包下的所有mapper接口进行加载   前提是使用了mapper代理方法  推荐使用-->

<package name="com.hes.mapper">

</mappers>

 

//spring-mybatis整合

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hes.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

--------------------------------------------------------------------------------------------------------------------------------

输入映射

通过parameterType指定输入参数的类型,类型可以是简单类型,hashmap,pojo的包装类型

 

输出映射

1.resultType      输出pojo对象,输出pojo列表,输出简单类型(总共输出一行一列)

  使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。如果查询出来的列名和pojo中的属性名全部不一致,

没有创建pojo对象。只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象。

2.resultMap

//type:resultMap最终映射的java对象类型,可以使用别名   id:对resultMap的唯一标识

<resultMap id="BaseResultMap" type="com.mybatis.model.customer.Customer" >  

<!-- id标识查询结果集中唯一标识,column:查询出来的列名,property:type所指定的pojo类型中的属性名-->
<id column="id" property="id" jdbcType="INTEGER" />

<!--result:对普通名的映射定义-->
<result column="shopId" property="shopid" jdbcType="INTEGER" />
<result column="customerName" property="customername" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="INTEGER" />
<result column="tel" property="tel" jdbcType="VARCHAR" />
<result column="birthday" property="birthday" jdbcType="DATE" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="memo" property="memo" jdbcType="VARCHAR" />
<result column="groupId" property="groupid" jdbcType="INTEGER" />
<result column="registDate" property="registdate" jdbcType="TIMESTAMP" />
</resultMap>

resultMap="BaseResultMap"

 

动态sql

1.<if test="query!=null and query!='' ">

(customerName LIKE CONCAT(CONCAT('%',#{query}),'%') or
tel like
CONCAT(CONCAT('%',#{query}),'%'))
</if>

2.<where>where自动去掉条件中的第一个and</where>

 

sql片段    一般基于单表来定义sql片段   在sql片段中不要包括where

(1)<sql id="Base_Column_List" > //id片段的唯一标识
id, shopId, customerName, sex, tel, birthday, address, memo, groupId, registDate
</sql>

(2) <include refid="Base_Column_List" /></include>   //refid对应sql判断id

 

 

foreach 向sql传递list或数组

<foreach collection="ids" item="id" open="AND id IN(" close=")" separator="OR">

<!--  collection:指定输入对象中的集合属性

item:每个遍历生成对象

open:开始遍历时拼接的串

close:结束遍历时拼接的串

separator:遍历的两个对象中需要拼接的串

-->

</foreach>

 

posted @ 2017-01-14 14:11  一缕清风丶  阅读(283)  评论(0编辑  收藏  举报