3.dao方法类
3.1BaseDao
package com.YU.util; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.YU.entity.Book; public class BaseDao<T> { /** * 通用查询的basedao * @param sql * @param clz * @param pageBean * @return * @throws Exception */ public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws Exception{ List<T> list = new ArrayList<T>(); Connection conn = DBAccess.getConnection(); PreparedStatement ps = conn.prepareStatement(countSQL); ResultSet rs = ps.executeQuery(); while(rs.next()) { //利用反射获取到对象 T t = (T) clz.newInstance(); //拿到clz对应所有的属性对象 Field[] fields = clz.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); f.set(t, rs.getObject(f.getName())); } list.add(t); } return list; } }
3.2 原生dao方法
public List<Book> find1(Book book,PageBean pageBean) throws Exception{ List<Book> list = new ArrayList<Book>(); Connection conn = DBAccess.getConnection(); String sql = "select * from t_mvc_book where 1=1"; String bname = book.getBname(); if(StringUtils.isNotBlank(bname)) { sql+=" and bname like '%"+bname+"%'"; } PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()) { Book b = new Book(); b.setBid(rs.getInt("bid")); b.setBname(rs.getString("bname")); b.setPrice(rs.getFloat("price")); list.add(b); } return list; }
3.3 原生Dao方法继承BaseDao后使用
public List<Book> find2(Book book,PageBean pageBean) throws Exception{ String sql = "select * from t_mvc_book where 1=1"; String bname = book.getBname(); if(StringUtils.isNotBlank(bname)) { sql+=" and bname like '%"+bname+"%'"; } return super.executeQuery(sql, Book.class, pageBean); }
BaseDao的作用在于,将一些简单重复性代码进行封装,再到Dao方法中进行调用。
3.4 原生dao方法继承BaseDao实现通用分页
BaseDao部分,原生代码如上
public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws Exception{ List<T> list = new ArrayList<T>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; if(pageBean!=null && pageBean.isPagination()) { //获取记录数 String countSQL = getCountSQL(sql); conn = DBAccess.getConnection(); ps = conn.prepareStatement(countSQL); rs = ps.executeQuery(); if(rs.next()) { pageBean.setTotal(rs.getObject("n").toString()); } //获取到SQl语句分页的数据 String pageSQL = getPageSQL(sql,pageBean); conn = DBAccess.getConnection(); ps = conn.prepareStatement(pageSQL); rs = ps.executeQuery(); }else { conn = DBAccess.getConnection(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); } while(rs.next()) { //利用反射获取到对象 T t = (T) clz.newInstance(); //拿到clz对应所有的属性对象 Field[] fields = clz.getDeclaredFields(); for (Field f : fields) { f.setAccessible(true); f.set(t, rs.getObject(f.getName())); } list.add(t); } return list; } /** * 查询最后分页数据的方法 * @param sql * @param pageBean * @return */ private String getPageSQL(String sql, PageBean pageBean) { return sql+" limit "+pageBean.getStartIndex()+","+pageBean.getRows(); } /** * 查询记录数的方法 * @param sql * @return */ private String getCountSQL(String sql) { return "select count(1) as n from ("+sql+") t"; }
运行结果:
Book [bid=45, bname=圣墟第45章, price=45.0] Book [bid=46, bname=圣墟第46章, price=46.0] Book [bid=47, bname=圣墟第47章, price=47.0] Book [bid=49, bname=圣墟第49章, price=49.0] Book [bid=50, bname=圣墟第50章, price=50.0] Book [bid=51, bname=圣墟第51章, price=51.0] Book [bid=52, bname=圣墟第52章, price=52.0] Book [bid=53, bname=圣墟第53章, price=53.0] Book [bid=54, bname=圣墟第54章, price=54.0] Book [bid=55, bname=圣墟第55章, price=55.0] PageBean [page=3, rows=10, total=85, pagination=true]
每页十条,总共85页,page表示第几页