最近看了看JavaWeb的書籍,才感覺到大二時候學的JavaWeb才僅僅只是個入門。最尷尬的當初還沒咋學一直在看.NET,現在看起來JavaWeb,各種框架各種頭疼啊。看了幾個例子之後覺得,還是自己動手做個項目試試。邊做邊學才能學的快一點。做的時候用博客園記錄下過程,省得以後忘。之所以選圖書館管理系 ...
最近看了看JavaWeb的書籍,才感覺到大二時候學的JavaWeb才僅僅只是個入門。最尷尬的當初還沒咋學一直在看.NET,現在看起來JavaWeb,各種框架各種頭疼啊。看了幾個例子之後覺得,還是自己動手做個項目試試。邊做邊學才能學的快一點。做的時候用博客園記錄下過程,省得以後忘。之所以選圖書館管理系統呢,是因為做過幾個課程設計。對它的需求還是比較瞭解一點。現在通用的框架模式都是MVC,我在學的時候想先從Model層開始學。於是就先選Hibernate開始。
一、資料庫
先簡單設計幾張表,圖書表,讀者,管理員,借閱記錄表。以下是建表的語句
1 /* 2 Navicat MySQL Data Transfer 3 4 Source Server : root 5 Source Server Version : 50513 6 Source Host : localhost:3306 7 Source Database : db_librarysystem 8 9 Target Server Type : MYSQL 10 Target Server Version : 50513 11 File Encoding : 65001 12 13 Date: 2017-06-11 13:00:32 14 */ 15 16 SET FOREIGN_KEY_CHECKS=0; 17 18 -- ---------------------------- 19 -- Table structure for tb_administrator 20 -- ---------------------------- 21 DROP TABLE IF EXISTS `tb_administrator`; 22 CREATE TABLE `tb_administrator` ( 23 `adminid` int(11) NOT NULL, 24 `name` varchar(20) NOT NULL COMMENT '管理員登錄名', 25 `pwd` varchar(15) NOT NULL COMMENT '密碼', 26 `createdate` varchar(50) NOT NULL COMMENT '管理員創建時間', 27 PRIMARY KEY (`adminid`) 28 ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 29 30 -- ---------------------------- 31 -- Records of tb_administrator 32 -- ---------------------------- 33 34 -- ---------------------------- 35 -- Table structure for tb_book 36 -- ---------------------------- 37 DROP TABLE IF EXISTS `tb_book`; 38 CREATE TABLE `tb_book` ( 39 `bid` int(11) NOT NULL COMMENT '圖書編號', 40 `name` varchar(50) NOT NULL COMMENT '書名', 41 `ISBN` varchar(20) NOT NULL, 42 `author` varchar(20) NOT NULL COMMENT '圖書作者', 43 `Publisher` varchar(50) NOT NULL COMMENT '圖書出版社', 44 `BType` varchar(20) NOT NULL COMMENT '圖書類型', 45 `BPrice` decimal(10,0) NOT NULL COMMENT '圖書價格', 46 `BCount` int(11) NOT NULL COMMENT '圖書總數', 47 `Bstate` varchar(20) NOT NULL COMMENT '圖書狀態(在架,已外借,丟失)', 48 PRIMARY KEY (`bid`) 49 ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 50 51 -- ---------------------------- 52 -- Records of tb_book 53 -- ---------------------------- 54 55 -- ---------------------------- 56 -- Table structure for tb_borrow 57 -- ---------------------------- 58 DROP TABLE IF EXISTS `tb_borrow`; 59 CREATE TABLE `tb_borrow` ( 60 `borrowid` int(11) NOT NULL, 61 `rid` int(11) NOT NULL, 62 `bid` int(11) NOT NULL, 63 `BorrwTime` varchar(50) NOT NULL COMMENT '借書時間', 64 `ReturnTime` varchar(50) DEFAULT NULL COMMENT '還書時間', 65 `ISReturn` varchar(5) NOT NULL COMMENT '是否歸還', 66 `NoReturn` varchar(5) DEFAULT NULL COMMENT '歸還異常', 67 PRIMARY KEY (`borrowid`), 68 KEY `borrow_bid` (`bid`), 69 KEY `borrow_rid` (`rid`), 70 CONSTRAINT `borrow_bid` FOREIGN KEY (`bid`) REFERENCES `tb_book` (`bid`) ON DELETE CASCADE ON UPDATE CASCADE, 71 CONSTRAINT `borrow_rid` FOREIGN KEY (`rid`) REFERENCES `tb_reader` (`rid`) ON DELETE CASCADE ON UPDATE CASCADE 72 ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 73 74 -- ---------------------------- 75 -- Records of tb_borrow 76 -- ---------------------------- 77 78 -- ---------------------------- 79 -- Table structure for tb_reader 80 -- ---------------------------- 81 DROP TABLE IF EXISTS `tb_reader`; 82 CREATE TABLE `tb_reader` ( 83 `rid` int(11) NOT NULL AUTO_INCREMENT COMMENT '讀者編號', 84 `name` varchar(20) NOT NULL DEFAULT '' COMMENT '讀者姓名', 85 `sex` varchar(2) NOT NULL DEFAULT '' COMMENT '性別', 86 `idcard` varchar(11) NOT NULL DEFAULT '' COMMENT '證件號', 87 `rmoney` double NOT NULL DEFAULT '0' COMMENT '欠費金額', 88 `rstate` varchar(10) NOT NULL DEFAULT '' COMMENT '讀者狀態', 89 `rcount` int(11) NOT NULL DEFAULT '3' COMMENT '可借閱數', 90 `pwd` varchar(15) NOT NULL DEFAULT '123456' COMMENT '登陸密碼', 91 PRIMARY KEY (`rid`) 92 ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 93 94 -- ---------------------------- 95 -- Records of tb_reader 96 -- ----------------------------資料庫建表語句
二、搭建Hibernate環境
1.先用MyEclipse建立一個名為LibrarySystem的Web項目。然後讓項目的字元編碼設置為UTF-8(這一步我老是忘,結果後面出現中文亂碼了才想起來)。
2.引入Hibernate所需要的包
3.引入Hibernate配置文件(放到src目錄下)
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 8 <session-factory> 9 <!-- 方言 --> 10 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 11 <!-- 資料庫連接 --> 12 <property name="connection.url">jdbc:mysql://localhost:3306/db_LibrarySystem</property> 13 <!-- 資料庫連接用戶名 --> 14 <property name="connection.username">root</property> 15 <!-- 資料庫連接密碼 --> 16 <property name="connection.password">sasasa</property> 17 <!-- 資料庫驅動 --> 18 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 19 <!-- 列印SQL語句 --> 20 <property name="show_sql">true</property> 21 <!-- 自動建表 --> 22 <property name="hbm2ddl.auto">update</property> 23 <!-- 映射文件 --> 24 <mapping resource="com/lby/entity/Reader.hbm.xml" /> 25 <mapping resource="com/lby/entity/Borrow.hbm.xml" /> 26 <mapping resource="com/lby/entity/Administrator.hbm.xml" /> 27 <mapping resource="com/lby/entity/Book.hbm.xml" /> 28 </session-factory> 29 30 </hibernate-configuration>hibernate.cfg.xml
4.建立項目的包結構
三、創建實體類
說起創建實體類,剛開始我還不知MyEclipse有自動生成Get,Set方法的快捷方式。我開始還一個一個大,後來感覺累了去網上搜一下才發現真有。以前不知道真是悲催。
自動生成Get,Set方法,在代碼界面上右擊彈出菜單里選source->Generate Getters and Setters
選中要生成的變數,點擊OK就生成了。
1 package com.lby.entity; 2 3 /** 4 * @author Administrator 5 * 6 */ 7 public class AdministratorInfo { 8 private Integer adminid;//管理員編號 9 private String name;//管理員姓名 10 private String pwd;//登錄密碼 11 private String createdate;//創建時間 12 public Integer getAdminid() { 13 return adminid; 14 } 15 public void setAdminid(Integer adminid) { 16 this.adminid = adminid; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public String getPwd() { 25 return pwd; 26 } 27 public void setPwd(String pwd) { 28 this.pwd = pwd; 29 } 30 public String getCreatedate() { 31 return createdate; 32 } 33 public void setCreatedate(String createdate) { 34 this.createdate = createdate; 35 } 36 37 }AdministratorInfo
1 package com.lby.entity; 2 3 public class BookInfo { 4 private Integer bid;//圖書編號 5 private String name;//書名 6 private String isbn;//索書號 7 private String author;//作者 8 private String publisher;//出版社 9 private String btype;//圖書類型 10 private Double bprice;//圖書價格 11 private Integer bcount;//圖書數量 12 private String bstate;//圖書狀態 13 public Integer getBid() { 14 return bid; 15 } 16 public void setBid(Integer bid) { 17 this.bid = bid; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getIsbn() { 26 return isbn; 27 } 28 public void setIsbn(String isbn) { 29 this.isbn = isbn; 30 } 31 public String getAuthor() { 32 return author; 33 } 34 public void setAuthor(String author) { 35 this.author = author; 36 } 37 public String getPublisher() { 38 return publisher; 39 } 40 public void setPublisher(String publisher) { 41 this.publisher = publisher; 42 } 43 public String getBtype() { 44 return btype; 45 } 46 public void setBtype(String btype) { 47 this.btype = btype; 48 } 49 public Double getBprice() { 50 return bprice; 51 } 52 public void setBprice(Double bprice) { 53 this.bprice = bprice; 54 } 55 public Integer getBcount() { 56 return bcount; 57 } 58 public void setBcount(Integer bcount) { 59 this.bcount = bcount; 60 } 61 public String getBstate() { 62 return bstate; 63 } 64 public void setBstate(String bstate) { 65 this.bstate = bstate; 66 } 67 68 }BookInfo
1 package com.lby.entity; 2 3 public class BorrowInfo { 4 private Integer borrowid;//借閱編號 5 private Integer rid;//讀者編號 6 private Integer bid;//圖書編號 7 private String borrwTime;//借書時間 8 private String returnTime;//還書時間 9 private String isreturn;//是否歸還 10 private String noReturn;//歸還異常 11 public Integer getBorrowid() { 12 return borrowid; 13 } 14 public void setBorrowid(Integer borrowid) { 15 this.borrowid = borrowid; 16 } 17 public Integer getRid() { 18 return rid; 19 } 20 public void setRid(Integer rid) { 21 this.rid = rid; 22 } 23 public Integer getBid() { 24 return bid; 25 } 26 public void setBid(Integer bid) { 27 this.bid = bid; 28 } 29 public String getBorrwTime() { 30 return borrwTime; 31 } 32 public void setBorrwTime(String borrwTime) { 33 this.borrwTime = borrwTime; 34 } 35 public String getReturnTime() { 36 return returnTime; 37 } 38 public void setReturnTime(String returnTime) { 39 this.returnTime = returnTime; 40 } 41 public String getIsreturn() { 42 return isreturn; 43 } 44 public void setIsreturn(String isreturn) { 45 this.isreturn = isreturn; 46 } 47 public String getNoReturn() { 48 return noReturn; 49 } 50 public void setNoReturn(String noReturn) { 51 this.noReturn = noReturn; 52 } 53 54 }BorrowInfo
1 package com.lby.entity; 2 3 public class ReaderInfo { 4 private Integer rid;//讀者編號 5 private String name;//讀者姓名 6 private String sex;//性別 7 private String idcard;//證件號 8 private Double rmoney;//欠費金額 9 private String rstate;//讀者狀態 10 private String rcount;//可借閱數 11 private String pwd;//登錄密碼 12 13 public Integer getRid() { 14 return rid; 15 } 16 public void setRid(Integer rid) { 17 this.rid = rid; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getSex() 26 { 27 return sex; 28 } 29 public void setSex(String sex) 30 { 31 this.sex=sex; 32 } 33 public String getIdcard() 34 { 35 return idcard; 36 } 37 public void setIdcard(String idcard) 38 { 39 this.idcard=idcard; 40 } 41 public Double getRmoney() 42 { 43 return rmoney; 44 } 45 public void setRmoney(Double rmoney) 46 { 47 this.rmoney=rmoney; 48 } 49 public String getRstate() 50 { 51 return rstate; 52 } 53 public void setRstate(String rstate) 54 { 55 this.rstate =rstate; 56 } 57 public String getRcount() 58 { 59 return rcount; 60 } 61 public void setRcount(String rcount) 62 { 63 this.rcount =rcount; 64 } 65 public String getPwd() 66 { 67 return pwd; 68 } 69 public void setPwd(String pwd) 70 { 71 this.pwd=pwd; 72 } 73 }ReaderInfo
今天先寫這麼多,由於是剛學。不敢保證這樣做就一定正確,大家僅作參考就行了。