第三章 生成POJO对象和表映射
上章我们已经解释了SSH框架的搭建,现在我们开始在搭建好的SSH框架下进行开发。
首先我们要建好需求中定义的数据库,并生成对应的POJO对象以及对象和表的映射文件。
建表脚本
1. 建立数据库
Create database demodb
2. 用户表(TUSER)
CREATE TABLE `demodb`.`TUSER` (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`USER_NAME` VARCHAR(10) NOT NULL,
`NAME` VARCHAR(30) NOT NULL,
`EMAIL` VARCHAR(50),
`PHONE` VARCHAR(20),
`PASSWORD` VARCHAR(10) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE INDEX `Index_2`(`USER_NAME`)
)
3. 商品表(SHOP)
CREATE TABLE `demodb`.`SHOP` (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(50) NOT NULL,
`PRICE` DOUBLE NOT NULL DEFAULT 0.0,
`SIMPLE_DESC` VARCHAR(500) NOT NULL,
`ALL_DESC` TEXT NOT NULL,
PRIMARY KEY (`ID`)
)
4. 订单主表(TORDER)
CREATE TABLE `demodb`.`TORDER` (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`USER_ID` INTEGER UNSIGNED NOT NULL,
`ORDER_TIME` DATETIME NOT NULL,
`R_ADDRESS` VARCHAR(200) NOT NULL,
`R_NAME` VARCHAR(50) NOT NULL,
`R_PHONE` VARCHAR(20) NOT NULL,
PRIMARY KEY (`ID`),
CONSTRAINT `FK_ORDER_1` FOREIGN KEY `FK_ORDER_1` (`USER_ID`)
REFERENCES `tuser` (`ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
5. 订单详细表(ORDER_DETAIL)
CREATE TABLE `demodb`.`ORDER_DETAIL` (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`ORDER_ID` INTEGER UNSIGNED NOT NULL,
`SHOP_ID` INTEGER UNSIGNED NOT NULL,
`AMOUNT` INTEGER UNSIGNED NOT NULL DEFAULT 1,
`S_PRICE` DOUBLE NOT NULL DEFAULT 0.0,
`A_PRICE` DOUBLE NOT NULL DEFAULT 0.0,
PRIMARY KEY (`ID`),
CONSTRAINT `FK_ORDER_DETAIL_1` FOREIGN KEY `FK_ORDER_DETAIL_1` (`ORDER_ID`)
REFERENCES `torder` (`ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `FK_ORDER_DETAIL_2` FOREIGN KEY `FK_ORDER_DETAIL_2` (`SHOP_ID`)
REFERENCES `shop` (`ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
6. 日志表(LOG)
CREATE TABLE `demodb`.`LOG` (
`ID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`USER_ID` INTEGER UNSIGNED,
`OPERATE` VARCHAR(50) NOT NULL,
`TIME` DATETIME NOT NULL,
`SHOP_ID` INTEGER UNSIGNED,
PRIMARY KEY (`ID`),
CONSTRAINT `FK_LOG_1` FOREIGN KEY `FK_LOG_1` (`USER_ID`)
REFERENCES `tuser` (`ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `FK_LOG_2` FOREIGN KEY `FK_LOG_2` (`SHOP_ID`)
REFERENCES `shop` (`ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
7.
用MyEclipse Hibernate插件,生成初步的POJO对象和相关的映射xml文件
1. 在数据库中,建立一个数据库demoDB,运行上面的数据库脚本,在demoDB建立项目需要的表
2. 在MyEclipse中,配置数据源
i) window->open perspective->MyEclipse Database Explorer
ii) 在DB Browser窗口中,点击鼠标右键,选择new,出现如下窗口
如上图填写数据库连接的配置 , 点 Finish 按钮