图示
分层的目的是为了解耦。解耦就是为了降低代码的耦合度。方便项目后期的维护和升级。
包结构
web 层 com.atguigu.web/servlet/controller
-
service 层 com.atguigu.service Service 接口包
- com.atguigu.service.impl Service 接口实现类 dao 持久层 com.atguigu.dao Dao 接口包
- com.atguigu.dao.impl Dao 接口实现类
实体bean 对象com.atguigu.pojo/entity/domain/bean JavaBean 类
测试包com.atguigu.test/junit
工具类com.atguigu.utils
util
封装的工具包
test
测试文件
web
pages
pages各种页面
WEB-INF
安全目录
WEB-INF下的包:导入的jar包放这里面
编写工具类JdbcUtils
导入需要的jar 包(数据库和连接池需要):
druid-1.1.9.jar
mysql-connector-java-5.1.7-bin.jar
以下是测试需要:
hamcrest-core-1.3.jar
junit-4.12.jar
在src 源码目录下编写jdbc.properties 属性配置文件:
username=root
password=root
url=jdbc:mysql://localhost:3306/book
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
编写JdbcUtils 工具类:
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 获取数据库连接的工具类
*
* @author wzg
*
*/
public class JDBCUtils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("book_devoloper");
/**
* 使用ThreadLocal保存Connection对象
*/
private static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<Connection>();
private JDBCUtils() {
}
/**
* 获取数据库连接
*
* @return 如果获取连接成功,返回数据的连接对象。<br/>
* 如果获取数据库连接失败,则返回null
*/
public static Connection getConnection() {
// 先从ThreadLocal中获取
Connection connection = connectionThreadLocal.get();
try {
if (connection == null) {
// 从c3p0中获取数据库连接
connection = dataSource.getConnection();
// 设置事务为手动提交
connection.setAutoCommit(false);
connectionThreadLocal.set(connection);
}
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 释放数据库连接
*/
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放数据库连接
*/
public static void closeConnection() {
// 从线程ThreadLocal中获取
Connection conn = connectionThreadLocal.get();
if (conn != null) {
try {
// 事务提交
conn.commit();
// 事务关闭
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 移除
connectionThreadLocal.remove();
}
public static void rollback() {
// 从线程ThreadLocal中获取
Connection conn = connectionThreadLocal.get();
if (conn != null) {
try {
// 事务回滚
conn.rollback();
// 关闭连接
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 移除
connectionThreadLocal.remove();
}
编写BaseDao:
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.SQLException;
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.atguigu.util.JDBCUtils;
/**
* 我们使用的是DBUtil操作数据库
*
* @author wzg
*
* @param <T>
*/
public abstract class BaseDaoImpl<T> {
/**
* DBUtils操作数据库需要使用的类
*/
protected QueryRunner queryRunner;
/**
* 保存BaseDaoImpl类的泛型对象class类型
*/
protected Class<T> type;
@SuppressWarnings("unchecked")
public BaseDaoImpl() {
// 创建一个QueryRunner对象实例
queryRunner = new QueryRunner();
// 获取父类中带有泛型的父类的class类型
ParameterizedType supperClass = (ParameterizedType) getClass().getGenericSuperclass();