(7)spring常用模式--------模板模式

1.模板方法简介

先了解一下模板方法的定义:

模板方法:定义一个操作中的算法的骨架, 而将一些步骤延迟到子类中。 Template Method 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤
应用场景: Spring ORM 数据模型,工作流

在来了解一下模板方法的uml图


(7)spring常用模式--------模板模式_第1张图片
模板方法.png

2.模板方法的实例

实例简介:
模拟试下了一个jdbcTemplate模板类,比如从数据源中获取一个链接,然后创建语句集,执行语句集获取结果集,解析结果集,最后结果集、语句集、连接的关闭都是固定的流程,而一个对象的如何映射成一个对象是需要具体的实现,这个由具体调用类实现对象的映射,这个步骤是可以重新定义的

接下来就是手写一个简单的jdbcTemplate的实现

行的映射接口,作用把数据库一行映射为一个对象

/**
 * @Project: spring
 * @description:   行的映射接口 ,把数据库一行映射为一个对象
 * @author: sunkang
 * @create: 2018-09-03 14:02
 * @ModificationHistory who      when       What
 **/
public interface RowMapper {
    T  mapRow(ResultSet rs,int rownum) throws  Exception;
}

jdbcTemplate 的具体实现类

/**
 * @Project: spring
 * @description:    jdbc 的模板类
 * @author: sunkang
 * @create: 2018-09-03 14:06
 * @ModificationHistory who      when       What
 **/
public class JdbcTemplate {

    private DataSource dataSource ;

    public JdbcTemplate(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    /**
     * 得到连接
     * @return
     * @throws SQLException
     */
    private Connection getConnection() throws SQLException {
        return this.dataSource.getConnection();
    }

    /**
     * 获得标准的语句集
     * @param sql
     * @param connection
     * @return
     * @throws SQLException
     */
    private PreparedStatement getPreparedStatement(String sql,Connection connection) throws SQLException {
        return connection.prepareStatement(sql);
    }

    /**
     * 得到结果集
     * @param preparedStatement
     * @param values
     * @return
     * @throws SQLException
     */
    private ResultSet getResultSet(PreparedStatement preparedStatement,Object[] values) throws SQLException {
       int i=0;
        for (Object value: values){
            preparedStatement.setObject(i++,value);
        }
        return preparedStatement.getResultSet();
    }
    /**
     * 解析结果集
     * @param resultSet
     * @param rowMapper
     * @return
     * @throws Exception
     */
    private List  parsedResultSet(ResultSet resultSet,RowMapper rowMapper) throws Exception {
        int rownumber = 1;
        List list = new ArrayList();
        while (resultSet.next()){
            list.add (rowMapper.mapRow(resultSet,rownumber++));
        }
        return  list;
    }
    /**
     * 关闭结果集
     * @param rs
     * @throws SQLException
     */
    private void  closeResultSet(ResultSet rs) throws SQLException {
        rs.close();
    }
    /**
     * 关闭语句集
     * @param statement
     * @throws SQLException
     */
    private  void closePreparedStatement(PreparedStatement statement ) throws SQLException {
        statement.close();
    }
    /**
     * 关闭连接
     * @param connection
     * @throws SQLException
     */
    private  void closeConnect(Connection connection ) throws SQLException {
        //通常把它放到连接池回收
        connection.close();
    }
    public List executeQuery(String sql , RowMapper mapper , Object[] values){
        try {
            //1.获得连接
           Connection connection =  this.getConnection();
            //2.获取语句集
            PreparedStatement  pstm = this.getPreparedStatement(sql,connection);
            //3.执行语句集,获得结果集
            ResultSet rs= this.getResultSet(pstm,values);
            //4.解析结果集
            List list  = this.parsedResultSet(rs,mapper);
            //5.关闭结果集
            this.closeResultSet(rs);
            //6.关闭语句集
            this.closePreparedStatement(pstm);
            //7.关闭连接
            this.closeConnect(connection);
            return  list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
 
 

模拟需要查询的实体类 Merber

/**
 * @Project: spring
 * @description:  Merber bean对象
 * @author: sunkang
 * @create: 2018-09-03 14:04
 * @ModificationHistory who      when       What
 **/
public class Merber {
    private String userName;
    private String password;
    private String nickName;

    private  int age;
    private  String addr;


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}

模拟Merber的dao层

/**
 * @Project: spring
 * @description:   模拟Merber的dao层
 * @author: sunkang
 * @create: 2018-09-03 16:34
 * @ModificationHistory who      when       What
 **/
public class MerberDao {
    //为什么不继承,主要是为了解耦
   //这里没有传直接的数据源 ,这里可以是mysql或者oracle的数据源都可以
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(null);
    public List query(){
        String sql = "select * from t_member";

        return jdbcTemplate.executeQuery(sql, new RowMapper() {
            @Override
            public Merber mapRow(ResultSet rs, int rownum) throws Exception {
                Merber merber = new Merber();
                merber.setUserName(rs.getString("username"));
                merber.setPassword(rs.getString("password"));
                merber.setNickName(rs.getString("nickname"));
                merber.setAge(rs.getInt("age"));
                merber.setAddr(rs.getString("addr"));
                return merber;
            }
        },null);
    }
}

模板方法测试

/**
 * @Project: spring
 * @description: 模板方法测试
 * @author: sunkang
 * @create: 2018-09-03 16:39
 * @ModificationHistory who      when       What
 **/
public class TemplateTest {

    public static void main(String[] args) {
        MerberDao merberDao   = new MerberDao();
        merberDao.query();
    }
}

你可能感兴趣的:((7)spring常用模式--------模板模式)