代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
封装的基类Springjdbc
作者:
刘石法
/ 发布于
2012/10/22
/
656
一个通用的BaseDao,简化开发。包括增删查改以及分页
<div>package oa.common.dao;</div> <div></div> import java.util.List;</div> <div></div> import oa.common.model.PageModel;</div> <div></div> import org.springframework.jdbc.core.BeanPropertyRowMapper;</div> import org.springframework.jdbc.core.RowMapper;</div> import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;</div> import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;</div> import org.springframework.jdbc.core.namedparam.SqlParameterSource;</div> <div></div> <div>public abstract class BaseDAO<T> extends NamedParameterJdbcDaoSupport {</div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected final static String PAGE_SQL_PREFIX = "select * from(select m.*,rownum num from (";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected final static String PAGE_SQL_END = ") m where rownum<=?) where num>?";</div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>/**</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * 适用于更新数据库,insert,update, delete</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> *</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param namedSql</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * :命名参数的SQL语句,而且参数的命名必须和JavaBean中的属性名对应</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param javaBean</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * :javabean对象</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @return</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> */</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected int update(String namedSql, Object javaBean) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>SqlParameterSource paramSource = new BeanPropertySqlParameterSource(</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>javaBean);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getNamedParameterJdbcTemplate().update(namedSql,paramSource);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected int commonUpdate(String sql, Object... paramValue) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate().update(sql, paramValue);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected T getJavaBean(String sql, Class<T> returnType,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Object... paramValue) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>try{</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>.queryForObject(sql, rowMapper, paramValue);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>}catch(Exception ex){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return null;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected List<T> getList(String sql, Class<T> returnType,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Object... paramValue) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate().query(sql, rowMapper, paramValue);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected List<T> getList(String sql, Class<T> returnType) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate().query(sql, rowMapper);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>/**</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * 计算总记录数</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> *</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param countSQL</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * 计算总记录数的count语句</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @param paramValue</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * 语句中对应的参数值</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> * @return 总记录数</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span> */</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected int getCount(String countSQL, List paramValue) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate().queryForInt(countSQL,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>paramValue.toArray());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected int getCount(String countSQL, Object... paramValue) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this.getJdbcTemplate().queryForInt(countSQL, paramValue);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>protected PageModel getPageModel(PageModel model,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>StringBuilder querySQL, StringBuilder countSQL,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>StringBuilder whereSQL, List paramList, Class<T> returnType) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>querySQL.append(whereSQL);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>countSQL.append(whereSQL);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>// 计算总记录数</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>int allCount = this.getCount(countSQL.toString(), paramList);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>// 获取分页记录集</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>// 1。构造完整的分页语句</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>querySQL.insert(0, PAGE_SQL_PREFIX);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>querySQL.append(PAGE_SQL_END);</div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>// 2.把分页语句中的参数值加入到paramList中</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>paramList.add(model.getNumPerPage()* model.getPageNum());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>paramList.add(( model.getPageNum() - 1) *model.getNumPerPage());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>List result = this.getList(querySQL.toString(), returnType,</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>paramList.toArray());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>PageModel models = new PageModel();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setTotalCount(allCount);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setNumPerPage(model.getNumPerPage());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setPageNum(model.getPageNum());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setResult(result);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setOrderDirection(model.getOrderDirection());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>models.setOrderField(model.getOrderField());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return models;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div>} <div></div>
试试其它关键字
Springjdbc
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
刘石法
贡献的其它代码
(
3
)
.
封装的基类Springjdbc
.
导出Excel通用方法
.
汉字转拼音以及得到首字母通用方法
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3