细心的读者可能已发现“Web应用开发50天”改为了“Web应用开发20天”,之所以这样调整是因为当时想前20篇只讲解Web基础知识,中间10篇讲解我一直想要封装的Web框架,后20篇将该应用转换为Android应用。但由于最近女儿出生,业余时间都被她占用了,为了尽快把Web基础讲述完就把这部分独立出来了。
本文主要讲解数据表的Hibernate配置,Come on...
1、由于系统首页内容会涉及到几张数据表,这里列出涉及的几张表
2、写出数据表创建脚本,并在MySQL中执行这些脚本
/*创建数据库*/ CREATE DATABASE MEDICAL GO /*创建级别表*/ CREATE TABLE leveltable(levelId int PRIMARY KEY NOT NULL, levelName VARCHAR(20), scoreLimit int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*插入级别数据*/ INSERT INTO leveltable VALUES(1, ‘医士‘, 100) GO INSERT INTO leveltable VALUES(2, ‘医生‘, 300) GO INSERT INTO leveltable VALUES(3, ‘主治医生‘, 1000) GO INSERT INTO leveltable VALUES(4, ‘副主任‘, 2000) GO INSERT INTO leveltable VALUES(5, ‘主任‘, 4000) GO /*创建用户表*/ CREATE TABLE usertable(userId VARCHAR(20) PRIMARY KEY NOT NULL, userAuth VARCHAR(64), userSign VARCHAR(128),iconPath VARCHAR(128), score int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建用户收藏表*/ CREATE TABLE favoritetable(favoriteId int PRIMARY KEY NOT NULL, userId VARCHAR(20), topicId VARCHAR(64), favoriteTime DATETIME) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建用户关注表*/ CREATE TABLE followtable(followId int PRIMARY KEY NOT NULL, userId VARCHAR(20), followKind int, beFollowId VARCHAR(64), followTime DATETIME) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建用户话题表*/ CREATE TABLE topictable(topicId VARCHAR(64) PRIMARY KEY NOT NULL, topicTitle VARCHAR(128), userId VARCHAR(20), depId int, prescript TEXT, challengers VARCHAR(512), browseNum int, topicTime DATETIME) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建科室表*/ CREATE TABLE departmenttable(depId int PRIMARY KEY NOT NULL, depName VARCHAR(64)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建科室记录表*/ INSERT INTO departmenttable VALUES(1, ‘内科‘) GO INSERT INTO departmenttable VALUES(2, ‘外科‘) GO INSERT INTO departmenttable VALUES(3, ‘妇产科‘) GO INSERT INTO departmenttable VALUES(4, ‘儿科‘) GO INSERT INTO departmenttable VALUES(5, ‘眼科‘) GO INSERT INTO departmenttable VALUES(6, ‘口腔科‘) GO INSERT INTO departmenttable VALUES(7, ‘耳鼻喉科‘) GO INSERT INTO departmenttable VALUES(8, ‘皮肤科‘) GO INSERT INTO departmenttable VALUES(9, ‘麻醉科‘) GO INSERT INTO departmenttable VALUES(10, ‘传染科‘) GO INSERT INTO departmenttable VALUES(11, ‘美容科‘) GO /*创建话题评论表*/ CREATE TABLE commentment(commentId VARCHAR(64) PRIMARY KEY NOT NULL, topicId VARCHAR(64), commentInfo TEXT, commentUser VARCHAR(20), commentTime DATETIME) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO /*创建消息表*/ CREATE TABLE messagetable(messageId int PRIMARY KEY NOT NULL, userId VARCHAR(20), fromUserId VARCHAR(20), messageInfo VARCHAR(1024), messageTime DATETIME) ENGINE=InnoDB DEFAULT CHARSET=UTF8 GO
3、在D:\medical\src\com\medical\server\dao下编写与数据表对应的DAO类
(1)用户表对应的DAO类
package com.medical.server.dao; public class UserDAO { /** * 用户ID */ private String userId = null; /** * 用户鉴权字符串 */ private String userAuth = null; /** * 用户自我描述 */ private String userSign = null; /** * 用户头像路径 */ private String iconPath = null; /** * 用户级别对应的分数 */ private int score = 0; // 省略相应的get&set方法 }
(2)级别表对应的DAO类
package com.medical.server.dao; public class LevelDAO { /** * 级别ID */ private int levelId = 0; /** * 级别名称 */ private String levelName = null; /** * 级别对应分数的极限值 */ private int scoreLimit = 0; // 省略get&set方法 }
(3)用户收藏表对应的DAO类
package com.medical.server.dao; public class FavoriteDAO { /** * 收藏ID */ private int favoriteId = 0; /** * 收藏的用户ID */ private String userId = null; /** * 收藏的话题ID */ private String topicId = null; /** * 收藏话题的时间 */ private String favoriteTime = null; // 省略相应的get&set方法 }
(4)用户关注表对应的DAO类
package com.medical.server.dao; public class FollowDAO { /** * 关注ID */ private int followId = 0; /** * 关注的用户ID */ private String userId = null; /** * 关注类别:0为关注人,1为关注话题 */ private int followKind = 0; /** * 被关注的人或话题 */ private String beFollowId = null; /** * 关注时间 */ private String followTime = null; // 省略get&set方法 }
(5)用户话题表对应的DAO类,把ChallengeDAO类修改为TopicDAO
package com.medical.server.dao; public class TopicDAO { /** * 话题ID */ private String topicId = null; /** * 话题名称 */ private String topicTitle = null; /** * 话题发表者 */ private String userId = null; /** * 话题所属科室 */ private int depId = 0; /** * 话题内容 */ private String prescript = null; /** * 被挑战人 */ private String challengers = null; /** * 话题浏览次数 */ private int browseNum = 0; /** * 话题发布时间 */ private String topicTime = null; // 省略get&set方法 }
(6)话题科室表对应的DAO类,把DepartDAO类重命名为DepartmentDAO
(7)话题评论表对应的DAO类
package com.medical.server.dao; public class CommentDAO { /** * 评论ID */ private int commentId = 0; /** * 被评论的话题ID */ private String topicId = null; /** * 被评论的话题内容 */ private String commentInfo = null; /** * 话题评论人 */ private String commentUser = null; /** * 话题评论时间 */ private String commentTime = null; // 省略get&set方法 }
(8)用户消息对应的DAO类
package com.medical.server.dao; public class MessageDAO { /** * 消息ID */ private int messageId = 0; /** * 用户ID */ private String userId = null; /** * 消息来源人 */ private String fromUserId = null; /** * 消息内容 */ private String messageInfo = null; /** * 消息时间 */ private String messageTime = null; // 省略get&set方法 }
4、在D:\medical\war\etc\mapping下定义与数据表对应的Hibernate配置文件
(1)用户表对应的Hibernate配置文件,修改user.hbm.xml文件内容
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="UserDAO" table="usertable"> <id name="userId" column="userId" type="string" /> <property name="userAuth" column="userAuth" /> <property name="userSign" column="userSign" /> <property name="iconPath" column="iconPath" /> <property name="score" column="score" /> </class> </hibernate-mapping>
(2)级别对应的Hibernate配置文件:level.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="LevelDAO" table="leveltable"> <id name="levelId" column="levelId" type="int"> <generator class="increment"></generator> </id> <property name="levelName" column="levelName" /> <property name="scoreLimit" column="scoreLimit" /> </class> </hibernate-mapping>
(3)用户收藏表对应的Hibernate配置文件:favorite.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="FavoriteDAO" table="favoritetable"> <id name="favoriteId" column="favoriteId" type="int"> <generator class="increment"></generator> </id> <property name="userId" column="userId" /> <property name="topicId" column="topicId" /> <property name="favoriteTime" column="favoriteTime" /> </class> </hibernate-mapping>
(4)用户关注表对应的Hibernate配置文件:follow.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="FollowDAO" table="followtable"> <id name="followId" column="followId" type="int"> <generator class="increment"></generator> </id> <property name="userId" column="userId" /> <property name="followKind" column="followKind" /> <property name="beFollowId" column="beFollowId" /> <property name="followTime" column="followTime" /> </class> </hibernate-mapping>
(5)重命名话题表对应的Hibernate配置文件challenge.hbm.xml为topic.hbm.xml,同时修改里面内容
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="TopicDAO" table="topictable"> <id name="topicId" column="topicId" type="string"> <generator class="uuid.hex"></generator> </id> <property name="topicTitle" column="topicTitle" /> <property name="userId" column="userId" /> <property name="depId" column="depId" /> <property name="prescript" column="prescript" /> <property name="challengers" column="challengers" /> <property name="browseNum" column="browseNum" /> <property name="topicTime" column="topicTime" /> </class> </hibernate-mapping>
(6)重命名话题科室表对应的Hibernate配置文件depart.hbm.xml为department.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="DepartmentDAO" table="departmenttable"> <id name="depId" column="depId" type="int"> <generator class="increment"></generator> </id> <property name="depName" column="depName" /> </class> </hibernate-mapping>
(7)话题评论表对应的Hibernate配置文件:comment.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="CommentDAO" table="commentment"> <id name="commentId" column="commentId" type="string"> <generator class="uuid.hex"></generator> </id> <property name="topicId" column="topicId" /> <property name="commentInfo" column="commentInfo" /> <property name="commentUser" column="commentUser" /> <property name="commentTime" column="commentTime" /> </class> </hibernate-mapping>
(8)话题消息表对应的Hibernate配置文件:message.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.medical.server.dao"> <class name="MessageDAO" table="messagetable"> <id name="messageId" column="messageId" type="int"> <generator class="increment"></generator> </id> <property name="userId" column="userId" /> <property name="fromUserId" column="fromUserId" /> <property name="messageInfo" column="messageInfo" /> <property name="messageTime" column="messageTime" /> </class> </hibernate-mapping>
5、在UserUtil增加updateUserScore()方法,用于更新用户级别分数
/** * 更新用户积分 */ public static void updateUserScore(String userName) { UserDAO userDao = getUserByName(userName); if(userDao != null) { userDao.setScore(userDao.getScore() + 5); } Session session = FrameDBUtil.openSession(); Transaction transaction = session.beginTransaction(); session.update(userDao); transaction.commit(); FrameDBUtil.closeSession(); }
6、修改TopicUtil的insertChallenge()方法,增加话题发布时间
/** * 把挑战书记录入库 */ public static void insertChallenge(String userId, String title, int depId, String prescript, String challengers) { TopicDAO topicDao = new TopicDAO(); topicDao.setChallengers(challengers); topicDao.setDepId(depId); topicDao.setPrescript(prescript); topicDao.setTopicTitle(title); topicDao.setUserId(userId); topicDao.setTopicTime(FrameUtil.getCurrentDate()); Session session = FrameDBUtil.openSession(); Transaction transaction = session.beginTransaction(); session.save(topicDao); transaction.commit(); FrameDBUtil.closeSession(); }
【备注】:在FrameUtil中增加了getCurrentDate()方法,读者可以在附件中查看
7、修改PublishChallengeAction逻辑,在发表挑战时对用户增加积分
// 获取挑战药方内容、挑战人 String prescript = getParameter("prescript"); String challengers = getParameter("challengers"); // 写入数据库 TopicUtil.insertChallenge(loginUser.getUserId(), title, depId, prescript, challengers); UserUtil.updateUserScore(loginUser.getUserId())