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. VC/MFC中计算程序运行时间

    转自原文VC/MFC中计算程序运行时间 说明,这四种方法也分别代表了类似的实现,在MFC中,所可以从哪些类集合去考虑. 方法一 利用GetTickCount函数(ms) CString str; lo ...

  2. jinjia2模板学习

    http://docs.jinkan.org/docs/jinja2/templates.html#

  3. BUPT复试专题—查找(2011)

    https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa?tpId=67&tqId=29646&tPage= ...

  4. 【从零学习openCV】IOS7下的openCV开发起步(Xcode5.1.1&amp;openCV2.49)

    前言: 开发IOS7已经有一月的时间了.近期在准备推研的事,有点想往CV方向发展.于是開始自学openCV. 关注CSDN已经非常久了.也从非常多博主那学到了非常多知识,于是我也从这周开启自己的blo ...

  5. 【剑指offer】打印1到最大的n位数

    题目描写叙述: 输入数字n,按顺序打印出从1到最大的n位十进制数.比方输入3,则打印出1.2.3一直到最大的3位数即999. 分析描写叙述: 首先想到的是先计算出最大的n位数是多少,然后用一个循环从1 ...

  6. 一句话说清楚啥是delegate

    所谓托付就是类对象调用.托付对象代表随意实现该托付的类的对象.

  7. typedef和#define的差别——————【Badboy】

    typedef 和#define 都经常使用来定义一个标识符及keyword的别名.但他们之间有关键的差别. typedef 是语言编译过程的一部分; #define是宏定义语句,它本身并不在编译过程 ...

  8. Page Design for Sexable Forum

    Design Demo 1. Home of Sexable Forum 1.1  home page not logined. 1,2 home page logined. 2. Pages wit ...

  9. gcc编译minigui新程序报错

    !ggcc *.c  -ljpeg -lpng  -lminigui -lpthread/usr/local/lib/libminigui.so: undefined reference to `dl ...

  10. vue 单页面(SPA) history模式调用微信jssdk 跳转后偶尔 "invalid signature"错误解决方案

    项目背景 vue-cli生成的单页面项目,router使用history模式.产品会在公众号内使用,需要添加微信JSSDK,做分享相关配置. 遇到的问题 相关配置与JS接口安全域名都已经ok,发布后, ...