学生信息管理系统(struts2+Hibernate)(1)

第一步:配置项目中要用到的jar包(struts2+Hibernate+Junit+mysql-jdbc)

1、window-preferences-java-Bulid Path-User Libraries     在这里面添加相关jar包

2、右击项目-Properties-Java Build Path-Add Library  将jar包导入到项目组中

第二步:配置web.xml、struts.xml、hibernate.cfg.xml.

1.在web.xml中配置struts2的过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <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>

2.配置struts.xml

<?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">
    </package>
</struts>

3.配置hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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="username">root</property>
        <property name="password"></property>
        <property name="connection.driver_class">com.MySQL.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///test?useUnicode=true&amp;
            characterEncoding=UTF-8</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

第三步:建实体类(Users和Students)

Users:

package entity;

public class Users {
    private int uid;
    private String username;
    private String password;

    public Users() {

    }

    public Users(int uid, String username, String password) {
        // super();
        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;
    }

}

Students:

package entity;

import java.util.Date;

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) {
    //super();
    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;
}

@Override
public String toString() {
    return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender
            + ", birthday=" + birthday + ", address=" + address + "]";
}

}

第四步:生成实体类的对象关系映射文件(Users.hbm.xml和Students.hbm.xml)

Users.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>
<class name="entity.Users" table="Users">
<id name="uid" type="int">
<generator class="native"/>
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>

</class>
</hibernate-mapping>

Students.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>
    <class name="entity.Students" table="Students">
        <id name="uid" type="java.lang.String"  length="8">
            <generator class="assigned" />
        </id>
        <property name="sname" type="java.lang.String" />
        <property name="gender" type="java.lang.String" />
        <property name="birthday" type="date" />
        <property name="address" type="java.lang.String" />

    </class>
</hibernate-mapping>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值