[Hibernate] - Study 1
1)解压Hibernate,在eclipse中导入jar包,其中lib\required里的jar包是必需包括在里头的。这里用的是sql server,所以要导入sqljdbc4.jar
2)在src根目录下新建hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1;DatabaseName=DBNAME;integratedSecurity=True;</property>
<property name="connection.username"></property>
<property name="connection.password"></property> <property name="connection.pool_size">2</property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="show_sql">true</property> <mapping resource="com/my/test/mapping/Account.hbm.xml" />
</session-factory>
</hibernate-configuration>
3)加入Account.hbm.xml:
<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.bean.Account" table="PPM_Account">
<id name="accountId" type="java.lang.String">
<column name="AccountId" />
<generator class="assigned" />
</id>
<property name="targetType" type="java.lang.String" length="30">
<column name="TargetType" />
</property>
</class>
</hibernate-mapping>
4)加入bean:Account.java
package com.my.bean;
public class Account {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String accountId;
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
private String targetType;
public String getTargetType() {
return targetType;
}
public void setTargetType(String targetType) {
this.targetType = targetType;
}
}
5)测试Hibernate:
package com.my.test; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.my.bean.Account; public class TestHibernate { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration cfg = new Configuration();
@SuppressWarnings("deprecation")
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();
org.hibernate.Transaction trans = session.beginTransaction(); String hql = "from Account";
Query query = session.createQuery(hql);
List<Account> list = query.list(); trans.commit();
session.close(); for (Account account : list) {
System.out.println(account.getTargetType());
} } }
也可以使用HSQL的Select来写:
package com.my.test; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.my.bean.Account; public class TestHibernate { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration cfg = new Configuration();
@SuppressWarnings("deprecation")
SessionFactory factory = cfg.configure().buildSessionFactory();
Session session = factory.openSession();
org.hibernate.Transaction trans = session.beginTransaction(); String hql = "select new Account(accountId, targetType) from Account";
Query query = session.createQuery(hql);
List<Account> list = query.list(); trans.commit();
session.close(); for(Account account : list){
System.out.println(account.getAccountId());
} } }
HSQL的Where条件可以这样写:
String hql = "select new Account(accountId, targetType) from Account where accountId=:accountId";
Query query = session.createQuery(hql);
query.setParameter("accountId", "7AC8352C-9F6B-4B06-A481-FFEFAC7B3E7D");
List<Account> list = query.list();
[Hibernate] - Study 1的更多相关文章
- [Hibernate] - Study test project
最近玩Hibernate的测试代码工程: http://files.cnblogs.com/HD/TestHibernate.7z
- Hibernate(二)——一对多查询
1. 前言 本章节我们讨论Hibernate一对多查询的处理. 在上一章节中(Hibernate(一)——入门),我们探讨了Hibernate执行最基本的增删改查操作.现在我们将情况复杂化:加入我们在 ...
- Hibernate(一)——入门
1. 前言 Hibernate是一个开放源代码的ORM持久化框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. ...
- Hibernate---数据操作示例BY实体类注释
通过实体的映射文件创建表的示例,除了基本jar包外,还需要jar包如下 ejb3-persistence.jar.hibernate-annotations.jar这两个包均在hibernate-an ...
- Hibernate---数据操作示例BY实体映射文件
创建一个Student.java类:该类需要一个无参的构造函数,以及属性的get/set方法 public class Student implements Serializable { privat ...
- Hibernate实现向数据库插入一条数据全过程(Study By Example)
1.数据库(直接在cmd下进入数据库操作亦可) (1)启动Navicat for MySQL (2)打开连接,创建一个数据库,名为testdb (3)新建表user1,表结构如图所示 2.数据库池 ( ...
- hibernate 入门
工程截图 1.jar包 和 hibernate配置文件 /src/hibernate.cfg.xml , /src/log4j.properties , /src/db.sql < ...
- java.lang.ExceptionInInitializerError /NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ...
- Hibernate框架简单应用
Hibernate框架简单应用 Hibernate的核心组件在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.proper ...
随机推荐
- Codeforces Round #297 (Div. 2)
A题 题目大意: 给你一个字符串,奇数的时候是钥匙,偶数的时候是门,一把钥匙只能开对应的门,然后问你最少额外需要多少把钥匙. 分析: 用的数组记录一下就行,(注意的是先开门,再拿钥匙!开始错在这里了, ...
- iPhone 6/iPhone 6 Plus硬件性能
iPhone 6/iPhone 6 Plus硬件性能 评分 4 分 正 如之前传闻的,这次同时发布了两款iPhone产品,分别是4.7英寸的iPhone 6和5.5英寸的iPhone 6 Plus.苹 ...
- Spring MVC 通过@Value注解读取.properties配置内容
第一步:在applicationContext.xml配置: <bean id="configProperties" class="org.springframew ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- poj1420 拓扑序
题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...
- sessionStorage和localStorage中 存储
转:http://my.oschina.net/crazymus/blog/371757 sessionStorage只在页面打开是起作用, localStorage关闭页面后仍然起作用. 有时候,我 ...
- eclipse常用插件在线安装地址或下载地址
本文转载自:http://my.oschina.net/bloghu/blog/198922 一,反编译插件: A.Jadclipse 1.打开eclipse增加站点:http://jadclipse ...
- EXT2 文件系统
转自:http://www.cnblogs.com/ggjucheng/archive/2012/08/22/2651641.html#ext2_filesystem 认识ext文件系统 硬盘组成与分 ...
- freeswitch 1.4
yum install git gcc-c++ autoconf automake libtool wget python ncurses-devel zlib-devel libjpeg-devel ...
- acess the C\c++ from the Java
https://en.wikipedia.org/wiki/Java_Native_Interface http://docs.oracle.com/javase/7/docs/technotes/g ...