javaweb中dao层如何使用

项目具体目录如下:
javaweb中dao层如何使用_第1张图片
Basedao的内容如下:

package com.monkeyshop.dao;

import java.sql.*;

public class Basedao {
    static{
        //加载驱动 mysql8.0及以上版本加载方式
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //都声明为静态方法是为了防止出错
    public static Connection getconn(){
        //创建一个链接对象
        Connection conn = null;
        try {
                                                                         //monkeyshop 数据库名                           用户名   密码  其余的内容差不多是固定的
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/monkeyshop?useSSL=false&serverTimezone=UTC","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  conn;
    }

    public static  int exectuIUD(String sql,Object[] params){
        int count = 0;
        //获取连接对象
        Connection conn = Basedao.getconn();
        //准备SQL
        PreparedStatement ps= null;

        try {
            ps = conn.prepareStatement(sql);
            for(int i=0; i<params.length; i++){
                ps.setObject(i+1,params[i]);
            }
            count = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Basedao.closeall(null,ps,conn); //释放资源吧  结果集 声明 连接
        }
        return  count;
    }


    public static void closeall(ResultSet rs,PreparedStatement ps, Connection conn){
        try {
            if(rs != null)
              rs.close();
            if(ps!=null){
                ps.close();
            }
            if(conn!=null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

sevice层例如购物车代码

package com.monkeyshop.service;

import com.monkeyshop.dao.Basedao;
import com.monkeyshop.entity.MONKEY_CART;
import com.monkeyshop.entity.MONKEY_PRODUCT;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

public class MONKEY_CARTDao {
    public static int insert(MONKEY_CART cart) {
        String sql = "insert into lmonkey_cart values(null,?,?,?,?,?,?,?,?)";

        Object[] params = {
                cart.getCart_p_filename(),
                cart.getCart_p_name(),
                cart.getCart_p_price(),
                cart.getCart_quantity(),
                cart.getCart_p_stock(),
                cart.getCart_p_id(),
                cart.getCart_u_id(),
                cart.getCart_valid()
        };
        return  Basedao.exectuIUD(sql,params);
    }

    public static ArrayList<MONKEY_CART> getCart(String uid) {
        ArrayList<MONKEY_CART> shoplist = new ArrayList<MONKEY_CART>();

        ResultSet rs = null;
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;

        String sql = "select * from lmonkey_cart where cart_u_id=? and cart_valid=1 order by cart_id desc";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,uid);
            rs = ps.executeQuery();
            while (rs.next()){
                MONKEY_CART c = new MONKEY_CART(
                        rs.getInt("cart_id"),
                        rs.getString("cart_p_filename"),
                        rs.getString("cart_p_name"),
                        rs.getInt("cart_p_price"),
                        rs.getInt("cart_quantity"),
                        rs.getInt("cart_p_stock"),
                        rs.getInt("cart_p_id"),
                        rs.getString("cart_u_id"),
                        rs.getInt("cart_valid")
                );
                shoplist.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Basedao.closeall(rs,ps,conn);
        }

        return shoplist;
    }

    public static MONKEY_CART getCartShop(String uid, String pid) {
        MONKEY_CART c = null;

        ResultSet rs = null;
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;

        String sql = "select * from lmonkey_cart where cart_u_id=? and cart_p_id=? and cart_valid=1 order by cart_id desc";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,uid);
            ps.setInt(2,Integer.parseInt(pid));
            rs = ps.executeQuery();
            while (rs.next()){
                  c = new MONKEY_CART(
                        rs.getInt("cart_id"),
                        rs.getString("cart_p_filename"),
                        rs.getString("cart_p_name"),
                        rs.getInt("cart_p_price"),
                        rs.getInt("cart_quantity"),
                        rs.getInt("cart_p_stock"),
                        rs.getInt("cart_p_id"),
                        rs.getString("cart_u_id"),
                        rs.getInt("cart_valid")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Basedao.closeall(rs,ps,conn);
        }

        return c;
    }
    public static MONKEY_CART getCartShop(String id) {
        MONKEY_CART c = null;

        ResultSet rs = null;
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;

        String sql = "select * from lmonkey_cart where cart_id=? and cart_valid=1 order by cart_id desc";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                c = new MONKEY_CART(
                        rs.getInt("cart_id"),
                        rs.getString("cart_p_filename"),
                        rs.getString("cart_p_name"),
                        rs.getInt("cart_p_price"),
                        rs.getInt("cart_quantity"),
                        rs.getInt("cart_p_stock"),
                        rs.getInt("cart_p_id"),
                        rs.getString("cart_u_id"),
                        rs.getInt("cart_valid")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Basedao.closeall(rs,ps,conn);
        }

        return c;
    }

    public static int updatenum(int cart_id, int countnew) {
        String sql = "update lmonkey_cart set cart_quantity=? where cart_id=?";
        Object[] params ={countnew,cart_id};

        return Basedao.exectuIUD(sql,params);
    }

    public static int getDeleteDD(int id) {
        String sql = "delete from lmonkey_cart where cart_id=?";
        Object[] params = {id};
        return Basedao.exectuIUD(sql,params);
    }
}

你可能感兴趣的:(java和Spring全家桶,java,mysql,sql)