java: framework from BLL、DAL、IDAL、MODEL、Factory, using MySql 8.0

sql script:

drop table BookKindList;
#書目錄
create table BookKindList
(
    BookKindID INT NOT NULL AUTO_INCREMENT, #自動增加
    BookKindName nvarchar(500) not null,
    BookKindParent int null,
   PRIMARY KEY(BookKindID)  #主鍵
);

  

 
#查詢一條
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Select_BookKindList` (IN param1 INT)
BEGIN
        SELECT * FROM BookKindList WHERE BookKindID = param1;
END $$
DELIMITER ;
 
#插入一條
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindList` (IN param1Name NVarChar(1000),IN param1Parent Int)
BEGIN
        insert into BookKindList(BookKindName,BookKindParent) values(param1Name,param1Parent);
END $$
DELIMITER ;
 
#插入一條返回值
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindOut` (IN param1Name NVarChar(1000),IN param1Parent Int,OUT ID INT)
BEGIN
     IF NOT EXISTS (SELECT * FROM BookKindList WHERE BookKindName=param1Name) then   #如果存在相同的記錄,不添加
        INSERT INTO BookKindList (BookKindName,BookKindParent)VALUES(param1Name ,param1Parent);
        #set ID=Last_insert_id()
        SELECT LAST_INSERT_ID() into ID;
      end if;
END $$
DELIMITER ;

  

MODEL:

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:實體類,連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */


package Geovin.Model;


/**
 * 實體類
 *@author geovindu  塗聚文 Geovin Du
 * @
 *
 * */
public class BookKind {
    //
    private int bookKindID;


    private String bookKindName;


    private int bookKindParent;
    /**
     * @param
     * @return  得到ID
     * */
    public int getBookKindID() {
        return bookKindID;
    }
    /**
     * @param bookKindID 設置輸入參數
     *
     * */
    public void setBookKindID(int bookKindID) {
        this.bookKindID = bookKindID;
    }
    /**
     * @param
     * @return 得到目錄名稱
     * */
    public String getBookKindName() {
        return bookKindName;
    }
    /**
     * @param bookKindName 設置輸入參數
     *
     * */
    public void setBookKindName(String bookKindName) {
        this.bookKindName = bookKindName;
    }
    /**
     * @param
     * @return 得到父節點的值
     * */
    public int getBookKindParent() {
        return bookKindParent;
    }
    /**
     * @param bookKindParent 設置輸入參數
     *
     * */
    public void setBookKindParent(int bookKindParent) {
        this.bookKindParent = bookKindParent;
    }

}

  

DAL:

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:DAL數據訪問層  數據業務層,連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Geovin Du
 * 2021-12-15 修改:塗聚文
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKindDAL.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver  com.mysql.cj.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */

//#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
package Geovin.DAL;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.sql.*;
import java.lang.reflect.Parameter;

import Geovin.Interface.BookKindInterface;
import Geovin.UtilitieDB.MySqlHelper;
import Geovin.Model.*;
import Geovin.Interface.*;
import Geovin.Factory.*;

/**
 *#parse("File Header.java")
 * @apiNote 數據業務層
 * @deprecated
 * @Description
 * @projectName
 * @author  geovindu 塗聚文 Geovin Du
 * @date
 * @version 1.0
 *
 */

public class BookKindDAL implements BookKindInterface {


    /**
     * @param info 輸入一個實體
     * @return 返回int 1 是否插入一條記錄
     * @Description 添加一條記錄
     * */
    public int Add(BookKind info)
    {
        int ok=0;
        ResultSet resultSet = null;
        try
        {
            String sql = "{call proc_Insert_BookKindList(?,?)}";
            String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) };
            MySqlHelper.CallProc(sql,parameters);
            ok=1;
        }
        catch (Exception exception)
        {
            ok=0;
            exception.printStackTrace();
        }
        finally {
            MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection());
        }


        return ok;

    }

    /**
     * @param info  輸入實體
     * @return 返回值
     *
     * */
    public int AddOut(BookKind info)
    {
        int ok=0;
        ResultSet resultSet = null;
        try
        {
            String sql = "{call proc_Insert_BookKindOut(?,?,?)}";
            String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) };
            Integer[] out = { Types.INTEGER };
            CallableStatement cs=(CallableStatement)MySqlHelper.CallProcOutInt(sql,parameters,out);
            ok= cs.getInt(3);
            // ok=out[0]; //不是添加的ID值
        }
        catch (Exception exception)
        {
            ok=0;
            exception.printStackTrace();
        }
        finally {
            MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection());
        }
        return ok;

    }
    /**
     * 添加返回值
     * @param info  輸入實體
     * @param outValue 返回值
     * @return 返回值
     *
     * */
    public int AddOut(BookKind info,OutValue outValue)
    {
        int ok=0;
        ResultSet resultSet = null;
        try
        {
            String sql = "{call proc_Insert_BookKindOut(?,?,?)}";
            String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) };
            Integer[] out = { Types.INTEGER };
            CallableStatement cs=(CallableStatement)MySqlHelper.CallProcOutInt(sql,parameters,out);
            outValue.setIntValue(cs.getInt(3));
            ok=cs.getInt(3);
           
        }
        catch (Exception exception)
        {
            ok=0;
            exception.printStackTrace();
        }
        finally {
            MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection());
        }
        return ok;

    }
    /**
     *添加返回值
     * @param info 
     * @return 
     *
     * */
    public int AddOut2(BookKind info)
    {
        int ok=0;
        ResultSet resultSet = null;
        try
        {
            String sql = "{call proc_Insert_BookKindOut(?,?,?)}";

            String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()),""};
            Integer out =Types.INTEGER;
            MySqlHelper.callProcInputAndOutPutString(sql,parameters);

            ok=out; //不是添加的ID值
        }
        catch (Exception exception)
        {
            ok=0;
            exception.printStackTrace();
        }
        finally {
            MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection());
        }
        return ok;

    }


    /**
     * #parse("更新記錄")
     * @param info  輸入實體
     * @return 返回參數
     *
     * */
    public int Update(BookKind info) {

        int ok=0;
        ResultSet resultSet = null;
        try
        {
            String sql = "{call proc_Update_BookKindList(?,?,?)}";
            String[] parameters = {String.valueOf(info.getBookKindID()), info.getBookKindName(), String.valueOf(info.getBookKindParent()) };
            MySqlHelper.CallProc(sql,parameters);
            ok=1; //
        }
        catch (Exception exception)
        {
            ok=0;
            exception.printStackTrace();
        }
        finally {
            MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection());
        }
        return ok;



    }
    /**
     * @param id
     * @return BookKind 指定查找的ID記錄
     * @author geovindu
     * @date 2021-12-20
     * */
    public BookKind SelectSQLBookKindInfo(String id)
    {
        BookKind info=null;
        String sql = "SELECT * FROM BookKindList where BookKindID=?";
        String[] parameters = { id };
        try {
            info=new BookKind();
            ResultSet rs = MySqlHelper.DuexecuteQuery(sql, parameters);
            while (rs.next()) {

                info.setBookKindID(rs.getInt("BookKindID"));
                info.setBookKindName(rs.getString("BookKindName"));
                info.setBookKindParent(rs.getInt("BookKindParent"));

            }
            //return info;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MySqlHelper.close(MySqlHelper.getRs(), MySqlHelper.getPs(), MySqlHelper
                    .getConnection());
        }

        return info;
    }
    /**
     * @param
     * @return BookKind 所有記錄
     * @date 2021-12-20
     * @author geovindu
     * */
    public ArrayList<BookKind> SelectSQLBookKindAll()
    {

        ArrayList<BookKind> list=new ArrayList<BookKind>();

        String sql = "SELECT * FROM BookKindList";

        try {
            BookKind info=null;
            ResultSet rs = MySqlHelper.DuexecuteQuery(sql,null);
            while (rs.next()) {
                info=new BookKind();
                info.setBookKindID(rs.getInt("BookKindID"));
                info.setBookKindName(rs.getString("BookKindName"));
                info.setBookKindParent(rs.getInt("BookKindParent"));
                list.add(info);
            }
            //return info;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            MySqlHelper.close(MySqlHelper.getRs(), MySqlHelper.getPs(), MySqlHelper
                    .getConnection());
        }
        return list;
    }

}

  

 

IDAL:

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:Interface 接口層,連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKindInterface.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver  com.mysql.cj.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */


package Geovin.Interface;

import Geovin.Model.BookKind;

import java.util.ArrayList;

/**
 * #parse("接口")
 * @author geovindu 塗聚文 Geovin Du
 * @version 1.0
 * */

public interface BookKindInterface {


    /**
     * @param info
     * @return
     * */
    public int Add(BookKind info);
    /**
     *
     * @param info
     * @return
     *
     * */
    public int AddOut(BookKind info);
    /**
     *
     * @param info
     * @return
     *
     * */
    public int Update(BookKind info);
    /**
     *
     * @param id
     * @return
     *
     * **/
    public BookKind SelectSQLBookKindInfo(String id);

    /**
     *
     * @param
     * @return
     *
     * */
    public ArrayList<BookKind> SelectSQLBookKindAll();

}

  

Factory:

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:工廠層,抽象工廠 連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */


package Geovin.Factory;


import Geovin.DAL.BookKindDAL;
import Geovin.Interface.BookKindInterface;

/**
 * #parse("抽象工廠")
 * @author geovindu 塗聚文 Geovin Du
 * @version 1.0
 *
 * */

public class AbstractFactory {


    /**
     *
     *
     * */
    public static  BookKindInterface CreateBookKind()
    {
        BookKindInterface iBookKindInterface=new BookKindDAL();
        return  iBookKindInterface;
    }

}

  

BLL:

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:業務邏輯層,連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKind.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */


package Geovin.BLL;


import Geovin.Model.*;
import Geovin.Factory.AbstractFactory;
import Geovin.Interface.*;

import java.util.ArrayList;

/**
 * #parse("業務邏輯層")
 * @author geovindu 塗聚文 Geovin Du
 * @
 * */
public class BookKindBLL {

    private  static BookKindInterface dal=AbstractFactory.CreateBookKind();

    /**
     *
     * */
    public int Add(BookKind info)
    {
       return dal.Add(info);
    }
    /**
     *
     * */
    public int AddOut(BookKind info)
    {
        return dal.AddOut(info);
    }
    /**
     *
     * */
    public int Update(BookKind info)
    {
        return dal.Update(info);
    }
    /**
     *
     * */
    public BookKind SelectSQLBookKindInfo(String id)
    {
        return dal.SelectSQLBookKindInfo(id);
    }
    /**
     *
     * */
    public ArrayList<BookKind> SelectSQLBookKindAll()
    {
        return dal.SelectSQLBookKindAll();
    }
}

  

測試:

 //CustomerDAL dal=new CustomerDAL();
         //dal.SelectSQLCustomer("1");
        // BookKindDAL dal=new BookKindDAL();
        BookKindBLL dal=new BookKindBLL();
         BookKind info=dal.SelectSQLBookKindInfo("1");

         System.out.println("\t\n實體讀出:id-"+info.getBookKindID()+";類目名稱:"+info.getBookKindName()+";父節點ID:"+info.getBookKindParent());

         BookKind newinfo=new BookKind();

        newinfo.setBookKindID(5);
        newinfo.setBookKindName("聚文小說");
        newinfo.setBookKindParent(2);
         int ok=dal.Update(newinfo);
         if(ok>0) {
          System.out.println("更新記錄,ok"+String.valueOf(ok));
         }
        else
        {
        System.out.println("更新不成功,no");
        }
         info=new BookKind();
        info=dal.SelectSQLBookKindInfo("5");
        System.out.println("\t\n"+info.getBookKindName());

  

 java Out Parameters

 from: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-a-stored-procedure-with-output-parameters?view=sql-server-ver15

https://stackoverflow.com/questions/4455693/how-to-create-in-out-or-out-parameters-in-java

https://mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/

http://javacamp.org/javavscsharp/outparam.html 這個沒在有啥參考價值

https://stackoverflow.com/questions/50713653/multiple-out-parameters-in-simplejdbccall

http://kscodes.com/java/callablestatement-example-with-in-out-parameters/

https://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html

https://www.codejava.net/java-se/jdbc/jdbc-examples-for-calling-stored-procedures-mysql

/*
 * 版權所有 2021 塗聚文有限公司
 * 許可信息查看:
 * 描述:實體層  ,連接MySQL
 *
 * 歷史版本:  JDK 14.02
 * 數據庫:My SQL 8.0
 * IDE: IntelliJ IDEA 2021.2.3
 * OS: Windows 10 x64
 * 2021-12-12 創建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口類
 * 2021-12-15 修改者:Geovin Du
 * 生成API幫助文檔的指令:
 *javadoc - -encoding Utf-8 -d apidoc BookKindDAL.java
 * 配置文件:
 * driver=com.mysql.jdbc.Driver  com.mysql.cj.jdbc.Driver
 *url=jdbc\:mysql\://localhost\:3306/數據庫名稱
 *user=root
 *password=root
 *
 * */



package Geovin.Model;

/**
 * 用於返回值
 * @author geovindu  塗聚文 Geovin Du
 * @date 2021-12-20
 * **/
public class OutValue {

    private int intValue;
    /**
     * 得到值
     * @param
     * @return
     *
     * */
    public int getIntValue()
    {
        return  intValue;
    }
    /**
     * 設定值
     * @param intValue  輸入值
     * @param
     * */
    public  void setIntValue(int intValue )
    {
        this.intValue=intValue;
    }

    private String stringValue;
    /**
     * 得到值
     * @param
     * @return
     *
     * */
    public String getStringValue()
    {
        return stringValue;
    }
    /**
     * 設定值
     * @param stringValue  設定值
     * @param
     * */
    public void setStringValue(String stringValue)
    {
        this.stringValue=stringValue;
    }


}

  

/**
     * 存儲過返回值
     * @param info  輸入實體類型值
     * @return  返回添加的ID的值
     * @
     * */
    public  int proc(BookKind info)
    {
        int out=0;
        Connection connection=null;
        CallableStatement cstm =null;

        try {
            Class.forName(DRIVER_CLASS);
        }
        catch (ClassNotFoundException exception)
        {
            exception.printStackTrace();
        }
        try {
             connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            String sql = "{CALL proc_Insert_BookKindOut(?,?,?)}"; //調用存儲過程
            cstm = connection.prepareCall(sql); //實例化對象cstm,執行存儲過程
            cstm.setString(1, info.getBookKindName()); //存儲過程輸入參數
            //也是可以的
            //cstm.setString(2,String.valueOf(info.getBookKindParent()));//可以判斷什麼類型,進行什麼類型轉換
            //
            cstm.setInt(2,info.getBookKindParent());
            cstm.registerOutParameter(3, Types.INTEGER); // 設置返回值類型 即返回值
            cstm.execute(); // 執行存儲過程
            System.out.println(cstm.getInt(3));
            out=cstm.getInt(3);
            cstm.close();
            connection.close();

        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();

        }
        return out;

    }
    /**
     * 得到返回的值
     * @param info  輸入參數
     * @param outValue  得到返回的值
     * @return  返回值
     * */
    public  int proc(BookKind info, OutValue outValue)
    {
        int out=0;
        Connection connection=null;
        CallableStatement cstm =null;

        try {
            Class.forName(DRIVER_CLASS);
        }
        catch (ClassNotFoundException exception)
        {
            exception.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            String sql = "{CALL proc_Insert_BookKindOut(?,?,?)}"; //調用存儲過程
            cstm = connection.prepareCall(sql); //實例化對象cstm,執行存儲過程
            cstm.setString(1, info.getBookKindName()); //存儲過程輸入參數
            //也是可以的
            //cstm.setString(2,String.valueOf(info.getBookKindParent()));//可以判斷什麼類型,進行什麼類型轉換
            //
            cstm.setInt(2,info.getBookKindParent());
            cstm.registerOutParameter(3, Types.INTEGER); // 設置返回值類型 即返回值
            cstm.execute(); // 執行存儲過程
            System.out.println(cstm.getInt(3));
            out=cstm.getInt(3);
            outValue.setIntValue(cstm.getInt(3));
            cstm.close();
            connection.close();

        }
        catch (SQLException sqlException)
        {
            sqlException.printStackTrace();

        }
        return out;

    }

 測試:

 OutValue value=new OutValue();

        BookKindDAL dal=new BookKindDAL();
        BookKind info=new BookKind();
        //info.setBookKindName("社會科學");
        //info.setBookKindName("自然科學");
        info.setBookKindName("文學");
        info.setBookKindParent(2);
       // int ok= dal.proc(info);
        int ok=dal.proc(info,value);

         if(ok>0) {
             System.out.println("ok:"+ok+",out:"+value.getIntValue());
         }
         else {
             System.out.println("no");
         }

  

 

如何判斷值類型

變量的值類型

一個類的屬性的值類型

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章