1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.white.dao;
import com.white.pojo.Byue;
import java.util.List;
public interface ByueDao {
public int addByue(Byue byue);
public int deleteByueId(Integer id);
public int updateByue(Byue byue);
public Byue queryByueById(Integer id);
public List<Byue> querByue();
} package com.white.dao.impl;
import com.white.utils.Jdbcutil; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection; import java.sql.SQLException; import java.util.List;
public abstract class BaseDao {//服用代码不需要实例
private QueryRunner queryRunner = new QueryRunner(); //update来执行/增删改查 public int update(String sql,Object ... args){ Connection connection = Jdbcutil.getConnection(); try { return queryRunner.update(connection,sql,args); } catch (SQLException e) { e.printStackTrace(); }finally { Jdbcutil.close(connection); } return -1; //返回-1表示失败,返回其他表示影响的行数 }
public <T> T queryForOne(Class<T>type,String sql,Object ... args){ Connection con = Jdbcutil.getConnection();
try { return queryRunner.query(con,sql,new BeanHandler<T>(type),args); } catch (SQLException e) { e.printStackTrace(); }finally { Jdbcutil.close(con); } return null; }
//查询返回多个java public <T>List<T> queryForyList(Class<T> type,String sql,Object ... args){ Connection con = Jdbcutil.getConnection();
try { return queryRunner.query(con,sql,new BeanListHandler<T>(type),args); } catch (SQLException e) { e.printStackTrace(); }finally { Jdbcutil.close(con); } return null; }
public Object queryForSingleValue(String sql,Object ... args){ Connection conn = Jdbcutil.getConnection(); try { return queryRunner.query(conn, sql,new ScalarHandler(),args); } catch (SQLException e) { e.printStackTrace(); } finally { Jdbcutil.close(conn); } return null; }
}
` '两个不同,书写sql语句时切忌 在sql语句配置时url=jdbc:mysql://localhost:3306/xmt?characterEncoding=utf8增加说明utf-8 在传参时候记得设置无参
|