java SSH框架

1整体流程:

 1:hibernate流程:

2:hibernate使用流程:

  2.1:建立用户jar包,导入hibernate相应的jar包

  2.2 引入mysql的JDBC驱动包

  2.3 在数据库中建立数据库和表

  2.4 建立hibernate配置文件,hibernate.cfg.xml(修改对应的数据库连接内容,注释暂时不用的内容)

  2.5 建立的Student类来对应表的字段

  2.6 建立Student类的映射文件,Student.hbm.xml

  2.7 将映射文件加入到hibernate.cfg.xml中

  2.8写测试类,对Student进行存储测试

目录结构:

 hibernate相关jar包

hibernate.cfg.xml文件在src目录下

<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <!--<property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- -->
        <!-- Enable Hibernate's automatic session context management -->
        <!-- <property name="current_session_context_class">thread</property> -->

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <!--<property name="hbm2ddl.auto">update</property> -->

        <mapping resource="com/hyb/hibernate/model/Student.hbm.xml" />
        <mapping class="com.hyb.hibernate.model.Teacher" /> 
    </session-factory>

</hibernate-configuration>

Student类

package com.hyb.hibernate.model;

public class Student {
    
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

Student.hbm.xml文件,和Student在同一个目录下(Model层下面),设置的是和数据库表对应的关系

<?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.hyb.hibernate.model">
    <class name="Student">
        <id name="id"></id>
        <property name="name"></property>
        <property name="age"></property>
    </class>
</hibernate-mapping>

 Student测试类

package com.hyb.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.hyb.hibernate.model.Student;

public class hibernateTest {
    public static void main(String[] args) {
        Student s = new Student();
        s.setId(2);
        s.setName("hhaha");
        s.setAge(22);

        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(s);
        session.getTransaction().commit();
        session.close();
        sf.close();
    }

}

还有一种通过注解的方式就不用写Teacher.hbm.xml,使用注解的时候要加入注解的3个jar包,

Teacher.java

package com.hyb.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {

    private int id;
    private String name;
    private String title;

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Teacher的测试类

需要在hibernate.cfg.xml里面加入映射关系 <mapping class="com.hyb.hibernate.model.Teacher" /> 

package com.hyb.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.hyb.hibernate.model.Teacher;

public class TeacherTest {
    public static void main(String[] args) {
        Teacher t = new Teacher();
        t.setId(1);
        t.setName("nihao");
        t.setTitle("中级");

        Configuration cfg = new AnnotationConfiguration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(t);
        session.getTransaction().commit();
        session.close();
        sf.close();
    }

}
原文地址:https://www.cnblogs.com/DonAndy/p/6477116.html