1.复制jar文件到lib

antlr-2.7.7.jar
dbmysql.jar
dboracle.jar
dbsqljdbc2005.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar

2.注解学生类

package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class Student {
private int id;
private String name; @Id
@GeneratedValue
public int getId() {return id;}
public void setId(int id) {this.id = id;}
@Column(name = "name", length = 20)
public String getName() {return name;}
public void setName(String name) {this.name = name;} }

3.HibernateUtil.java 可编程方式类

package com;
import java.util.List;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.entity.Student;
public class HibernateUtil {
public static void main(String[] args) {
Properties m = new Properties();
m.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
m.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
m.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8");
m.put("hibernate.connection.username", "root");
m.put("hibernate.connection.password", "fengze");
m.put("hibernate.hbm2ddl.auto","update"); Properties o = new Properties();
o.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
o.put("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
o.put("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:ORCL");
o.put("hibernate.connection.username", "system");
o.put("hibernate.connection.password", "FengZe2012");
o.put("hibernate.hbm2ddl.auto","update"); Properties s = new Properties();
s.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2005Dialect");
s.put("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
s.put("hibernate.connection.url", "jdbc:sqlserver://localhost:1433;databaseName=db");
s.put("hibernate.connection.username", "sa");
s.put("hibernate.connection.password", "fengze");
s.put("hibernate.hbm2ddl.auto","update"); //Configuration cfg = new Configuration().setProperties(m);
Configuration cfg = new Configuration().setProperties(s);
cfg.addAnnotatedClass(com.entity.Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session session = sf.openSession(); //插入数据
Transaction tx = session.beginTransaction();
Student s1 = new Student();
s1.setName("张三丰");
session.save(s1);
tx.commit(); List<Student> stu = session.createQuery("from Student").list();
for(Student st : stu){
System.out.println(st.getName());
}
session.close();
sf.close();
}
}

Hibernate 4.3.7 可编程方式+注解的更多相关文章

  1. Hibernate映射文件配置(hbm.xml和注解方式)

    一:通过*.hbm.xml配置实体的实现方式 mappingResources用于指定少量的hibernate配置文件像这样 Xml代码  <property name="mappin ...

  2. shiro框架学习-7- Shiro权限控制注解和编程方式

    讲解权限角色控制 @RequiresRoles, @RequiresPermissions等注解的使用和编程式控制 配置文件的方式 使用ShiroConfig 注解方式 @RequiresRoles( ...

  3. C#+EntityFramework编程方式详细之Code First 数据迁移

    在前几篇的C#+EntityFramework编程方式中介绍了C#+EntityFramework编程方式Code First ,Model First以及Dtatabase First 等编程方式, ...

  4. 大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)

    大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)   现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一 ...

  5. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

  6. ASP.NET MVC下的四种验证编程方式[续篇]

    在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...

  7. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

  8. [转]Windows网络编程学习-面向连接的编程方式

    直接附上原文链接:windows 网络编程学习-面向连接的编程方式

  9. C#通过编程方式实现Ping

    代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...

随机推荐

  1. flask的debug模式下,网页输入pin码进行调试

    网站后端Python+Flask .FLASK调试模式之开启DEBUG与PIN使用? 自动加载: # 方式一 1 2 if __name__ == '__main__':     app.run(ho ...

  2. Intent传递简单对象与集合

    我们在Intent传递传递对象.能够有三种方式,实现Serializable接口.实现Parcelable接口,使用json格式序列化与反序列化. 在此我们使用第二方式,现实Parcelable接口, ...

  3. [转]Linux统计代码行数

    wc -l *.c *.h 就可以知道当前目录下的所有c 和 h 文件的行数的详细信息.很不错 如果要递归,可以配合其他命令一起使用 当前目录及子目录: find . -name *.c |xargs ...

  4. Android入门常见问题

    前言: 眼下非常多人入门用android studio,可是我觉得这是 一个不好的開始. 一个集成的软件,不用你去部署.那么你就好难去学习究竟层的东西. 以后的问题时.问题所在还是在底层,就像&quo ...

  5. 经典游戏“大富翁4”存档文件修改器Rich4Editor下载

    下载地址: http://files.cnblogs.com/files/xiandedanteng/Rich4Editor20170614.zip http://files.cnblogs.com/ ...

  6. linux下查看网卡信息的命令

    rhel 内核版本号信息: [root@hvrhub ~]# uname -a Linux hvrhub 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2 ...

  7. 【基础练习】【线性DP】codevs3027 线段覆盖2题解

    文章被盗还是非常严重,加版权信息 转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看看 这道题目是线性动归 可是思想和背包有些类似 事实上线性动 ...

  8. Flash/Flex获取外部参数

    Part One:Flex程序如何获取html容器传递的URL参数值 我们经常在Flex程序需要用从外部html向swf文件传递参数,(类似 test.html?name=jex&addres ...

  9. 多线程网页爬虫 python 实现(二)

    #!/usr/bin/env python #coding=utf-8 import threading import urllib import re import time cur=0 last= ...

  10. dubbo简单配置与使用

    dubbo简介 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时 ...