• 使用Struts2+Hibernate开发学生信息管理功能1


    第一章:Struts2与Hibernate整合

    1.课程简介

    2.界面原型演示

    3.Struts2与Hibernate整合

    4.创建实体类

    5.生成实体映射文件

    6.生成表结构

    1.课程简介

    • Struts2+Hibernate4实现的简单的CRUD案例
    • 完成功能:

      后台登录

      学生表的增删改查

    环境:Struts2.3.4+Hibernate4.2.4+MySQL6.0

    2.界面原型演示

    • 界面演示

    3.Struts2与Hibernate整合

    • 创建struts2和hibernate用户类库
    • 导入struts2与hibernate的jar包
    • 配置web.xml
    • 创建struts.xml
    • 配置hibernate.cfg.xml

    配置web.xml

     创建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>
    

     配置hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
    	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    	<session-factory>
    		<property name="connection.username">root</property>
    		<property name="connection.password">123456</property>
    	    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="connection.url">jdbc:mysql:///test?useUnicode=true&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="hibernate.current_session_context_class">thread</property>		
    		
    	</session-factory>
    </hibernate-configuration>
    

    4.创建实体类

    创建两个实体类:用户类、学生类,对应两个表

    • 用户表
    • 学生表

    用户类

    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;
    	}
    	
    	
    }
    

     学生类

    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
    				+ "]";
    	}	
    	
    }
    

    5.生成实体映射文件

    • Users.hbm.xml
    • Students.hbm.xml

    Users.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/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"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    
        <class name="entity.Students" table="STUDENTS">
        <!-- 学号长度8位 -->
            <id name="sid" type="java.lang.String" length="8">
            <!-- assigned手工赋值 -->
                <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>
    

    6.生成表结构

    • 使用SchemaExport生成表结构

      把刚才生成的两个对象关系映射文件加到主配置文档hibernate.cfg.xml当中

    <!DOCTYPE hibernate-configuration PUBLIC
    	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    	<session-factory>
    		<property name="connection.username">root</property>
    		<property name="connection.password">123456</property>
    	    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="connection.url">jdbc:mysql:///test?useUnicode=true&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="hibernate.current_session_context_class">thread</property>		
    		
    		<mapping resource="entity/Students.hbm.xml"/>
    		<mapping resource="entity/Users.hbm.xml"/>
    	</session-factory>
    </hibernate-configuration>
    

    使用SchemaExport生成表结构

    TestStudents.java

    package entity;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.service.ServiceRegistryBuilder;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.Test;
    
    public class TestStudents {
    
    	@Test
    	public void testSchemaExport(){
    		
    		//创建配置对象
    		Configuration config=new Configuration().configure();
    		//创建服务注册对象
    		ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
    		//创建sessionFactory
    		SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
    		//创建session对象
    		Session session = sessionFactory.getCurrentSession();
    		//创建SchemaExport对象
    		SchemaExport export = new SchemaExport(config);
    		export.create(true, true);//第一个参数表示生成表结构,第二个参数表示输出sql语句
    	}
    }
    
  • 相关阅读:
    每日一小练——因子分解
    HDU 4588 Count The Carries 数位DP || 打表找规律
    ThinkPHP数据分页Page.class.php
    android与C# WebService基于ksoap通信(C#篇)
    HDU4009 Transfer water 【最小树形图】
    libmemcached的安装及測试
    Linux 静态链接库和动态连接库
    js实现table排序(jQuery下的jquery.sortElements)
    yum -y --downloadonly --downloaddir=/ruiy upggrde;
    yum -y --downloadonly --downloaddir=/root/ruiy update
  • 原文地址:https://www.cnblogs.com/songsongblue/p/9531169.html
Copyright © 2020-2023  润新知