Hibernate helloworld xml版本

来源:互联网 发布:数据签名异常 编辑:程序博客网 时间:2024/06/01 08:27

整体架构

这里写图片描述

1 环境搭建

Eclipse Jee Neon
tomcat8.5
jdk1.8.0_111
这里写图片描述

2 工程配置hibernate User Libraray

这里写图片描述
点击new,新建一个名为hibernate的用户库
这里写图片描述

3 添加hibernate,mysql驱动,slf4j和log4j的jar包

在mysql中建表

Create database hibernate;Use hibernate;Create table student(id int primary key, name varchar(20), age int);

这里写图片描述

4 建Student类

这里写图片描述

5 配置Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?><!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>        <!-- Database connection settings -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/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>        <!-- 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>        <!-- <property name="current_session_context_class">thread</property> -->        <!-- Drop and re-create the database schema on startup -->        <!-- <property name="hbm2ddl/auto">create</property> -->        <mapping resource=""/>    </session-factory></hibernate-configuration>

6 建student的映射文件Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.study.hibernate.model">    <class name="Student" table="student">        <!-- 主键 -->        <id name="id" column="id">            <generator class="increment"/>        </id>        <!-- 普通属性 -->        <property name="name"/>        <property name="age"/>    </class></hibernate-mapping>

7 Mapping映射信息:Student.hbm.xml

<mapping resource="com/study/hibernate/model/Student.hbm.xml"/>

8 配置log4j,导入log4j-1.2.17.jar,并在src导入log4j.properties文件

log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.rootLogger=info, stdoutlog4j.logger.org.hibernate.test=infolog4j.logger.org.hibernate.tool.hbm2ddl=debuglog4j.logger.org.hibernate.hql.ast.QueryTranslatorImpl=tracelog4j.logger.org.hibernate.hql.ast.HqlSqlWalker=tracelog4j.logger.org.hibernate.hql.ast.SqlGenerator=tracelog4j.logger.org.hibernate.hql.ast.AST=tracelog4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=tracelog4j.logger.org.hibernate.type.BasicTypeRegistry=trace

9 测试环境

Hibernate3:StudentTest.java

package com.study.hibernate.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.study.hibernate.model.Student;/** * hibernate3 测试方法 * @author lol * */public class StudentTest {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    @Before    public void init() {        // 获得 session工厂        sessionFactory = new Configuration().configure().buildSessionFactory();        // 获得 session        session = sessionFactory.openSession();        // 开启事务        transaction = session.beginTransaction();    }    @After    public void destory() {        transaction.commit(); // 提交事务    }    @Test    public void test01() {        //设置model实体        Student s = new Student();        s.setId(1);        s.setName("s1");        s.setAge(1);        session.save(s);    }}

Hibernate5.2.7:StudentTest2.java

package com.study.hibernate.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.study.hibernate.model.Student;/** * hibernate5 测试方法 * @author lol * */public class StudentTest2 {    private SessionFactory sessionFactory;    private Session session;    private Transaction transaction;    @Before    public void init() {        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()                .configure() // configures settings from hibernate.cfg.xml                .build();        sessionFactory = new MetadataSources( registry )                .buildMetadata().buildSessionFactory();         session = sessionFactory.openSession();        // 开启事务        transaction = session.beginTransaction();    }    @After    public void destory() {        transaction.commit(); // 提交事务        session.close();    }    @Test    public void test01() {        //设置model实体        Student s = new Student();        s.setId(1);        s.setName("s1");        s.setAge(1);        session.save(s);    }}

10 运行结果

这里写图片描述

11 github下载

https://github.com/lishenli1994/hibernate_xml_helloworld.git

0 0