【Struts2+Hibernate4】开发学生信息管理功能(一)

来源:互联网 发布:如何做网络推广员 编辑:程序博客网 时间:2024/04/30 17:41

       Struts2+Hibernate4整合

       如果使用Struts2+Hibernate4开发一个小的项目示例,那么就首先搭建Struts2+Hibernate4的开发环境。先来

Struts2+Hibernate4的过程,说白了就是导入相关的jar包和配置文件。

       第一步:导入相关jar包

       由于要使用到Struts2框架,这是一个MVC框架,那么我们就必须建立一个Web工程,项目名称为

struts2_hibernate4,我们导入jar包的位置就是项目中的WebContent/WEB-INF/lib目录下,先来看我们应该导入

些jar包。我所使用的struts2版本是struts-2.3.31,那么Struts2的核心jar包就是:struts-2.3.31\apps\struts2-

blank\WEB-INF\lib目录下的所有jar包:

       

       我们使用的HIbernate框架版本是hibernate-4.2.21,那么需要导入HIbernate核心的jar包hibernate-release-

4.2.21.Final\lib\required目录下的所有jar包:

        

       除了以上的jar包我们还需要导入数据库和单元测试的相关jar包,由于使用的MySQL数据库,所以导入MySQL数

据库驱动:mysql-connector-java-5.1.22-bin.jar;导入的JUnit4的测试包:junit-4.10.jar。最后将所有的jar包粘

贴到我们上面所说的目录下。

      第二步:导入相关配置文件

       首先配置的是项目下的WebContent/WEB-INF/web.xml,在里面需要配置Struts2的核心过滤器:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">    <display-name>struts2_hibernate4</display-name>      <welcome-file-list>         <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <!-- 配置Struts2核心过滤器 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>  </web-app>

       其次就是在src目录下导入Struts2核心配置文件struts.xml,我们可以从下载的文件找到

struts2.3.31\apps\struts2-blank\WEB-INF\classes,将一些没有用的示例代码删除即可:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <package name="default" namespace="/" extends="struts-default" /></struts>

       最后就是配置HIbernate的核心配置文件hibernate.cfg.xml,这个我们在下载的文件中也可以找到hibernate-

release-4.2.21.Final\project\etc,将没有用的代码删除:

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory></session-factory></hibernate-configuration>

       第三步是创建包结构:

       一般的话,创建包结构需要进行分层,大致是实体层,dao层和action层,相关的测试包

       

       这样Struts2+Hibernate4的开发环境基本搭建完成。

       创建实体类及对象关系映射文件

       创建实体类

       我们本次的示例使用到两个实体,就是User和Students。

       User实体类:

package com.demo.entity;/** * 用户实体类 * @author Administrator * @date 2016年12月16日 */public class Users {private int uid;private String username;private String password;public Users() {}public Users(int uid, String username, String password) {this.uid = uid;this.username = username;this.password = password;}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "Users [uid=" + uid + ", username=" + username + ", password=" + password + "]";}}

       Students实体类:

package com.demo.entity;import java.util.Date;/** * 学生实体类 * @author Administrator * @date 2016年12月16日 */public class Students {private String sid;private String sname;private String gender;private Date birthday;private String address;public Students() {}public Students(String sid, String sname, String gender, Date birthday, String address) {this.sid = sid;this.sname = sname;this.gender = gender;this.birthday = birthday;this.address = address;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday+ ", address=" + address + "]";}}

       对象关系映射文件

       生成实体类对应的同时,其中一些配置需要手动修改,工具只是生成一些基本配置,不灵活。

       User实体类对象关系映射文件:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-12-16 14:17:27 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="com.demo.entity.Users" table="USERS">        <id name="uid" type="int">            <column name="UID" />            <generator class="native" />        </id>        <property name="username" type="java.lang.String">            <column name="USERNAME" />        </property>        <property name="password" type="java.lang.String">            <column name="PASSWORD" />        </property>    </class></hibernate-mapping>

       Students实体类对象关系映射文件:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-12-16 14:17:27 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="com.demo.entity.Users" table="USERS">        <id name="uid" type="int">            <column name="UID" />            <generator class="native" />        </id>        <property name="username" type="java.lang.String">            <column name="USERNAME" />        </property>        <property name="password" type="java.lang.String">            <column name="PASSWORD" />        </property>    </class></hibernate-mapping>

       将对象关系映射文件加入到hibernate-cfg.xml文件中,在这个配置文件中还需要连接数据和相关的配置:

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><property name="hibernate.current_session_context_class">thread</property><mapping resource="com/demo/entity/Users.hbm.xml"/><mapping resource="com/demo/entity/Students.hbm.xml"/></session-factory></hibernate-configuration>

       生成表结构

       首先在MySQL中建立ssh数据库:

       

       我们在建立的测试源文件包,建立对应的包结构创建TestSchemaExport类进行表结构的生成:

       TestSchemaExport类:

package com.demo.entity;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class TestSchemaExport {@Test//生成表结构public void testSchemaExport(){//创建配置对象        Configuration config =new Configuration().configure();        SchemaExport export = new SchemaExport(config);export.create(true,true);}}

       控制台输出:

       

       数据库显示:

       

       接下来就是用户登录模块和学生信息模块的完成。



1 0
原创粉丝点击