一般采用了dto和dao相结合的设计模式,dto进行传输数据,dao数据访问对象是一个面向对象的数据库接口,进行数据访问
如何实现
1.创建连接数据库的工具类
新建ConnectionFactory
类与数据库配置文件dbconfig.properties
ConectionFactory
类中代码如下
package com.test.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionFactory {
private static String driver;
private static String dburl;
private static String user;
private static String password;
private static final ConnectionFactory factory =new ConnectionFactory();
private static Connection conn;
static{ //静态代码块用于初始化类,可以为类的属性进行赋值
Properties prop=new Properties(); //保存属性文件中的键值队
try{
InputStream in=ConnectionFactory.class.getClassLoader() //获取当前类的类加载器
.getResourceAsStream("dbconfig.properties");
prop.load(in);
}
catch(Exception e){
System.out.println("配置文件读取错误");
}
driver=prop.getProperty("driver");
dburl=prop.getProperty("dburl");
user=prop.getProperty("user");
password=prop.getProperty("password");
}
private ConnectionFactory(){
}
public static ConnectionFactory getInstance(){ //单例模式
return factory;
}
public Connection makeConnection(){
try {
Class.forName(driver);
conn=DriverManager.getConnection(dburl,user,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
该类采用了单例模式,确保工程中只存在一个factory类
2.创建dto类,进行数据的按需组织
由于每张表都有主键Id 所以抽象出一个类IdEntity
作为所有dto类的父类,代码如下:
package com.test.entity;
public abstract class IdEntity {
protected Long id;
public Long getId(){
return id;
}
public void setId(Long id){
this.id=id;
}
}
新建实体类User
继承自IdEntity
获取数据,代码如下:
package com.test.entity;
public class User extends IdEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + ", id=" + id + "]";
}
// private String password;
}
3.创建dao接口与相应接口实现类
创建DAO接口UserDao
定义封装了一些抽象的方法,代码如下:
package com.test.dao;
import java.sql.Connection;
import java.sql.SQLException;
import com.test.entity.User;
public interface UserDao {
public void save(Connection conn,User user) throws SQLException;
public void delete(Connection conn,User user) throws SQLException;
}
创建UserDaoImpl类实现DAO接口
实现接口方法,进行数据库数据的读取工作,代码如下:
package com.test.dao.lmpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.test.dao.UserDao;
import com.test.entity.User;
public class UserDaolmpl implements UserDao {
/**
*保存用户信息
**/
@Override
public void save(Connection conn, User user) throws SQLException {
PreparedStatement ps=conn.
prepareCall("INSERT INTO hero(name) VALUES (?)");
ps.setString(1, user.getName());
ps.execute();
}
/**
*删除指定用户信息
**/
@Override
public void delete(Connection conn, User user) throws SQLException {
PreparedStatement ps=conn.
prepareCall("DELETE FROM hero WHERE id=?");
ps.setLong(1, user.getId());
ps.execute();
}
}
4.创建主函数进行测试
代码如下:
package com.test.test;
import java.sql.Connection;
import com.test.dao.UserDao;
import com.test.dao.lmpl.UserDaolmpl;
import com.test.entity.User;
import com.test.util.ConnectionFactory;
public class UserDaoTest {
public static void main(String[] args) {
Connection conn=null;
try{
conn =ConnectionFactory.getInstance().makeConnection(); //初始化连接
conn.setAutoCommit(false);
UserDao userDao=new UserDaolmpl();
User tom=new User();
tom.setName("tom");
userDao.save(conn, tom);
conn.commit(); //事务提交
}catch(Exception e){
try{
conn.rollback();//出错则回滚
}catch(Exception e2){
}
}
}
}