本系列博客内容为:做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. 【38.46%】【codeforces 615E】Hexagons

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. Configuring a remote m-phy

    An interface for low power, high bandwidth communications between units in a device in provided here ...

  3. sql 连接查询

    什么是连接查询呢 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 分类 首先划分一下,连接分为三种:内连接.外连接.交叉连接 内连接(INNER JOIN): ...

  4. MyCat负载均衡 下篇

    MyCat负载均衡 下篇   之前在 一步一步在Windows中使用MyCat负载均衡 上篇 中已经讲了如何配置出MyCat.下面讲其相关的使用. 五.配置MyCat-eye 对于MyCat监控官网还 ...

  5. 推断(inference)、贝叶斯规则(Bayes's rule)与导出分布(derived distribution)

    1. 建模 对原始信号 X 进行观测,观测可以抽象为(离散:PY|X(y|x), 连续:fY|X(y|x)),物理世界噪声的存在,将导致观测到的 X 出现一定的噪声,记为 Y: X⇒fY|X(y|x) ...

  6. Delphi读取文件属性

    Read File Detailed Properties https://www.board4all.biz/threads/read-file-detailed-properties.655787 ...

  7. 【C语言学习】C语言功能

    代码,功能为了更好地实现模块化编程.那么,什么是函数的性质?在函数中定义的变量(全局变量.局部变量.静态变量)如何存储?为什么范围和全局变量和局部变量的寿命是不一样的?只是有一个更深入的了解的功能.能 ...

  8. DevOps技术路线图

    来自roadmap.sh github地址 自己整理的百度脑图中文版

  9. JS 密码弱中强显示

    <!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" conten ...

  10. boost库asio详解8——几个TCP的简单例子

    摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 void test_asio_synclient() { typedef boost::asio::io_service IoSe ...