• 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();
        }
    
    }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 选择排序
    Java实现 蓝桥杯VIP 算法提高 选择排序
    Java实现 蓝桥杯VIP 算法提高 选择排序
    QT中的SOCKET编程
    代理Delegate的小应用(使用setModelData设置下拉日期对话框)
    360企业云盘需求调研,包括定价
    大神为你分析 Go、Java、C 等主流编程语言(Go可以替代Java,而且最小化程序员的工作量,学习比较容易)
    VS 查看是否有内存泄露的方法
    SpringMVC之 数据绑定-1
    动画操作 (Applying Animations) ngAnimate12
  • 原文地址:https://www.cnblogs.com/DonAndy/p/6477116.html
Copyright © 2020-2023  润新知