在项目中有时候hibernate的使用不是那么的方便,程序员还是需要直接和sql打交道,这个时候ibatis的选择还是不错的。
如何在一个项目中配置ibatis呢,下面给出一个小的demo。
applicationContext.xml中配置如下:
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:/META-INF/ibatis/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
其中的dataSource表示数据源,这个根据不同的数据库进行不同的设置
SqlMapConfig.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis-with-memcached.googlecode.com/files/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="cache_config.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="256"
maxSessions="256"
maxTransactions="150"
useStatementNamespaces="true"
databaseUrl="user"
/>
<sqlMap resource="META-INF/ibatis/sqlmap/pj/pj.xml"/>
</sqlMapConfig>
pj.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="pj">
<select id="selectsqmx" resultClass="java.util.HashMap" parameterClass="java.lang.String">
select b.czrydmas czrydm from bsjd a,tab b where a.id=b.id
and a.id=#value#
</select>
</sqlMap>
配置好上述xml的文件之后,我们需要写相关的逻辑处理的过程了,如下所示:
DAO层:
PjManagerIbatis.java
package org.js.dao.pj;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class PjManagerIbatis extends SqlMapClientDaoSupport {
private String spacename="pj.";
public List queryForList(String ibatisname, Object object) {
List result = getSqlMapClientTemplate().queryForList(spacename + ibatisname, object);
return result;
}
public Object queryForObject(String ibatisname, Object object) {
Object result = getSqlMapClientTemplate().queryForObject(spacename + ibatisname, object);
return result;
}
public void saveObject(String ibatisname, Object object)
{
getSqlMapClientTemplate().insert(spacename + ibatisname, object);
}
//通用-根据参数更新
public void updateForObj(String ibatisname, Object object) {
getSqlMapClientTemplate().update(spacename + ibatisname, object);
}
//通用-根据参数取列表
public List getNList(String ibatisname, Object object) {
List result = getSqlMapClientTemplate().queryForList(spacename +ibatisname, object);
return result;
}
public List getList(String ibatisname, Object object) {
List result = getSqlMapClientTemplate().queryForList(spacename + ibatisname, object);
return result;
}
}
Services层:
package org.js.service.pj;
import org.js.domain.pj.Sqpjxx;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface PjService {
public List getSqmx(HashMap arg);
public String getTotal(String arg);
}
package org.js.service.pj.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("pjService")
@Transactional
public class pjServiceImpl implements PjService {
@Autowired
private PjManagerIbatis pjManagerIbatis;
public List getSqmx(HashMap arg) {
List res=null;
res=pjManagerIbatis.queryForList("selectsqmx",arg) ;
return res;
}
public String getTotal(String arg) {
String count=null;
count=(String) pjManagerIbatis.queryForObject("selectcount",arg);
return count;
}
}
Controller层:
package org.js.web.pj;
import org.js.domain.pj.Sqpjxx;
import org.js.service.pj.pjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping("/pj/**")
@Controller
public class pjController {
@Autowired
PjService pjService;
@RequestMapping(value = "/pj/wspjmain", method = RequestMethod.GET)
public String getSqmxFirstPage(ModelMap modelmap,
HttpServletRequest request, HttpServletResponse response) {
Map page=new HashMap();
List rt = new ArrayList();
String count=(String)pjService.getTotal(request.getattribute("arg"));
if(!count.equals("0"))
{rt=pjService.getSqmx((HashMap) page);}
modelmap.addAttribute("pageMap", pageMap);
modelmap.addAttribute("rtlist", rt);
return "pj/wspjmain";
} else {
return null;
}
}
}