在项目中有时候hibernate的使用不是那么的方便,程序员还是需要直接和sql打交道,这个时候ibatis的选择还是不错的。
如何在一个项目中配置ibatis呢,下面给出一个小的demo。
applicationContext.xml中配置如下:
其中的dataSource表示数据源,这个根据不同的数据库进行不同的设置
SqlMapConfig.xml配置如下:
pj.xml的配置如下:
配置好上述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;@Repositorypublic 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")@Transactionalpublic 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/**")@Controllerpublic 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; } }}