其他分享
首页 > 其他分享> > 段对第一阶段冲刺3总结

段对第一阶段冲刺3总结

作者:互联网

今天冲刺任务就是完成dao层的编写

package com.supercook.dao;

import com.supercook.dButils.JdbcUtils;
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 class BaseDao {

    //使用dButils操作数据库
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * update()
用来执行:Insert\Update\Delete语句
    
*
     * @return
如果返回-1,说明执行失败<br/>返回其他表所示影响的行数
    
*/
   
public int update(String sql,Object... args){
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.update(connection, sql,args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(connection);
        }
        return -1;
    }

    /**
     *
查询返回一个JavaBeansql语句
    
*
     * @param
type 返回对象的类型
    
* @param sql 执行的sql语句
    
* @param args sql对应的参数值
    
* @param <T> 返回的类型的泛型
    
* @return
    
*/
   
public <T> T queryForOne(Class<T> type,String sql,Object... args){
        Connection con = JdbcUtils.getConnection();

        try {
            return queryRunner.query(con,sql,new BeanHandler<T>(type),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con);
        }
        return null;
    }

    /**
     *
查询返回多个JavaBeansql语句
    
*
     * @param
type 返回对象的类型
    
* @param sql 执行的sql语句
    
* @param args sql对应的参数值
    
* @param <T> 返回的类型的泛型
    
* @return
    
*/
   
public <T> List<T> queryForList(Class<T> type, String sql, Object... args){
        Connection con = JdbcUtils.getConnection();
        System.out.println(con);
        try {
            return queryRunner.query(con,sql,new BeanListHandler<T>(type),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(con);
        }
        return null;
    }

    /**
     *
执行返回一行一列的sql语句
    
* @param sql   执行的sql语句
    
* @param args  sql对应的参数值
    
* @return
     
*/
   
public Object queryForSingleValue(String sql,Object... args){

        Connection conn = JdbcUtils.getConnection();

        try {
            return queryRunner.query(conn,sql,new ScalarHandler(),args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn);
        }
        return null;
    }
}

 

 

package com.supercook.dao;
import com.supercook.bean.Food;
import com.supercook.bean.Food2;
import com.supercook.bean.Windows;

import java.util.List;


public interface FoodDao {
    /**
     *
根据food名查询food信息
    
*
     * @param
foodname food
    
* @return 如果返回null, 说明没有这个food。反之亦然
    
*/
   
public List<Food> queryxiaochi(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querynoodle(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> queryrice(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querypastry(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querydish(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querysimplesnack(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querydrink(String foodname);

    /**
     *
根据food名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querysoup(String foodname);

    /**
     *
根据kind名查询food信息
    
* @param kind food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food> querykind(String kind);

    /**
     *
根据foodname名查询food信息
    
* @param foodname food
    
* @return 如果返回null,说明没有这个food。反之亦然
    
*/
   
public List<Food2> search(String foodname);

    public int dianzan(String name);

    public int dianzan1(String name);

    public Windows querysrc(String name);
}

 

 

package com.supercook.dao;

public interface UserDao {

    public int adduser(String username,String password,String number,String email,String sex,String age,String weight);

    public int login(String name,String paw);

}

 

 

package com.supercook.dao;

import com.supercook.bean.Food;
import com.supercook.bean.Food2;
import com.supercook.bean.Windows;
import com.supercook.dButils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl extends BaseDao implements FoodDao {

    @Override
    public List<Food> queryxiaochi(String foodname) {
        String sql = "select * from food join windows on food.position=windows.WPosition where foodname = ? and kind = '小吃'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querynoodle(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '面食'";
        return  queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> queryrice(String foodname) {
        String sql = "select * from food join windows on food.position=windows.WPosition where foodname = ? and kind = '米饭'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querypastry(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '面点'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querydish(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '面食配菜'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querysimplesnack(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '中式简餐'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querydrink(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '饮品'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querysoup(String foodname) {
        String sql = "select * from food where foodname = ? and kind = '汤'";
        return queryForList(Food.class,sql,foodname);
    }

    @Override
    public List<Food> querykind(String kind) {
        String sql = "select * from food where kind = ?";
        return queryForList(Food.class,sql,kind);
    }

    @Override
    public List<Food2> search(String name) {
        String sql = "select * from food join windows on food.position=windows.WPosition where foodname like '%"+name+"%' or WName like '%"+name+"%'";
        Connection connection = JdbcUtils.getConnection();
        Statement statement = null;
        ResultSet rs = null;
        List<Food2> list = new ArrayList<>();

        try {
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            Food2 kc=null;
            while(rs.next())
            {
                int WId=rs.getInt("WId");
                String WPosition=rs.getString("WPosition");
                String WName=rs.getString("WName");
                int id=rs.getInt("id");
                String foodname=rs.getString("foodname");
                double price=rs.getDouble("price");
                String kind=rs.getString("kind");
                //String img=rs.getString("img");
                int score=rs.getInt("score");
                kc =new Food2(WId,WPosition,price,WName,id,kind,foodname,score);
                System.out.println(kc);
                list.add(kc);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(connection);
        }
        return list;
    }

    @Override
    public int dianzan(String name) {
        String sql = "update windows set score=score+1 where WName='"+name+"'";
        Connection connection = JdbcUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        int flag = 0;

        try {
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);
            if(i>0){
                System.out.println("点赞成功");
            }else {
                System.out.println("点赞失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(connection);
        }
        return flag;
    }

    @Override
    public int dianzan1(String name) {
        String sql = "update windows set score=score+1 where WPosition='"+name+"'";
        Connection connection = JdbcUtils.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        int flag = 0;

        try {
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);
            if(i>0){
                System.out.println("点赞成功");
            }else {
                System.out.println("点赞失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(connection);
        }
        return flag;
    }

    @Override
    public Windows querysrc(String name) {
        String sql = "select * from windows where WPosition = ?";
        return queryForOne(Windows.class,sql,name);
    }
}

 

 

package com.supercook.dao;

import com.supercook.bean.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao{
    @Override
    public int adduser(String username, String password, String number, String email, String sex, String age, String weight) {
        String sql = "insert into user(username,password,number,email,sex,age,weight)values('"+username+"','"+password+"','"+number+"','"+email+"','"+sex+"','"+age+"','"+weight+"')";
        return update(sql,username,password,number,email,sex,age,weight);
    }

    @Override
    public int login(String name, String paw) {
        int flag = 0;
        String sql = "select * from user";
        List<User> users = queryForList(User.class, sql);
        for(int i = 0;i < users.size();i++){
            if(users.get(i).getUsername().equals(name)&&users.get(i).getPassword().equals(paw)){
                flag = 1;
            }
        }
        if(flag == 1){
            System.out.println("登陆成功");
        }else {
            System.out.println("登陆失败");
        }
        return flag;
    }
}

 

标签:总结,return,String,food,冲刺,public,sql,foodname,第一阶段
来源: https://www.cnblogs.com/20203923rensaihang/p/16373348.html