hibernate hql 使用实例
来源:互联网 发布:淘宝开店需要电脑吗 编辑:程序博客网 时间:2024/05/22 01:34
1. 导入hibernate3 包
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/student
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">student</property>
<mapping resource="entyti/Student.hbm.xml" />
<mapping resource="entyti/Scj.hbm.xml" />
<mapping resource="entyti/Chengji.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.编写实体类映射文件
<hibernate-mapping>
<class name="entyti.Student" table="student" catalog="student">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="sex" type="java.lang.Short">
<column name="sex" />
</property>
<property name="score" type="java.lang.Integer">
<column name="score" />
</property>
<property name="dept" type="java.lang.String">
<column name="dept" />
</property>
<property name="chengji" type="java.lang.String">
<column name="chengji" />
</property>
<property name="classname" type="java.lang.String">
<column name="classname" />
</property>
</class>
</hibernate-mapping>
4. 编写实体类
5. 编写
public class BaseDaoTest {
static SessionFactory sf =null;
static{
Configuration cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
public Session getSession(){
return sf.openSession();
}
}
6.查询
public class HibernateTest extends BaseDaoTest {
@Test
public void select(){
//得到session
Session session = this.getSession();
String sql ="from Student";
//查询出数据
List<Student>list = session.createQuery(sql).list();
for(Student stu:list){
System.out.println(stu.getId());
System.out.println(stu.getName());
}
}
}
7.添加
@Test
public void add(){
//得到session
Session session = this.getSession();
//得到事务
Transaction ts = session.beginTransaction();
try {
//实例化对象
Student stu = new Student();
stu.setName("yuntaoss");
stu.setClassname("hhhhhhh");
//保存添加
session.save(stu);
//提交事务
ts.commit();
} catch (Exception e) {
ts.rollback();
}
}
//修改
@Test
public void xiugai(){
//得到session
Session session = this.getSession();
//得到事务
Transaction ts = session.beginTransaction();
try {
//实例化对象
Student stu = (Student)session.get(Student.class,1);
stu.setName("ssssssddd");
//保存添加修改
session.update(stu);
//提交事务
ts.commit();
} catch (Exception e) {
ts.rollback();
}
}
//删除
@Test
public void deleteTest(){
//得到session
Session session = this.getSession();
//得到事务
Transaction ts = session.beginTransaction();
try {
//实例化对象
Student stu = (Student)session.get(Student.class,13);
//删除
session. delete(stu);
//提交事务
ts.commit();
} catch (Exception e) {
ts.rollback();
}
}
- hibernate hql 使用实例
- Hibernate HQL 初步实例
- Hibernate Hql查询实例
- Hibernate HQL 中的select new map 使用实例 嵌套Map
- Hibernate之HQL 实例总结
- 【hibernate】HQL的种种实例
- hibernate 之HQL查询实例
- Hibernate HQL使用总结
- Hibernate HQL Editor使用
- hibernate HQL使用
- hibernate使用hql 查询
- Hibernate HQL的使用
- HQL语句使用实例
- Hibernate HQL语句使用总结
- hibernate 使用 HQL 和 SQL
- Hibernate HQL之动态构造实例对象
- Hibernate HQL查询 插入 更新实例
- Hibernate工具类:实例2 HQL查询
- 构造回文数操作_网易编程
- JS URL转码escape() encodeURI() encodeURIComponent()
- Installing maven 3.3.3 on Ubuntu
- 【Java并发编程】之二:线程中断(含代码)
- 初识单调栈
- hibernate hql 使用实例
- perl亲身试验ini---使用perl读写配置文件
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- C语言随笔_区分=与==
- 浅谈Hive vs. HBase
- Ubuntu下开发环境配置
- Codeforces #371(Div.2)A.Meeting of Old Friends【模拟+思维】
- ubuntu远程桌面无法修改ip地址,(32) Not authorized to control networking
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends