首页 > 代码库 > JavaWeb学习入门之——图书馆管理系统开发(Hibernate学习1)
JavaWeb学习入门之——图书馆管理系统开发(Hibernate学习1)
最近看了看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>
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 }
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 }
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 }
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 }
今天先写这么多,由于是刚学。不敢保证这样做就一定正确,大家仅作参考就行了。
JavaWeb学习入门之——图书馆管理系统开发(Hibernate学习1)