时间:2021-07-01 10:21:17 帮助过:11人阅读
- package com.augmentum.oes.common;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public interface JDBCCallback<T> {
- T rsToObject(ResultSet rs) throws SQLException;
- void setParams(PreparedStatement stmt) throws SQLException;
- }
加入数据
View Code
- <span style="color: #0000ff">package</span><span style="color: #000000"> com.augmentum.oes.dao.impl;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.PreparedStatement;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.ResultSet;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.SQLException;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.List;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.jdbc.core.JdbcTemplate;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.common.JDBCAbstractCallback;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.common.JDBCCallback;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.dao.QuestionDao;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.model.Question;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.util.Pagination;
- </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.augmentum.oes.util.StringUtil;
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> QuestionDaoImpl <span style="color: #0000ff">implements</span><span style="color: #000000"> QuestionDao{
- </span><span style="color: #0000ff">private</span> Question rsToQuestion(ResultSet rs) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- Question question </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> Question();
- question.setId(rs.getInt(</span>"id"<span style="color: #000000">));
- question.setQuestion_desc(rs.getString(</span>"question_desc"<span style="color: #000000">));
- question.setRight_choice_name(rs.getString(</span>"right_choice_name"<span style="color: #000000">));
- question.setChoice_a(rs.getString(</span>"choice_a"<span style="color: #000000">));
- question.setChoice_b(rs.getString(</span>"choice_b"<span style="color: #000000">));
- question.setChoice_c(rs.getString(</span>"choice_c"<span style="color: #000000">));
- question.setChoice_d(rs.getString(</span>"choice_d"<span style="color: #000000">));
- question.setQuestion_status(rs.getInt(</span>"question_status"<span style="color: #000000">));
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> question;
- }
- </span><span style="color: #0000ff">private</span><span style="color: #000000"> JdbcTemplate jdbcTemplate;
- @Override
- </span><span style="color: #0000ff">public</span> Question queryById(<span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span><span style="color: #000000"> question_id){
- String sql </span>= "SELECT * FROM question where id = ?"<span style="color: #000000"> ;
- JDBCCallback</span><Question> j = <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setInt(</span>1<span style="color: #000000">, question_id);
- </span><span style="color: #0000ff">super</span><span style="color: #000000">.setParams(stmt);
- }
- @Override
- </span><span style="color: #0000ff">public</span> Question rsToObject(ResultSet rs) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> rsToQuestion(rs);
- }
- };
- List</span><Question> list =<span style="color: #000000"> jdbcTemplete.query(sql,j);
- </span><span style="color: #0000ff">return</span> list.get(0<span style="color: #000000">);
- }
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> update(<span style="color: #0000ff">final</span><span style="color: #000000"> Question question) {
- String sql </span>= "UPDATE question SET question_desc=?,right_choice_name=?,"
- + "choice_a=?, choice_b=?,choice_c=?,choice_d=?,question_status=? WHERE id = ? "<span style="color: #000000">;
- </span><span style="color: #0000ff">int</span> count =jdbcTemplete.update(sql, <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setString(</span>1<span style="color: #000000">, question.getQuestion_desc());
- stmt.setString(</span>2<span style="color: #000000">, question.getRight_choice_name());
- stmt.setString(</span>3<span style="color: #000000">, question.getChoice_a());
- stmt.setString(</span>4<span style="color: #000000">, question.getChoice_b());
- stmt.setString(</span>5<span style="color: #000000">, question.getChoice_c());
- stmt.setString(</span>6<span style="color: #000000">, question.getChoice_d());
- stmt.setInt(</span>7<span style="color: #000000">, question.getQuestion_status());
- stmt.setInt(</span>8<span style="color: #000000">, question.getId());
- }
- });
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> count;
- }
- @Override
- </span><span style="color: #0000ff">public</span> List<Question> getListByKeyWord(<span style="color: #0000ff">final</span><span style="color: #000000"> String keyword, Pagination pagination,String orderTags) {
- pagination.setTotalCount(</span><span style="color: #0000ff">this</span><span style="color: #000000">.getCount(keyword));
- </span><span style="color: #0000ff">if</span> (pagination.getCurrentPage() ><span style="color: #000000"> pagination.getTotalCount()) {
- pagination.setCurrentPage(pagination.getTotalCount());
- }
- String sql </span>="SELECT * FROM question WHERE question_status = 0 AND question_desc LIKE ? ORDER BY id "+(StringUtil.isEmpty(orderTags)?"ASC":"DESC")+" LIMIT "+pagination.getOffset()+","+<span style="color: #000000">pagination.getPageSize() ;
- List</span><Question> list = jdbcTemplete.query(sql, <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setString(</span>1,"%"+keyword+"%"<span style="color: #000000">);
- }
- @Override
- </span><span style="color: #0000ff">public</span> Question rsToObject(ResultSet rs) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> rsToQuestion(rs);
- }
- });
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> list;
- }
- @Override
- </span><span style="color: #0000ff">public</span> List<Question><span style="color: #000000"> getList(Pagination pagination,String orderTags) {
- pagination.setTotalCount(</span><span style="color: #0000ff">this</span>.getCount(<span style="color: #0000ff">null</span><span style="color: #000000">));
- </span><span style="color: #0000ff">if</span> (pagination.getCurrentPage() ><span style="color: #000000"> pagination.getTotalCount()) {
- pagination.setCurrentPage(pagination.getTotalCount());
- }
- String sql </span>="SELECT * FROM question WHERE question_status = 0 ORDER BY id "+(StringUtil.isEmpty(orderTags)?"ASC":"DESC")+" LIMIT "+pagination.getOffset()+","+<span style="color: #000000">pagination.getPageSize() ;
- List</span><Question> list = jdbcTemplete.query(sql, <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> Question rsToObject(ResultSet rs) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> rsToQuestion(rs);
- }
- });
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> list;
- }
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> deleteById(<span style="color: #0000ff">final</span> <span style="color: #0000ff">int</span><span style="color: #000000"> id) {
- </span><span style="color: #008000">//</span><span style="color: #008000"> String sql = "DELETE FROM question where id = ?";</span>
- String sql = "UPDATE question SET question_status=1 WHERE id= ?"<span style="color: #000000">;
- </span><span style="color: #0000ff">int</span> count = jdbcTemplete.deleteByid(sql, <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setInt(</span>1<span style="color: #000000">, id);
- }
- });
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> count;
- }
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> getCount(<span style="color: #0000ff">final</span><span style="color: #000000"> String question_desc) {
- </span><span style="color: #0000ff">int</span> count =0<span style="color: #000000">;
- String sql </span>= "SELECT count(*) FROM question WHERE question_status = 0"<span style="color: #000000">;
- </span><span style="color: #0000ff">if</span><span style="color: #000000"> (StringUtil.isEmpty(question_desc)) {
- count </span>=<span style="color: #000000"> jdbcTemplete.getCountAll(sql);
- } </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
- sql </span>= sql +" where question_desc LIKE ?"<span style="color: #000000">;
- count </span>= jdbcTemplete.getCount(sql, <span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setString(</span>1,"%"+question_desc+"%"<span style="color: #000000">);
- </span><span style="color: #0000ff">super</span><span style="color: #000000">.setParams(stmt);
- }
- });
- }
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> count;
- }
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span><span style="color: #000000"> getNextId() {
- </span><span style="color: #0000ff">int</span> nextId = 0<span style="color: #000000">;
- String sql </span>= "SELECT max(id) FROM question"<span style="color: #000000">;
- nextId </span>=<span style="color: #000000"> jdbcTemplete.getCountAll(sql);
- </span><span style="color: #0000ff">return</span><span style="color: #000000"> nextId;
- }
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> addUpdate(<span style="color: #0000ff">final</span><span style="color: #000000"> Question question) {
- String sql </span>= "INSERT INTO question(question_status) VALUES (?)"<span style="color: #000000">;
- jdbcTemplete.insert(sql, </span><span style="color: #0000ff">new</span> JDBCAbstractCallback<Question><span style="color: #000000">() {
- @Override
- </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setParams(PreparedStatement stmt) <span style="color: #0000ff">throws</span><span style="color: #000000"> SQLException {
- stmt.setInt(</span>1<span style="color: #000000">, question.getQuestion_status());
- }
- });
- }
- }</span>
JDBCtemplete 模板
标签:value else ret tco one edm acl cal where