本系列博客内容为:做DRP系统中Dao层常用功能。

该项目采用MVC架构

  1. C(Controller)控制器,主要职责;1、取得表单参数;2、调用业务逻辑;3、转向页面
  2. M(Model)模型,主要职责:1、业务逻辑;2、保存数据的状态
  3. V(View)视图,主要职责:显示

本文主要是针对于Dao层的常见使用方法:增删改查sql语句及常用操作。

 package com.bjpowernode.drp.basedata.dao;

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.bjpowernode.drp.basedata.domain.Item;
import com.bjpowernode.drp.util.ApplicationException;
import com.bjpowernode.drp.util.DbUtil;
import com.bjpowernode.drp.util.PageModel;
import com.bjpowernode.drp.util.datadict.domain.ItemCategory;
import com.bjpowernode.drp.util.datadict.domain.ItemUnit; public class ItemDao4OracleImpl implements ItemDao {
//增加物料信息
public void addItem(Connection conn, Item item) {
String sql = "insert into t_items (item_no, item_name, spec, pattern, item_category_id, item_unit_id) ";
sql+=" values (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = null;
try {
//Dao的设计粒度一般是细粒度的,如果没有特殊需求,Dao和Manager粒度可以一样,不同考虑太多
//Dao的设计是比较单纯的,不应该放入过多的业务逻辑(业务规则)
//如果放置了业务逻辑,有些Manager不采用此业务逻辑,这样这个Dao方法就没有复用率了
//对于我们的应用来说Dao最底层的,所以应该越通用越好
// if (findItemById(conn, item.getItemNo()) != null) {
// throw new ApplicationException("物料代码已经存在,代码=" + item.getItemNo() + "");
// }
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, item.getItemNo());
pstmt.setString(2, item.getItemName());
pstmt.setString(3, item.getSpec());
pstmt.setString(4, item.getPattern());
pstmt.setString(5, item.getItemCategory().getId());
pstmt.setString(6, item.getItemUnit().getId());
pstmt.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
//System.out.println("errorCode=" + e.getErrorCode());
//System.out.println("description=" + e.getMessage());
// if (e.getErrorCode() == 1) {
// throw new ApplicationException("物料代码已经存在,代码【" + item.getItemNo() + "】");
// }
throw new ApplicationException("添加物料失败!");
}finally {
DbUtil.close(pstmt);
}
}
//删除物料信息数组
public void delItem(Connection conn, String[] itemNos) {
StringBuffer sbString = new StringBuffer();
for (int i=0; i<itemNos.length; i++) {
sbString.append("?");
if (i < (itemNos.length - 1)) {
sbString.append(",");
}
}
String sql = "delete from t_items where item_no in(" + sbString.toString() + ")";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i=0; i<itemNos.length; i++) {
pstmt.setString(i+1, itemNos[i]);
}
pstmt.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
throw new ApplicationException("删除物料失败!");
}finally {
DbUtil.close(pstmt);
}
}
//通过Id查询物料信息
public Item findItemById(Connection conn, String itemNo) {
StringBuffer sbSql = new StringBuffer();
//第一中方法
sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.item_category_id, ")
.append("b.name as item_category_name, a.item_unit_id, c.name as item_unit_name ")
.append("from t_items a, t_data_dict b, t_data_dict c ")
.append("where a.item_category_id=b.id and a.item_unit_id=c.id and a.item_no=?"); // //第二中方法
// sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.category as category_id, ")
// .append("(select b.name from t_data_dict b where a.category=b.id) as category_name, ")
// .append("a.unit as unit_id, ")
// .append("(select c.name from t_data_dict c where a.unit=c.id) as unit_name ")
// .append("from t_items a where a.item_no=?"); //通常采用日志组件记录,如log4j, 级别:info,debug,error... PreparedStatement pstmt = null;
ResultSet rs = null;
Item item = null;
try {
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setString(1, itemNo);
rs = pstmt.executeQuery();
if (rs.next()) {
item = new Item();
item.setItemNo(rs.getString("item_no"));
item.setItemName(rs.getString("item_name"));
item.setSpec(rs.getString("spec"));
item.setPattern(rs.getString("pattern"));
//构造ItemCategory
ItemCategory ic = new ItemCategory();
ic.setId(rs.getString("item_category_id"));
ic.setName(rs.getString("item_category_name"));
item.setItemCategory(ic); //构造ItemUnit
ItemUnit iu = new ItemUnit();
iu.setId(rs.getString("item_unit_id"));
iu.setName(rs.getString("item_unit_name"));
item.setItemUnit(iu);
}
}catch(SQLException e) {
e.printStackTrace();
//记录到日志文件 error
throw new ApplicationException("根据物料代码查询出错,物料代码[" + itemNo + "]");
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return item;
}
//根据pageNO、pageSize、condation显示很多物料信息列表
public PageModel findItemList(Connection conn, int pageNo, int pageSize, String condation) {
StringBuffer sbSql = new StringBuffer(); //第一中方法
// sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.item_category_id, ")
// .append("b.name as item_category_name, a.item_unit_id, c.name as item_unit_name ")
// .append("from t_items a, t_data_dict b, t_data_dict c ")
// .append("where a.item_category_id=b.id and a.item_unit_id=c.id and a.item_no=?"); // //第二中方法
// sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.category as category_id, ")
// .append("(select b.name from t_data_dict b where a.category=b.id) as category_name, ")
// .append("a.unit as unit_id, ")
// .append("(select c.name from t_data_dict c where a.unit=c.id) as unit_name ")
// .append("from t_items a where a.item_no=?"); sbSql.append("select * ")
.append("from (")
.append("select i.*, rownum rn from (")
.append("select a.item_no, a.item_name, a.spec, a.pattern, a.item_category_id, ")
.append("b.name as item_category_name, a.item_unit_id, c.name as item_unit_name ")
.append("from t_items a, t_data_dict b, t_data_dict c ")
.append("where a.item_category_id=b.id and a.item_unit_id=c.id ");
if (condation != null && !"".equals(condation)) {
sbSql.append(" and (a.item_no like '" + condation + "%' or a.item_name like '" + condation + "%') ");
}
sbSql.append(" order by a.item_no")
.append(") i where rownum<=? ")
.append(") ")
.append("where rn >? ");
System.out.println("sql=" + sbSql.toString()); //通常采用日志组件记录,如log4j, 级别:info,debug,error...
PreparedStatement pstmt = null;
ResultSet rs = null;
PageModel pageModel = null;
try {
pstmt = conn.prepareStatement(sbSql.toString());
pstmt.setInt(1, pageNo * pageSize);
pstmt.setInt(2, (pageNo - 1) * pageSize);
rs = pstmt.executeQuery();
List itemList = new ArrayList();
while (rs.next()) {
Item item = new Item();
item.setItemNo(rs.getString("item_no"));
item.setItemName(rs.getString("item_name"));
item.setSpec(rs.getString("spec"));
item.setPattern(rs.getString("pattern"));
//构造ItemCategory
ItemCategory ic = new ItemCategory();
ic.setId(rs.getString("item_category_id"));
ic.setName(rs.getString("item_category_name"));
item.setItemCategory(ic); //构造ItemUnit
ItemUnit iu = new ItemUnit();
iu.setId(rs.getString("item_unit_id"));
iu.setName(rs.getString("item_unit_name"));
item.setItemUnit(iu); itemList.add(item);
}
pageModel = new PageModel();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setList(itemList);
//根据条件取得记录数
int totalRecords = getTotalRecords(conn, condation);
pageModel.setTotalRecords(totalRecords);
}catch(SQLException e) {
e.printStackTrace();
//记录到日志文件 error
throw new ApplicationException("分页查询失败");
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return pageModel;
} /**
* 根据条件取得记录数
* @param conn
* @param queryStr
* @return
*/
private int getTotalRecords(Connection conn, String condation)
throws SQLException {
String sql = "select count(*) from t_items ";
if (condation != null && !"".equals(condation)) {
sql+="where item_no like '" + condation + "%' or item_name like '" + condation + "%' ";
}
PreparedStatement pstmt = null;
ResultSet rs = null;
int temp = 0;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
temp = rs.getInt(1);
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return temp;
}
//修改物料信息
public void modifyItem(Connection conn, Item item) {
String sql = "update t_items set item_name=?, spec=?, pattern=?, item_category_id=?, item_unit_id=? ";
sql+=" where item_no=?";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, item.getItemName());
pstmt.setString(2, item.getSpec());
pstmt.setString(3, item.getPattern());
pstmt.setString(4, item.getItemCategory().getId());
pstmt.setString(5, item.getItemUnit().getId());
pstmt.setString(6, item.getItemNo());
pstmt.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
throw new ApplicationException("修改物料失败!");
}finally {
DbUtil.close(pstmt);
}
} }

【DRP】-Dao层常用功能代码:增删改查的更多相关文章

  1. DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类) 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据 ...

  2. zkCli的使用 常用的节点增删改查命令用法

    zkCli的使用 常用的节点增删改查命令用法 1. 建立会话  命令格式:zkCli.sh -timeout 0 -r -server ip:port ./zkCli.sh -server -time ...

  3. MYSQL的常用命令和增删改查语句和数据类型

    连接命令:<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库 ...

  4. MYSQL的常用命令和增删改查语句和数据类型【转】

    连接命令:<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库 ...

  5. .NET Core实战项目之CMS 第十五章 各层联动工作实现增删改查业务

    连着两天更新叙述性的文章大家可别以为我转行了!哈哈!今天就继续讲讲我们的.NET Core实战项目之CMS系统的教程吧!这个系列教程拖得太久了,所以今天我就以菜单部分的增删改查为例来讲述下我的项目分层 ...

  6. SQL学习之MYSQL的常用命令和增删改查语句和数据类型

    连接命令:mysql -h[主机地址] -u[用户名] -p[用户密码] 创建数据库:create database [库名] 显示所有数据库: show databases; 打开数据库:use [ ...

  7. MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. JS源生代码“增删改查”之增

    51呢最近在做一个管理数据的,第一次接触到用JS的源代码去实现一些功能,才知道网页里的许多功能都是依赖于“增删改查”完成的,下面的几张图片就是对于增的演示: 下面是有关HTML的代码:这个主要是弹窗部 ...

  9. HBase入门操作 常用命令和增删改查的简单应用操作

    这里启动关闭Hadoop和HBase的顺序一定是: 启动Hadoop—>启动HBase—>关闭HBase—>关闭Hadoop ssh localhost 开启hadoopcd /us ...

随机推荐

  1. tolua#是Unity静态绑定lua的一个解决方案

    tolua#代码简要分析 2017-04-16 23:02 by 风恋残雪, 98 阅读, 1 评论, 收藏, 编辑 简介 tolua#是Unity静态绑定lua的一个解决方案,它通过C#提供的反射信 ...

  2. 【19.05%】【codeforces 731F】 Video Cards

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. C# Color结构的各属性颜色对照表(转)

    转自:http://blog.sina.com.cn/s/blog_454dc49501016q2p.html Color.AliceBlue 240,248,255 Color.LightSalmo ...

  4. Logistic Regression 的简单推导

    Logistic Regression 是一种 Generalized Linear Model(GLM),也即广义线性模型. 1. LR 的基本假设 LR 模型假设观测值 y 成立的对数几率(log ...

  5. Mybatis使用TypeHandler实现数据的加解密转换

    参考: MyBatis之TypeHandler: https://www.cnblogs.com/yulinfeng/p/5991170.html   前段时间收到这么个需求:为安全起见,要求在数据库 ...

  6. Android 悬浮窗、悬浮球开发

    原文:Android 悬浮窗.悬浮球开发 1.权限管理 直接看我另外一篇博客吧,传送门: https://my.oschina.net/u/1462828/blog/1933162 2.Base类Ba ...

  7. WPF获取外部EXE图标最简单的方法

    原文:WPF获取外部EXE图标最简单的方法 首先在工程添加对System.Drawing的引用 创建以下方法: public static ImageSource GetIcon(string fil ...

  8. android自定义View绘制天气温度曲线

    原文:android自定义View绘制天气温度曲线 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012942410/article/detail ...

  9. 脚本 启动/停止 jar包服务

    windows (.bat): @set port=8692 @echo %port% for /f "tokens=5" %%i in ('netstat -aon ^| fin ...

  10. CentOS6.5优盘安装

    从CentOS6.5开始直接把iso文件写入u盘就可实现优盘安装 windows平台:1.用UltraISO打开iso(如:CentOS-6.5-x86_64-bin-DVD1.iso)2.然后点“启 ...