大家好,我是一个爱举铁的程序员Shr。
本篇文章将用到前几篇文章介绍过的知识自定义数据访问层框架,建议看这篇文章之前先去了解JDBC元数据和反射。
如果是初学者,觉得JDBC封装数据太麻烦,一个类十多个字段,重复的代码导致浪费了大量时间,那待会我开车的时候你可要抓紧了。
如果你用Hibernate,Mybatis用了两三年还只是停留在使用的情况,看源码太费劲,看一会就想睡觉,本篇文章将带你走进框架底层,探索精彩的世界。
本篇文章较长,耐心看完~~~
源码地址:https://github.com/ShrMus/Dao/tree/master/dao_20180603/src/main/java/com/shrmus/jdbc02
一、新建数据库表
新建名为dao_20180603的数据库,再新建emp表
CREATE TABLE `emp` (
`id` int(11) NOT NULL,
`name` varchar(255) default NULL,
`address` varchar(255) default NULL,
`hireDate` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
二、新建类
public class Emp {
private int id;
private String name;
private String address;
private Date hireDate;
public Emp() {
}
public Emp(int id, String name, String address, Date hireDate) {
this.id = id;
this.name = name;
this.address = address;
this.hireDate = hireDate;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", address=" + address + ", hireDate=" + hireDate + "]";
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
}
三、新建数据源类
/**
* 自定义数据源
* <p>Title:MyDataSource</p>
* <p>Description:</p>
* @author Shr
* @date 2018年6月3日上午9:32:51
* @version
*/
public class MyDataSource {
// 数据库驱动
private final String driverClassName;
// 数据库连接URL
private final String url;
// 数据库用户名
private final String username;
// 数据库密码
private final String password;
/**
* 构造方法注入属性值
*/
public MyDataSource(){
Properties properties = new Properties();
InputStream inputStream;
try {
String path = this.getClass().getResource("").getPath();
inputStream = new FileInputStream(path + "/jdbc.properties");
properties.load(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.driverClassName = properties.getProperty("jdbc.driverClassName");
this.url = properties.getProperty("jdbc.url");
this.username = properties.getProperty("jdbc.username");
this.password = properties.getProperty("jdbc.password");
}
public MyDataSource(String driverClassName,String url,String username,String password){
this.driverClassName = driverClassName;
this.url = url;
this.username = username;
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
四、新建数据库连接属性文件
新建文件jdbc.properties,文件和数据源类在同一个目录,文件内容如下:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dao_20180603?characterEncoding=utf8
jdbc.username=root
jdbc.password=shrmus
五、新建数据库连接类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 自定义连接
* <p>Title:MyConnection</p>
* <p>Description:</p>
* @author Shr
* @date 2018年6月3日上午9:59:31
* @version
*/
public final class MyConnection{
private Connection connection = null;
public MyConnection() {
}
public MyConnection(MyDataSource myDataSource) {
try {
Class.forName(myDataSource.getDriverClassName());
connection = DriverManager.getConnection(myDataSource.getUrl(), myDataSource.getUsername(), myDataSource.getPassword());
} catch (ClassNotFoundException e) {
System.err.println("构造MyConnection实例失败!");
} catch (SQLException e) {
System.err.println("获取MyConnection失败!");
}
}
public Connection getConnection() {
return connection;
}
/**
* 关闭数据库连接对象
* @param connection
*/
public static void close(Connection connection) {
if(null != connection) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("关闭数据库连接对象失败!");
}
}
}
/**
* 关闭SQL执行对象
* @param statement
*/
public static void close(Statement statement) {
if(null != statement) {
try {
statement.close();
} catch (SQLException e) {
System.err.println("关闭SQL执行对象失败!");
}
}
}
/**
* 关闭结果集对象
* &