1.DAO基类(数据库操作基类)
这里使用了层超类模式,复用代码,统一处理异常,日志等等..
BaseDAO:
package com.d1zhan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class BaseDAO {
/**
* 获取数据库连接
* @return
*/
public Connection getConnection() {
Connection conn = null;
String jdbcURL = "jdbc:mysql://localhost/dbname";
String jdbcDriver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "root";
try {
DbUtils.loadDriver(jdbcDriver);
conn = DriverManager.getConnection(jdbcURL, user, password);
} catch (SQLException e) {
// handle the exception
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
return conn;
}
/**
* 查找多个对象
* @param sqlString
* @param clazz
* @return
*/
public List query(String sqlString, Class clazz) {
List beans = null;
Connection conn = null;
try {
conn = getConnection();
QueryRunner qRunner = new QueryRunner();
beans =
(List) qRunner.query(
conn,
sqlString,
new BeanListHandler(clazz));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
return beans;
}
/**
* 查找对象
* @param sqlString
* @param clazz
* @return
*/
public Object get(String sqlString, Class clazz) {
List beans = null;
Object obj = null;
Connection conn = null;
try {
conn = getConnection();
QueryRunner qRunner = new QueryRunner();
beans =
(List) qRunner.query(
conn,
sqlString,
new BeanListHandler(clazz));
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
if(beans!=null && !beans.isEmpty()){ //注意这里
obj=beans.get(0);
}
return obj;
}
/**
* 执行更新的sql语句,插入,修改,删除
* @param sqlString
* @return
*/
public boolean update(String sqlString) {
Connection conn = null;
boolean flag = false;
try {
conn = getConnection();
QueryRunner qRunner = new QueryRunner();
int i = qRunner.update(conn,sqlString);
if (i > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
return flag;
}
}
2,Employee 雇员数据库操作DAO类
大家可以看到,下面的DAO类,继承BaseDAO,方法体都是组装sql语句,不必关心异常的处理,资源的管理(Connection,Statement等),这样代码简洁,质量高.
EmployeeDAO:
package com.d1zhan;
import java.util.List;
public class EmployeeDAO extends BaseDAO {
/**
* searchModel的属性,name,address等为查询参数
* @param searchModel
* @return
*/
public List query(Employee searchModel) {
String sql = "select * from employee where 1=1";
//如果雇员名字不为null,则将雇员名字加入where查询条件
if (searchModel.getName() != null) {
sql += "and employee.name like '" + searchModel.getName() + "' ";
}
return this.query(sql, Employee.class);
}
/**
* 修改雇员信息
* @param emp
* @return
*/
public boolean edit(Employee emp) {
String sql = "update employee set "; //注意: set加在外面
//如果name不为null,修改它的值到数据库
if (emp.getName() != null) {
sql += "employee.name='" + emp.getName() + "' ,";
}
//如果address不为null,修改它的值到数据库
if (emp.getAddress() != null) {
sql += "employee.address='" + emp.getAddress() + "', ";
}
sql=sql.substring(0,sql.length()-1); //去掉最后一个","
sql += "where employee.id=" + emp.getId();
return this.update(sql);
}
/**
* 根据雇员ID删除雇员
* @param id
* @return
*/
public boolean delete(int id) {
String sql = "delete from employee where id =" + id;
return this.update(sql);
}
/**
* 根据雇员ID查找雇员
* @param id
* @return
*/
public Employee get(int id) {
String sql = "select * from employee where id=" + id;
return (Employee) this.get(sql, Employee.class);
}
}
3.雇员信息类
Employee:
package com.pcm.netrender.model;
public class Employee {
private int id;
private String name;
private String address;
/**
* @return
*/
public String getAddress() {
return address;
}
/**
* @return
*/
public int getId() {
return id;
}
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param string
*/
public void setAddress(String string) {
address = string;
}
/**
* @param i
*/
public void setId(int i) {
id = i;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
}
4,现在就可以在业务层(service)调用我们的dao类方法,实现业务逻辑了
在service我们要处理业务逻辑,数据库操作事务等等.
然后Struts Action调用 service类的方法,完成软件了.
使用代码大概是这样的
Employee emp=new Employee();
emp.setName("测试雇员");
emp.setAddress("深圳南山科技园");
EmployeeDAO dao=new EmployeeDAO();
dao.save(emp);