MVC案例之DAO层实现
搭建相关环境,
- 1.搭建c3p0环境,在Web工程的src目录下添加c3p0-config.xml文件。同时在WebContent/WEB-INF/lib/下加入c3p0的jar包和Oracle数据库驱动文件。
- 2.因为Jdbc用到DBUtils工具,所以同时导入DBUtils的jar包。
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="mvccrud">
<property name="user">scott</property>
<property name="password">tiger</property>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">50</property>
<property name="maxStatements">20</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
下面的代码在上次笔记上补充,具体代码:
package com.xs.mvc_crud.db;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils {
public static void releaseConnection(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static DataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource("mvccrud");
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
package com.xs.mvc_crud.dao;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.xs.mvc_crud.db.JdbcUtils;
public class DAO<T> {
private QueryRunner queryRunner = new QueryRunner();
private Class<T> clazz;
public DAO() {
Type superClass = getClass().getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)superClass;
Type [] typeArgs = parameterizedType.getActualTypeArguments();
if (typeArgs != null && typeArgs.length > 0) {
if (typeArgs[0] instanceof Class) {
clazz = (Class<T>) typeArgs[0];
}
}
}
}
public <E> E getForValue(String sql,Object ... args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new ScalarHandler<>(), args);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
public List<T> getForList(String sql, Object ... args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanListHandler<>(clazz), args);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
public T get(String sql,Object ... args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
return queryRunner.query(connection, sql, new BeanHandler<>(clazz), args);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseConnection(connection);
}
return null;
}
public void update(String sql,Object ... args) {
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
queryRunner.update(connection, sql, args);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseConnection(connection);
}
}
}
package com.xs.mvc_crud.dao.impl;
import java.math.BigDecimal;
import java.util.List;
import com.xs.mvc_crud.dao.CustomerDAO;
import com.xs.mvc_crud.dao.DAO;
import com.xs.mvc_crud.domain.Customer;
public class CustomerDAOJdbcImpl extends DAO<Customer> implements CustomerDAO {
@Override
public List<Customer> getAll() {
String sql = "SELECT id, name, address, phone FROM customers";
return getForList(sql);
}
@Override
public void save(Customer customer) {
String sql = "INSERT INTO customers(id, name, address, phone) values(id_seq.nextval,?,?,?)";
update(sql, customer.getName(), customer.getAddress(), customer.getPhone());
}
@Override
public Customer get(int id) {
String sql = "SELECT id, name, address, phone FROM customers WHERE id = ?";
return get(sql,id);
}
@Override
public void delete(int id) {
String sql = "DELETE FROM customers WHERE id = ?";
update(sql, id);
}
@Override
public BigDecimal getCountWithName(String name) {
String sql = "SELECT count(id) FROM customers WHERE name = ?";
return getForValue(sql,name);
}
}