Eclipse中新建Java Project工程:

工程结构  和  需要的Jar包:

我用的SqlServer数据库,所以连接数据库的Jar包是sqljdbc4.jar

一、基于XML配置

1、实体类(对应数据表中的字段)

package Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
public class Student {
private int id;
private String name;
private int age; public Student(){} public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=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;
} }

2、Student.hbm.xml(XML文件 将实体类 对应 数据表 和 表中字段)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="Entity.Student" table="Student">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>

3、hibernate.cfg.xml(配置连接数据库、映射Student.hbm.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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;database=test</property>
<property name="connection.username">sa</property>
<property name="connection.password">Zhutou0216</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句-->
<property name="show_sql">true</property>
<!-- 格式化语句 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <!-- 映射数据库对应实体类: xml配置文件生效方式 -->
<mapping resource="entity/Student.hbm.xml" /> </session-factory> </hibernate-configuration>

4、MainApp(运行类)

package Entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class MainApp { public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory(); Session session = null;
try{
session = factory.openSession(); session.beginTransaction(); Student student = new Student();
student.setName("en");
student.setAge(88);
session.save(student);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
} } }

二、基于注解配置

1、实体类(在实体类中用注解方式对应数据表和表中字段,因此用不着Student.hbm.xml了)

package Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="Student")
public class Student { @Id @GeneratedValue
@Column(name="id")
private int id; @Column(name="name")
private String name; @Column(name="age")
private int age; public Student(){} public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=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;
} }

2、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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;database=test</property>
<property name="connection.username">sa</property>
<property name="connection.password">Zhutou0216</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句-->
<property name="show_sql">true</property>
<!-- 格式化语句 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <!-- 映射数据库对应实体类: xml配置文件生效方式 -->
<!-- <mapping resource="entity/Student.hbm.xml" /> --> <!-- 映射数据库对应实体类:注解生效方式生效方式 -->
<mapping class="Entity.Student"/> </session-factory> </hibernate-configuration>

3、MainApp(这里没有改变)

package Entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class MainApp { public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory(); Session session = null;
try{
session = factory.openSession(); session.beginTransaction(); Student student = new Student();
student.setName("en");
student.setAge(88);
session.save(student);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
} } }

Hibernate基于【XML】和【注解】——完整实例的更多相关文章

  1. mybatis学习一:基于xml与注解配置入门实例与问题

    注:本case参考自:http://www.cnblogs.com/ysocean/p/7277545.html 一:Mybatis的介绍: MyBatis 本是apache的一个开源项目iBatis ...

  2. MyBatis 项目开发中是基于 XML 还是注解?

    只要你对 MyBatis 有所认识和了解,想必知道 MyBatis 有两种 SQL 语句映射模式,一种是基于注解,一种是基于XML. 基于 XML <mapper namespace=" ...

  3. spring 基于XML和注解的两种事务配置方式

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

  5. spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)

    1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...

  6. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  7. JavaWeb中点赞功能的实现及完整实例

    实现原理1.功能描述:一个用户对同一文章只能点赞一次,第二次就是取消赞2.建立一个点赞表great,字段有文章ID(aid),点赞用户ID(uid)3.当有用户进行点赞行为时,使用aid和uid搜索点 ...

  8. hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)

    绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...

  9. Hibernate+Oracle注解式完整实例

    MyEclipse10,新建Web Project,取名hibernate, jar包 1.Cat.java (实体类) package com.hibernate.bean; import java ...

随机推荐

  1. 主引导记录MBR的结构和作用

    MBR磁盘分区都有一个引导扇区,称为主引导记录,英文简称为MBR.1. MBR的结构MBR扇区位于整个硬盘的第一个扇区:按照C/H/S地址描述,即0柱面〇磁头1扇 区:按照LBA地址描述即0扇区.它是 ...

  2. Wireshark分析RabbitMQ

    消费者Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. 【eclipse】删除工作空间

  4. Future Works on P4

    Future Works on P4 P4 and NV: MPvisor, Hyper4, HyperV, Flex4 P4 and NFV P4 and Network Cache P4 and ...

  5. js访3d上下轮播图

    js/css访3d上下轮播图 (附件) <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  6. MVC ---- 增删改成 EF6

    1.MVC EF6的增删改成小练习 namespace T4Demo { public partial class Form1 : Form { NBEntities nb = new NBEntit ...

  7. Jenkins + Ant + Jmeter 对项目的接口进行CI持续集成的配置 外加发送邮件 sendEmail

    Step1:安装Jenkins,从指定git上拉代码[安装步骤忽略] Step2:在项目安装的linux server上安装Ant 和Jmeter,我都安装在了自己的目录上,如下图 Step3:编写b ...

  8. VMware Workstation Pro12安装RedHat6.4 64位

    1.打开VM12软件,并新建一个虚拟机.   点击 下一步.  默认即可,点击 下一步.  选择 稍后安装操作系统(S). 这一项,基本上都是默认的.点击 下一步.  这里 客户机操作系统 就选择 L ...

  9. [原][osg][osgEarth]关于在OE中使用物理引擎的调研

    关于物理引擎旋转的一些整理 参考文档 http://blog.wolfire.com/2010/03/Comparing-ODE-and-Bullet 介绍ODE和bullet的利弊 http://s ...

  10. Lua面向对象 --- 封装

    工程结构: Player.lua: Player = {} function Player:new() local self = {} setmetatable(self, {__index = Pl ...