Hibernate 4.3.7 可编程方式+注解
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 可编程方式+注解的更多相关文章
- Hibernate映射文件配置(hbm.xml和注解方式)
一:通过*.hbm.xml配置实体的实现方式 mappingResources用于指定少量的hibernate配置文件像这样 Xml代码 <property name="mappin ...
- shiro框架学习-7- Shiro权限控制注解和编程方式
讲解权限角色控制 @RequiresRoles, @RequiresPermissions等注解的使用和编程式控制 配置文件的方式 使用ShiroConfig 注解方式 @RequiresRoles( ...
- C#+EntityFramework编程方式详细之Code First 数据迁移
在前几篇的C#+EntityFramework编程方式中介绍了C#+EntityFramework编程方式Code First ,Model First以及Dtatabase First 等编程方式, ...
- 大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)
大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式) 现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一 ...
- 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)
前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...
- ASP.NET MVC下的四种验证编程方式[续篇]
在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...
- ASP.NET MVC下的四种验证编程方式
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...
- [转]Windows网络编程学习-面向连接的编程方式
直接附上原文链接:windows 网络编程学习-面向连接的编程方式
- C#通过编程方式实现Ping
代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...
随机推荐
- 开源天气预报api整理
高德天气:https://lbs.amap.com/api/webservice/guide/api/weatherinfo/? github上对开源api的整理:https://github.com ...
- EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体server,EasyPlayer手机播放器
在不断进行EasyDarwin开源流媒体server的功能和性能完好的同一时候,我们也配套实现了眼下在安防和移动互联网行业比較火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 ...
- Java 递归解决 "仅仅能两数相乘的计算器计算x^y" 问题
/** * 求一个数的乘方 * 求x^y,y是一个正整数. 设计算器仅仅能计算两数相乘,不能一次计算n个数相乘. * 知:2^5=(2^2)^2*2; 2^6=(2^2)^3=((4)^2)*4; 2 ...
- Office转SWF的一些感想(Office2007和Office2010)
Office2007需要借助SaveAsPDFandXPS的插件完成,Office2010可以直接兼容. Office2PDF主要采用的是 Microsoft.Office.Interop的方式进行, ...
- 【转载】C#相等性比较
本文阐述C#中相等性比较,其中主要集中在下面两个方面 ==和!=运算符,什么时候它们可以用于相等性比较,什么时候它们不适用,如果不使用,那么它们的替代方式是什么? 什么时候,需要自定一个类型的相等性比 ...
- HDU 2578 Dating with girls(1) [补7-26]
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 程序员笔记|Spring IoC、面向切面编程、事务管理等Spring基本概念详解
一.Spring IoC 1.1 重要概念 1)控制反转(Inversion of control) 控制反转是一种通过描述(在java中通过xml或者注解)并通过第三方去产生或获取特定对象的方式. ...
- 【直播预告】7月25日3D游戏引擎免费公开课答疑第三期,有奖问答!
喜讯喜讯! 为了酬谢广大学员.CSDN学院特推出iOS和3D游戏引擎开发免费技术答疑公开课.让您度过一个充实的暑假~ 參与本次公开课,进行有奖问答.即有机会获奖. 答疑公开课时间:7月25日 晚7:3 ...
- MySQL的简单优化
一.如何发现需要优化的SQL 主要使用MySQL的慢查日志对有效率问题的SQL进行监控 第一步:启动慢查日志的监控 打开开关,将未使用索引的查询记录到慢查日志中 设置查询时间,当查询时间大于这个值,就 ...
- 附录: mysql show processlist中的State的意义
附录: mysql show processlist中的State的意义 Checking table 正在检查数据表(这是自动的). Closing tables 正在将表中修改的数据刷新到磁盘中, ...