博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring简单整合ibatis
阅读量:7225 次
发布时间:2019-06-29

本文共 4395 字,大约阅读时间需要 14 分钟。

  hot3.png

     在项目中有时候hibernate的使用不是那么的方便,程序员还是需要直接和sql打交道,这个时候ibatis的选择还是不错的。

如何在一个项目中配置ibatis呢,下面给出一个小的demo。

applicationContext.xml中配置如下:

    
        
        
    

其中的dataSource表示数据源,这个根据不同的数据库进行不同的设置

SqlMapConfig.xml配置如下:

    
    
    

   

     pj.xml的配置如下:

 
      
            select b.czrydmas czrydm from bsjd a,tab b where a.id=b.id                    and a.id=#value#      

配置好上述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;        }    }}

转载于:https://my.oschina.net/guanhe/blog/657414

你可能感兴趣的文章
POJ 1039 Pipe(直线和线段相交判断,求交点)
查看>>
Java-抽象类定义构造方法
查看>>
一键安装IIS的点点滴滴——For所有Microsoft的操作系统(上)
查看>>
Android 短信模块分析(二) MMS中四大组件核心功能详解
查看>>
Eclipse 工程使用相对路径导入Jar包设置
查看>>
Struts2中的 配置文件
查看>>
手动安装 MyEclipse6.5 FindBugs
查看>>
poj 3615(floyd变形)
查看>>
缓存子系统如何设计(Cachable tag, Memcache/redis support, xml config support, LRU/LFU/本地缓存命中率)...
查看>>
解决数据库 Table 'content_tags' is marked as crashed and should be repaired 表损坏问题
查看>>
算法-随手写的二分查找
查看>>
测量史上首个易语言工程测量模块
查看>>
面向对象初步总结
查看>>
分享45个设计师应该见到的新鲜的Web移动设备用户界面PSD套件
查看>>
SDL_BlitSurface
查看>>
Ubuntu12.04编译Android2.3.4
查看>>
IDA设置函数类型
查看>>
日期控件ie9失效
查看>>
群里一个高手写的url?传参执行php函数的小程序, 收藏下
查看>>
Linux桌面扩展 Docky
查看>>