标签:one 作业 object 结构 temp protected rop exec cte
模板模式邮件模板方法模式(Template Method Pattern),是指定义一个算法的骨架,并允许子类为一个或多个步骤提供实现。模板模式使模板使用于以下场景:
(1)一次性实现一个算法的不变部分,并将可变部分的行为留给子类类实现。
(2)各子类中公共的行为呗提取出来并集到一个公共的父类中,从而避免代码重复。
创建一个模板类 jdbcTemplate 封装所有的 JDBC 操作。以查询为例,每次查询的表不同,返回的数据结构也就不一样。我们针对不同的数据,都要将其封装成不同的实体对象。而每个实体对象的封装逻辑是不一样的,但封装前和封装后的处理流程是不变的,因此可以使用模板模式来进行设计。先创建约束 ORM 逻辑的接口 RowMapper:
package com.xq.design.template; import java.sql.ResultSet; public interface RowMapper<T> { T mapRow(ResultSet rs, int rowNum) throws Exception; }
再创建封装了所有处理流程的抽象类 JdbcTemlate :
package com.xq.design.template.jdbc; import com.xq.design.template.RowMapper; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public abstract class JdbcTemplate { private DataSource dataSource; public JdbcTemplate(DataSource dataSource) { this.dataSource = dataSource; } public List<?> executeQuery(String sql, RowMapper<?> rowMapper, Object[] values){ try{ //获取连接 Connection conn = this.getConnection(); //创建语句 PreparedStatement pstm = this.createPropareStatement(conn,sql); //执行语句集 ResultSet rs = this.executedQuery(pstm,values); //处理结果集 List<?> result = this.paresResultSet(rs,rowMapper); //关闭结果集 this.closeResultSet(rs); //关闭语句集 this.closeStatement(pstm); //关闭连接 this.closeConnection(conn); return result; } catch (Exception e) { e.printStackTrace(); } return null; } protected void closeConnection(Connection conn) throws Exception{ conn.close(); } protected void closeStatement(PreparedStatement pstm) throws Exception{ pstm.close(); } protected void closeResultSet(ResultSet rs) throws Exception{ rs.close(); } protected List<?> paresResultSet(ResultSet rs, RowMapper<?> rowMapper ) throws Exception{ List<Object> result = new ArrayList<Object>(); int rowNum = 1; while(rs.next()){ result.add(rowMapper.mapRow(rs,rowNum ++)); } return result; } protected ResultSet executedQuery(PreparedStatement pstm,Object[] values) throws Exception{ for(int i = 0; i < values.length; i++){ pstm.setObject(i,values[i]); } return pstm.executeQuery(); } protected PreparedStatement createPropareStatement(Connection conn, String sql) throws Exception{ return conn.prepareStatement(sql); } public Connection getConnection() throws Exception{ return this.dataSource.getConnection(); } }
创建实体对象类 Member:
package com.xq.design.template.entity; import lombok.Data; @Data public class Member { private String userName; private String passWord; private String nickName; private int age; private String addr; }
创建数据库操作类 MemberDao:
package com.xq.design.template.jdbc.dao; import com.xq.design.template.RowMapper; import com.xq.design.template.entity.Member; import com.xq.design.template.jdbc.JdbcTemplate; import javax.sql.DataSource; import java.sql.ResultSet; import java.util.List; public class MemberDao extends JdbcTemplate { public MemberDao(DataSource dataSource) { super(dataSource); } public List<?> selectAll(){ String sql = "select * from t_member"; return super.executeQuery(sql, new RowMapper<Member>() { public Member mapRow(ResultSet rs, int rowNum) throws Exception{ Member member = new Member(); member.setUserName(rs.getString("userName")); member.setPassWord(rs.getString("passWord")); member.setAge(rs.getInt("age")); member.setAddr(rs.getString("addr")); return member; } },null); } }
客户端测试代码如下:
package com.xq.design.template.jdbc; import com.xq.design.template.jdbc.dao.MemberDao; import java.util.List; public class MemberDaoTest { public static void main(String[] args) { MemberDao memberDao = new MemberDao(null); List<?> result = memberDao.selectAll(); System.out.println(result); } }
标签:one 作业 object 结构 temp protected rop exec cte
原文地址:https://www.cnblogs.com/xianquan/p/12885828.html