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
- Hibernate helloworld xml版本
- SpringMVC helloworld (xml版本)
- hibernate helloworld(xml)
- Hibernate(annotation版本helloworld)-2
- Hibernate的Annotation版本的HelloWorld
- Hibernate 中创建Annotation版本的HelloWorld
- 003:hibernate:Annotation版本的Helloworld
- hibernate 建立annotation版本的helloworld
- hibernate helloworld
- Helloworld!Hibernate
- hibernate-helloworld
- Hibernate-HelloWorld
- Hibernate-helloWorld
- 3.跟我一起学建站之hibernate Helloworld xml版
- Hibernate深入理解----02HelloWorld详解(Hibernate.cfg.xml配置文件详解,创建持久化对象详解)
- hibernate3.3.2学习笔记---Annotation版本的HelloWorld及org.hibernate.HibernateException错误改正
- annotation版本的helloworld
- ios版本的helloworld
- C/C++中赋值语句返回值
- wpf 项目添加图片资源
- git push报错 The requested URL returned error: 403
- SGI-STL 迭代器实现
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理
- Hibernate helloworld xml版本
- 在Windows下搭建React Native Android开发环境常见问题
- 关于JFinal官方Demo的理解
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- 代码中的特殊注释-TODO,FIXME,XXX
- mybatis逆向工程
- 偏差、方差、SVD、PCA、LDA
- bad pmd issue analysis
- OpenGL入门(一):基于VS2010的OpenGL环境的搭建与使用