1.SqlMapConfig.xml
在前面已经简单的使用过SqlMapConfig.xml中的mapper ,以下将介绍几个标签 :
首先SqlMapConfig是 mybatis的配置文件:
(1)properties 属性
将数据库连接参数单独的配置在db.properties中,主需要在SqlMapConfig.xml中加载db.properties的属性值,在SqlMapConfig.xml中不需要配置对数据库连接参数的硬编码。
原因是:方便对参数进行统一的管理,其他xml可以加载引用该db.properties
比如 :
db.properties
oracle.driver=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:xe
oracle.name=hr
oracle.pass=yuan
properties属性 :
<properties resource="db.properties"></properties>
使用 :
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${oracle.driver}" />
<property name="url" value="${oracle.url}" />
<property name="username" value="${oracle.name}" />
<property name="password" value="${oracle.pass}" />
<!-- <property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="hr" />
<property name="password" value="yuan" /> -->
</dataSource>
</environment>
</environments>
注意:MyBatis将按照下面顺序来加载属性:
1) 在properteis元素体内定义的属性首先被读取;
2) 然后会读取properties元素中resource或url加载的属性,它会覆盖已读取的同名属性;
3) 最后读取parameerType传递的属性,它会覆盖已读取的同名属性;
因此,通过paramterType传递的属性具有最高优先级,resource或url加载的属性次之,最低优先级的是properties元素体内定义的属性;
建议:不要在properties元素体内添加任何属性值,只将属性值定义在properties文件中,在properties中文件定义属性名一定的特殊性,如:xxx.xxxx
(2) setting 全局参数配置
mybatis框架在运行时可以调整运行参数,需要得时候设置,不需要的时候不要乱设置;
使用 :
<settings><settings>
(3)typeAliases 别名
在mapper.xml中,定义了很多的statement,statement需要parameterType制定的输入参数类型,和resultType制定的输出结果映射类型;
如果在指定类型时输入类型全路径,不方便开发,可以针对paramterType或resultType指定的类型定义一些别名,在mapper.xml中通别名定义,方便开发;
使用:
<typeAliases>
单挑定义
<typeAlias type="包名" alias=""/>
多条定义,别名就是类名
<package name="包名"/>
</typeAliases>
示列 :
<typeAliases>
<typeAlias type="cn.labelnet.pojo.Client" alias="client"/>
<typeAlias type="cn.labelnet.pojo.Operation" alias="operation"/>
</typeAliases>
(4)typeHandlers 类型处理器
mybatis中通过typeHandlers完成jdbc类型和java类型转换;通常情况下,mybatis提供的类型处理器满足日常需要,不需要自定义;
5)mappers (映射器)
1)通过resource加载 : <mapper resource=" " />
2)通过mapper接口加载映射文件 :<mapper class=""/> ,规范:需要mapper接口类名和mapper.xml
映射文件名称保持一致,且在一个目录中,使用的是mapper代理方向;
3)通过package 批量加载mapper ,指定mapper接口包名,mybatis自动扫描包下的所有mapper接口进行加载;
<mappers>
<package name="包名"/>
<!-- mapper.xml -->
<mapper resource="mapper/ClientMapper.xml"/>
<mapper resource="mapper/OperationMapper.xml"/>
</mappers>
2.输入映射 (注意 : Mapper.xml文件 的statement中sql语句不准出现 分号 ;)
通过paramteType指定的输入参数类型,类型可以是简单类型,hashmap,pojo包装类型;(1)传递pojo的包装对象
完成综合查询(关联查询)的时候,建议使用自定义的包装类型pojo ,将复杂的查询条件包装为pojo类型;
(2)实例 :多条件查询实现
1)Mapper.xml实现 :
<select id="selectOperationIfos" parameterType="cn.labelnet.pojo.OperationQueryVo1" resultType="cn.labelnet.pojo.OperationCustom">
select * from f_operation where f_operation.client_id=#{c.id} and f_operation.legal_person_name='${name}'
</select>
2)接口实现
//多条件查询
public OperationCustom selectOperationIfos(OperationQueryVo1 oa);
3)包装类实现
/**
* 自定义包装类型,查询条件
* 描述: TODO
* 作者 :原明卓
* 时间 :2015年12月22日 下午3:02:20
* 版本 :1.0
*/
public class OperationQueryVO {
//经营信息的条件
private OperationCustom operation;
public Operation getOperation() {
return operation;
}
public void setOperation(OperationCustom operation) {
this.operation = operation;
}
}
4)经营信息 operation pojo实现
package cn.labelnet.pojo;
import java.util.Date;
/**
* 经营信息
*
* 作者 :原明卓
* 时间 :2015年12月22日 上午9:22:57
* 包名 :cn.labelnet.pojo
* 描述 :TODO
*/
public class Operation {
//资产编号
private Integer id;
//用户编号
private Integer client_id;
//资产信息描述
private String operate_infomation_describe;
//持股比例
private Integer possess_sharholding;
//经营地址
private String operate_address;
//年收入
private String year_avg_income;
//开始经营日期
private Date begin_operation_time;
//员工总数
private Integer employees_amount;
//法人姓名
private String legal_person_name;
//法人证件号码
private String legal_person_crd_number;
//经营证件号码
private String operate_crd_number;
//更新时间
private Date update_time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getClient_id() {
return client_id;
}
public void setClient_id(Integer client_id) {
this.client_id = client_id;
}
public String getOperate_infomation_describe() {
return operate_infomation_describe;
}
public void setOperate_infomation_describe(String operate_infomation_describe) {
this.operate_infomation_describe = operate_infomation_describe;
}
public Integer getPossess_sharholding() {
return possess_sharholding;
}
public void setPossess_sharholding(Integer possess_sharholding) {
this.possess_sharholding = possess_sharholding;
}
public String getOperate_address() {
return operate_address;
}
public void setOperate_address(String operate_address) {
this.operate_address = operate_address;
}
public String getYear_avg_income() {
return year_avg_income;
}
public void setYear_avg_income(String year_avg_income) {
this.year_avg_income = year_avg_income;
}
public Date getBegin_operation_time() {
return begin_operation_time;
}
public void setBegin_operation_time(Date begin_operation_time) {
this.begin_operation_time = begin_operation_time;
}
public Integer getEmployees_amount() {
return employees_amount;
}
public void setEmployees_amount(Integer employees_amount) {
this.employees_amount = employees_amount;
}
public String getLegal_person_name() {
return legal_person_name;
}
public void setLegal_person_name(String legal_person_name) {
this.legal_person_name = legal_person_name;
}
public String getLegal_person_crd_number() {
return legal_person_crd_number;
}
public void setLegal_person_crd_number(String legal_person_crd_number) {
this.legal_person_crd_number = legal_person_crd_number;
}
public String getOperate_crd_number() {
return operate_crd_number;
}
public void setOperate_crd_number(String operate_crd_number) {
this.operate_crd_number = operate_crd_number;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
@Override
public String toString() {
return "Operation [id=" + id + ", client_id=" + client_id
+ ", operate_infomation_describe="
+ operate_infomation_describe + ", possess_sharholding="
+ possess_sharholding + ", operate_address=" + operate_address
+ ", year_avg_income=" + year_avg_income
+ ", begin_operation_time=" + begin_operation_time
+ ", employees_amount=" + employees_amount
+ ", legal_person_name=" + legal_person_name
+ ", legal_person_crd_number=" + legal_person_crd_number
+ ", operate_crd_number=" + operate_crd_number
+ ", update_time=" + update_time + "]";
}
}
/**
* 经营信息拓展类
* 描述: TODO
* 作者 :原明卓
* 时间 :2015年12月22日 下午3:01:17
* 版本 :1.0
*/
public class OperationCustom extends Operation{
//拓展经营信息pojo
//联系方式
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String contact_mode;
public String getContact_mode() {
return contact_mode;
}
public void setContact_mode(String contact_mode) {
this.contact_mode = contact_mode;
}
public String getUrgency_contact_mode() {
return urgency_contact_mode;
}
public void setUrgency_contact_mode(String urgency_contact_mode) {
this.urgency_contact_mode = urgency_contact_mode;
}
//紧急联系方式
private String urgency_contact_mode;
}
6)测试
@Test
public void testselectOperationIfos(){
SqlSession os = sqlSessionFactory.openSession();
OperationMapper om = os.getMapper(OperationMapper.class);
OperationQueryVo1 oa = new OperationQueryVo1();
oa.setName("吴迪");
Client client=new Client();
client.setId(14);
oa.setC(client);
OperationCustom oc = om.selectOperationIfos(oa);
System.out.println(oc);
}
输入映射 ,主要使用了包装类,有时候查询条件多个参数,需要使用一个包装类,作为输入映射对象;
3.输出映射
(1)resultType1)使用resultType进行映射,只有查询出来的列表和pojo中的属性名一致,该列才可以映射成功。
2)如果查询出来的列表和pojo中的属性名全部不一致,没有创建pojo对象;
3)只有查询出来的列名和属性名一个一致,就会创建pojo对象;
4)查询输出的结果集只有一行且一列,可以使用简单类型进行输出映射;
5)不管是输出的pojo单个对象还是一个列表(list中包括pojo),在mapper.xml中resslType指定的类型时一致的。
生成的动态代理对象中的根据 比如 :pojo : Fclient ,进行扩展类 FclientCustom;
( 2)resultMap
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultmap定义列名与属性名之间的映射;
1)定义resultmap
<!-- 定义resultMap : type:resultMap最终 映射的java对象类型,可以使用别名、
id :对resultMap的 唯一标识 -->
<resultMap type="operation" id="openatoinResultMap">
<!-- column :查询出来的列名
property : type指定的pojo类型的属性名
最终resultMap对column和property作一个映射关系(对应关系) -->
<!-- 唯一列的标识 -->
<id column="id_" property="id"/>
<!-- 对普通列的标识 -->
<result column="client_id_" property="client_id"/>
</resultMap>
2)使用resultmap作为statement的输出映射;
<!-- 测试Reusultmap ,如果resultmap在其他mapper,则加上 mapper的命名空间 -->
<select id="selectOperationIfosResultMap" parameterType="cn.labelnet.pojo.OperationQueryVo1" resultMap="openatoinResultMap">
select id id_ , client_id client_id_ from f_operation where f_operation.legal_person_name='${name}'
</select>
综合查询 :
比如进行下面查询 :
select id id_ ,client_id client_id_ from f_operation where f_operation.legal_person_name='吴迪';
1)实现 接口
//多条件查询
public Operation selectOperationIfosResultMap(OperationQueryVo1 oa);
3)测试
@Test
public void testselectOperationIfosResultmap(){
SqlSession os = sqlSessionFactory.openSession();
OperationMapper om = os.getMapper(OperationMapper.class);
OperationQueryVo1 oa = new OperationQueryVo1();
oa.setName("吴迪");
Client client=new Client();
client.setId(14);
oa.setC(client);
Operation oc = om.selectOperationIfosResultMap(oa);
System.out.println(oc);
}
4.动态Sql
之间在statement中实现 :mybatis核心 对sql语句进行灵活的操作,通过表达式进行判断,对sql进行灵活的拼接,组装;
综合查询,对查询条件进行判断,后进行拼接:
<select>
select * from user
<where>
<if test="user!=null">
<if test="user.id!=null and user.id!=''">
and user.id=#{user.id}
</if>
<if test="user.name!=null and user.name!=''">
and user.name=#{user.name}
</if>
....
</if>
</where>
</select>
5.Sql片段
定义Sql片段 ,可以重复使用条件,将上面的动态sql判断代码块抽取出来,组成一个sql片段,为了可重用性;id为sql片段的唯一标示,单表定义; 不要包含where ,比如下面的mapper文件 :
<mapper>
<sql id="query_where">
<if test="user!=null">
<if test="user.id!=null and user.id!=''">
and user.id=#{user.id}
</if>
<if test="user.name!=null and user.name!=''">
and user.name=#{user.name}
</if>
....
</if>
</sql>
<select>
select * from user
<where>
<include refid="query_where" />
...还可以引用其他的sql片段
</where>
</select>
</mapper>
6.foreach
向sql传递属性或list , mybatis使用foreach解析;传入多个id 时,比如 : where id=1 or id=1 or id=2 或 id in(1,20,23) <sql id="query_where">
<if test="user!=null">
<if test="user.id!=null and user.id!=''">
and user.id=#{user.id}
</if>
<if test="user.name!=null and user.name!=''">
and user.name=#{user.name}
</if>
foreach 遍历传入集合ids
sql条件 : and (id=1 or id=10 or id=12)
<foreach collection="ids" item="item_id" open="and {" close="}" separator="or">
拼接sql 语句 :and (id=1 or id=10 or id=12)
id=#{item_id}
</foreach>
....
</if>
</sql>
7.Demo免积分下载
http://download.csdn.net/detail/lablenet/9372461