//20210127
写在前面:刚期末考试完,考了面向对象,里边儿有23个设计模式,我寻思着考完挨个儿实现一下,本文实现模板模式
模板模式核心思想:
如果完成某件事的步骤就那么几个,那么我们就可以定义一个模板专门用于完成某件事情,就好像实现一个框架,子类往里边填充步骤的具体内容就行
举例:做成一道菜的步骤有择菜、洗菜、炒菜,那么我们就可以定义一个模板,分为择菜、洗菜、炒菜,然后如果想要做出西红柿炒蛋,那就先挑选并处理西红柿和鸡蛋(择菜),再洗西红柿、打鸡蛋(洗菜),最后下锅炒(炒菜),这样西红柿鸡蛋的具体步骤就填充进框架了,外界只用调用填充好的子类就可以做出西红柿炒鸡蛋了
程序源代码
此处实现一个JDBC模板,更接近我们的使用和学习
注:需要导入mysql官方jar包,否则会报错ClassNotFound
/**
* 抽象dao层——模板
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class AbstractDao {
protected Object find(String sql,Object[] params){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Object obj = null;
try{
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0;i
ps.setObject(i+1,params[i]);
}
rs = ps.executeQuery();
while(rs.next()){
obj = rowMapper(rs);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.free(rs,ps,conn);
}
return obj;
}
protected abstract Object rowMapper(ResultSet rs) throws SQLException;
//同时可以添加insert,update等方法
}
//具体dao层
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* UserDAO
*/
public class UserDAO extends AbstractDao{
public User findUser(int userId){
String sql = "select * from user where id = ?";//预语句
Object[] params = new Object[]{userId};
Object user = super.find(sql,params);
// System.out.println((User)user);
return (User)user;
}
@Override
protected Object rowMapper(ResultSet rs) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
user.setAddress(rs.getString("address"));
return user;
}
}
/**
* 用户类
*/
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
/**
* jdbc工具类
*/
import java.sql.*;
public class JDBCUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbcstudy";
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, PreparedStatement ps , Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
//测试主类
public class Main {
public static void main(String[] args) {
UserDAO dao = new UserDAO();
System.out.println(dao.findUser(1));
System.out.println(dao.findUser(2));
System.out.println(dao.findUser(3));
System.out.println(dao.findUser(4));
}
}
数据库创建&添加table语句如下:
create table user(
id int primary key auto_increment,
name varchar(20) not null,
age int,
sex varchar(2) not null,
address varchar(50)
);
insert into user(name,age,sex,address) values ("张三","18","男","河北唐山");
insert into user(name,age,sex,address) values ("李四","16","男","河南驻马店");
insert into user(name,age,sex,address) values ("王五","22","女","北京朝阳区");
insert into user(name,age,sex,address) values ("赵六",null,"男",null);
数据库cmd终端输出如下:
输出如下:
以上
希望对大家有所帮助