ibatis/Mybatis实战分享与讨论

[i]以前有类似讨论,互联网项目要用spring jdbc 或者ibatis 不然会有种种问题,神乎其神,到底是为啥呢?hibernate到底有啥不好,做互联网项目还是先看看ibatis是个什么东东吧[/i]
[b]关于ibatis入门如何?[/b]
入门相对容易,好像没什么有他官方上那本pdf更好的,而且有中文版,看起来速度刷刷的

[b]ibatis主要提供了什么?[/b]
数据库字段到model映射,这个通过强大的xml配置解决,可以做出类似hibernate的效果,但是sql语句是你自己书写,也没有面向对象建立数据库的压力,又比spring jdbc之类强大

[b]模板配置sql到底多强大?[/b]
可以动态配置sql,可以使用ognl表达式,你的sql用广告词,叫做会跳舞的sql,以前看帖子,有哥们说他们项目自己该造,用freemarker搭建了一个模板,很强大,到底有那些好处,具体不太了解,我觉得现在的绝对够用,就是配置有些繁琐
比如我们ognl严重
<where>
<if test="@Ognl@isNotEmpty(clickId)">
AND click_id = #{clickId}
</if>
</where>

public class Ognl {

/**
* 可以用于判断String,Map,Collection,Array是否为空
* @param o
* @return
*/
public static boolean isEmpty(Object o) throws IllegalArgumentException {
if(o == null) return true;

if(o instanceof String) {
if(((String)o).length() == 0){
return true;
}
} else if(o instanceof Collection) {
if(((Collection)o).isEmpty()){
return true;
}
} else if(o.getClass().isArray()) {
if(Array.getLength(o) == 0){
return true;
}
} else if(o instanceof Map) {
if(((Map)o).isEmpty()){
return true;
}
}else {
return false;
// throw new IllegalArgumentException("Illegal argument type,must be : Map,Collection,Array,String. but was:"+o.getClass());
}

return false;
}

/**
* 可以用于判断 Map,Collection,String,Array是否不为空
* @param c
* @return
*/
public static boolean isNotEmpty(Object o) {
return !isEmpty(o);
}

public static boolean isNotBlank(Object o) {
return !isBlank(o);
}
...略


[b]ibatis这些点怎么搭建一个良好快速的开发环境呢?(也就是对ibatis提供的特性进行了强力的封装)[/b]
[b]1.代码生成器需要统一的几个点[/b]
利用代码生成器,我们每张表的常规操作,CRUD于基于简单字段查询条件的分页都自动生成映射到model上了
统一了几个点:namespace=table.xxx xxx代表具体操作,每张表生成适合都是统一的
也自动生成了action--service--dao--xml
[b]2.底层统一接口调用的问题[/b]
Dao层继承BaseIbatisDao,上面统一的几个点造成了一些Crud于有条件的分页可以放到顶层的Dao中
不可能每次调用都要打开sqlsession 关闭sqlsession
那么模板方法模式封装好这个流程,然后每次调用就可以了,每次直接调用模板sqlsessionTemplete
类似模板方法:

public Object execute(SqlSessionCallback action) {
SqlSession session = null;
try {
session = sqlSessionFactory.openSession();
Object result = action.doInSession(session);
return result;
}finally {
if(session != null) session.close();
}
}

public Object executeBatch(SqlSessionCallback action) {
SqlSession session = null;
try {
session = sqlSessionFactory.openSession(false);
Object result = action.doInSession(session);
return result;
}finally {
if(session != null) {
session.commit(true);
session.close();
}
}
}


[b]3.分页问题解决 [/b]
这个没想象那么难,见下面模板跟代码
    <select id="AdClickinfo.findPage" resultMap="RM.AdClickinfo">
SELECT <include refid="AdClickinfo.columns" />
FROM ad_clickinfo
<include refid="AdClickinfo.findPage.where"/>

<if test="@Ognl@isNotEmpty(sortColumns)">
ORDER BY ${sortColumns}
</if>
</select>
<sql id="AdClickinfo.findPage.where">
<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用,具体请查看Ognl类 -->
<where>
<if test="@Ognl@isNotEmpty(clickId)">
AND click_id = #{clickId}
</if>
<if test="@Ognl@isNotEmpty(adId)">
AND ad_id = #{adId}
</if>
<if test="@Ognl@isNotEmpty(clickDatetimeBegin)">
AND click_datetime >= #{clickDatetimeBegin}
</if>
<if test="@Ognl@isNotEmpty(clickDatetimeEnd)">
AND click_datetime <= #{clickDatetimeEnd}
</if>
<if test="@Ognl@isNotEmpty(clickIp)">
AND click_ip = #{clickIp}
</if>
<if test="@Ognl@isNotEmpty(clickFrom)">
AND click_from = #{clickFrom}
</if>
</where>
</sql>


public static Page pageQuery(SqlSessionTemplate sqlSessionTemplate,String statementName,String countStatementName, PageRequest pageRequest) {

Number totalCount = (Number) sqlSessionTemplate.selectOne(countStatementName,pageRequest);
if(totalCount == null || totalCount.longValue() <= 0) {
return new Page(pageRequest,0);
}

Page page = new Page(pageRequest,totalCount.intValue());

Map filters = new HashMap();
filters.put("offset", page.getFirstResult());
filters.put("pageSize", page.getPageSize());
filters.put("lastRows", page.getFirstResult() + page.getPageSize());
filters.put("sortColumns", pageRequest.getSortColumns());

Map parameterObject = PropertyUtils.describe(pageRequest);
filters.putAll(parameterObject);

List list = sqlSessionTemplate.selectList(statementName, filters,page.getFirstResult(),page.getPageSize());
page.setResult(list);
return page;
}


public class Page<T> implements Serializable,Iterable<T>
{

protected List<T> result;

protected int pageSize;

protected int pageNumber;
/* added by xiajun 新增设置当前页数方法 2010-11-02*/
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}

protected int totalCount = 0;

public Page(PageRequest p, int totalCount) {
this(p.getPageNumber(),p.getPageSize(),totalCount);
}

public Page(int pageNumber,int pageSize,int totalCount) {
this(pageNumber,pageSize,totalCount,new ArrayList(0));
}

public Page(int pageNumber,int pageSize,int totalCount,List<T> result) {
if(pageSize <= 0) throw new IllegalArgumentException("[pageSize] must great than zero");
this.pageSize = pageSize;
this.pageNumber = PageUtils.computePageNumber(pageNumber,pageSize,totalCount);
this.totalCount = totalCount;
setResult(result);
}




[b]实际使用遇到的问题
1.关于灵活性的问题[/b]
你需要model中的几个属性值,你是调用自动生成的代码取得整个model,还是自己写一个,如果这个查询发生次数多,必须自己写
[b]2.关于配置复杂的问题[/b]
看下官方文档高级映射,就知道这个难度不大,写着麻烦,配置很繁琐,为了一个特殊的查询是否有必要取那样做,前后端都hashmap,跨过model映射这个,如果要自己写,你的model要符合,我们的model不满足,全部直接都hashmap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值