目录结构

1.在domain包中创建User实体类

package com.rick.domain;

import java.util.Date;

public class User {
private int id;
private String name;
private Date birthday;
private float money; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public float getMoney() {
return money;
} public void setMoney(float money) {
this.money = money;
}
}

2.在Dao包中创建User的实现接口

package com.rick.dao;

import com.rick.domain.User;

public interface UserDao {
public void addUser(User user); public User getUser(int userId); public User findUser(String loginName, String password); public void update(User user); public void delete(User user);
}

3.新建一个utils包创建工具类JdbcUtils

package com.rick.utlis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = ""; private JdbcUtils() {
} static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
} public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
} public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

4.在impl中创建接口实现类UserDaoJdbcImpl

package com.rick.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import com.rick.dao.UserDao;
import com.rick.domain.User;
import com.rick.utlis.JdbcUtils; public class UserDaoJdbcImpl implements UserDao { public void addUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "insert into user(name,birthday, money) values (?,?,?) ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
} public void delete(User user) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "delete from user where id=" + user.getId();
st.executeUpdate(sql);
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, st, conn);
} } public User findUser(String loginName, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select id, name, money, birthday from user where name=?";
ps = conn.prepareStatement(sql);
ps.setString(1, loginName);
rs = ps.executeQuery();
while (rs.next()) {
user = mappingUser(rs);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
return user;
} public User getUser(int userId) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select id, name, money, birthday from user where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
rs = ps.executeQuery();
while (rs.next()) {
user = mappingUser(rs);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
return user;
} private User mappingUser(ResultSet rs) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setMoney(rs.getFloat("money"));
user.setBirthday(rs.getDate("birthday"));
return user;
} public void update(User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "update user set name=?, birthday=?, money=? where id=? ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.setInt(4, user.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
} }

5.在dao中创建一个捕获异常类DaoException继承RuntimeException

package com.rick.dao;

public class DaoException extends RuntimeException {

	public DaoException() {
// TODO Auto-generated constructor stub
} public DaoException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public DaoException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} public DaoException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public DaoException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} }

6.在业务逻辑层service包中创建UserService(模拟,此项目中无用)

package com.rick.service;

import com.rick.dao.UserDao;

public class UserService {
private UserDao userDao; public void regist(com.rick.domain.User user) {
this.userDao.addUser(user);
// sendMail.send(user);
}
}

7.创建daoconfig.properties文件

8.在Dao中创建工厂类DaoFactory

package com.rick.dao;

import java.io.InputStream;
import java.util.Properties; public class DaoFactory {
private static UserDao userDao = null;
private static DaoFactory instance = new DaoFactory(); private DaoFactory() {
try {
Properties prop = new Properties();
InputStream inStream = DaoFactory.class.getClassLoader()
.getResourceAsStream("daoconfig.properties");
prop.load(inStream);
String userDaoClass = prop.getProperty("userDaoClass");
Class clazz = Class.forName(userDaoClass);
userDao = (UserDao) clazz.newInstance();
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
} public static DaoFactory getInstance() {
return instance;
} public UserDao getUserDao() {
return userDao;
}
}

9.测试

package com.rick.dao;

import java.util.Date;

import com.rick.domain.User;

public class UserDaoTest {

	public static void main(String[] args) {
UserDao userDao = DaoFactory.getInstance().getUserDao();
// System.out.println(userDao); User user = new User();
user.setBirthday(new Date());
user.setName("dao name1");
user.setMoney(1000.0f); userDao.addUser(user); // User u = userDao.findUser(user.getName(), null);
// System.out.println(u.getId()); // User u = userDao.getUser(7);
// u.setMoney(20000.1f);
// userDao.update(u); // User u1 = userDao.getUser(8);
// userDao.delete(u1);
}
}

DAO三层架构及工厂模式的更多相关文章

  1. SSH和三层架构的MVC模式的对应关系

    1.MVC(Model-View-Controller)设计模式: 首先让我们了解下MVC(Model-View-Controller)的概念: MVC全名是Model View Controller ...

  2. 关于三层架构和MVC模式的思考

    MVC模式 核心: 1.解耦Model和View,即使得Model可以被不同的展示,比如一批统计数据可以分别用柱状图.饼图表示 2.Controller用来保证Model和View的同步 Model ...

  3. 三层架构和MVC模式

    目录[-] 1.三层架构 2.MVC 2.1 标准的MVC(Model-View-Controller) 2.2 Web MVC 3.三层架构和MVC的区别与联系 1.三层架构 三层架构(3-tier ...

  4. (转)浅析三层架构与MVC模式的区别

    MVC模式介绍: MVC全名是Model ViewController,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据 ...

  5. 浅析MVC模式与三层架构的区别01

    三层架构和MVC是有明显区别的,MVC应该是展现模式(三个加起来以后才是三层架构中的UI层)三层架构(3-tier application) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI ...

  6. MVC模式与三层架构的区别

    之前总是混淆MVC表现模式和三层架构模式,为此记录下. 三层架构和MVC是有明显区别的,MVC应该是展现模式(三个加起来以后才是三层架构中的UI层) 三层架构(3-tier application) ...

  7. 浅析MVC模式与三层架构的区别

    浅析MVC模式与三层架构的区别 三层架构和MVC是有明显区别的,MVC应该是表现模式(三个加起来以后才是三层架构中的UI层).三层架构(3-tier application) 通常意义上的三层架构就是 ...

  8. 从MVC和三层架构说到SSH整合开发

    相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...

  9. 三层架构与MVC的区别

    我们平时总是将混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. MVC是一个设 ...

随机推荐

  1. Kotlin 学习 (一)

    开始学习kotlin mark入门资料一篇: Kotlin入门第一课:从对比Java开始 未完待续...

  2. 【Luogu4448】 [AHOI2018初中组]球球的排列

    题意 有 \(n\) 个球球,每个球球有一个属性值 .一个合法的排列满足不存在相邻两个球球的属性值乘积是完全平方数.求合法的排列数量对 \(10^9+7\) 取膜. \(n\le 300\) (本题数 ...

  3. LoadRunner监控Linux系统

    需要下载3个包:  地址链接:链接:https://pan.baidu.com/s/1lltAa6JnjJ7Mr88duixUSQ 密码:5yiw(1)rsh-0.17-14.i386.rpm (2) ...

  4. Object.getOwnPropertyDescriptors()

    Object.getOwnPropertyDescriptors() 前面说过,Object.getOwnPropertyDescriptor方法会返回某个对象属性的描述对象(descriptor). ...

  5. 热门提示输入之AutoCompleteTextView

    前言: 在我们百度时,经常会遇到在你输入一个内容后在你的输入框的下面会提示一些别的热门的东西,接下来我们来简单实现这种功能 正文: 直接上代码,和之前的东西都大同小异 下面是Java代码 public ...

  6. git使用问题二删除远程仓库文件,本地保留不动

    git rm --cached filename/-r directory git commit "xxxx" git push

  7. ThinkPad重大更新!5款创意设计PC齐发2日

    导读 日前,ThinkPad巨匠P系列专业移动工作站迎来全线更新,包括ThinkPad P1隐士2019.ThinkPad P53.ThinkPad P73.ThinkPad P43s.ThinkPa ...

  8. idea中跑mapreduce报错, PATH设置错误

    问题如题,报错: [root@node01 servers]# hadoop jar loginVisit.jar cn.itcast.loginVisit.step1.Step1Main19/07/ ...

  9. Java生鲜电商平台-优惠券系统设计详解

    Java生鲜电商平台-优惠券系统设计详解 优惠券作为电商最常用的营销手段,对于商家而言可以起到拉新.促活.提高转化的作用,对用户而言也可以获得实惠,今天就来谈谈优惠券系统的设计逻辑. 我对于优惠券系统 ...

  10. 009、Java中超过了int的最大值或最小值的结果

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...