Hibernate个人学习笔记(2)
新增改查的操作
一、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">
<!-- 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/test
</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">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/neusoft/Dao.hbm.xml" />
</session-factory>
</hibernate-configuration>
二、先实例化对象
public class User {
private int id;
private String username;
private String password;
private String nicheng;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String username, String password, String nicheng) {
super();
this.id = id;
this.username = username;
this.password = password;
this.nicheng = nicheng;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNicheng() {
return nicheng;
}
public void setNicheng(String nicheng) {
this.nicheng = nicheng;
}
}
三、配置实例化配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.neusoft.user" table="user" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned"></generator>
</id>
<property name="username" type="java.lang.String">
<column name="username" length="10" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="10" />
</property>
<property name="nicheng" type="java.lang.String">
<column name="nicheng" length="20" />
</property>
</class>
</hibernate-mapping>
四、session 工具类
package com.neusoft.HibernateTest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class UserUtils {
//读取Hibernate.cfg.xml文件
private static SessionFactory factory;
static{
try{
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
factory = cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
//打开Session
public static Session getSession(){
return factory.openSession();
}
//关闭Session
public static void closeSession(Session session){
if(session != null ){
if(session.isOpen()){
session.close();
}
}
}
public static SessionFactory getSessionFactory(){
return factory;
}
}
五:
新增
public class Save extends TestCase{
//新增
public void test(){
Session session = null;
try {
session = UserUtils.getSession();
session.beginTransaction();
Dao dao = new Dao();
dao.setUsername("qwe");
dao.setPassword("asd");
dao.setNicheng("zxc");
session.save(dao);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
UserUtils.closeSession(session);
}
}
}
查询:
public class Load extends TestCase{
//查詢
public void test(){
Session session = null;
try {
session = UserUtils.getSession();
session.beginTransaction();
Dao dao = (Dao) session.load(Dao.class, 1);
System.out.println(dao.getUsername());
System.out.println(dao.getPassword());
System.out.println(dao.getNicheng());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
UserUtils.closeSession(session);
}
}
}
删除:
public class Delete extends TestCase{
//刪除
public void test(){
Session session = null;
try {
session = UserUtils.getSession();
session.beginTransaction();
Dao dao = (Dao) session.load(Dao.class, 1);
session.delete(dao);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
UserUtils.closeSession(session);
}
}
}
修改:
public class Undate extends TestCase {
//修改
public void test(){
Session session = null;
try {
session = UserUtils.getSession();
session.beginTransaction();
Dao dao = (Dao) session.load(Dao.class, 1);
dao.setUsername("lll");
session.update(dao);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally{
UserUtils.closeSession(session);
}
}
}
Hibernate个人学习笔记(2)的更多相关文章
- j2ee开发之hibernate框架学习笔记
hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换 ...
- Hibernate 系列 学习笔记 目录 (持续更新...)
前言: 最近也在学习Hibernate,遇到的问题差不多都解决了,顺便把学习过程遇到的问题和查找的资料文档都整理了一下分享出来,也算是能帮助更多的朋友们了. 最开始使用的是经典的MyEclipse,后 ...
- hibernate框架学习笔记11:Criteria查询详解
创建实体类对象: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public class Custome ...
- hibernate框架学习笔记10:HQL查询详解
HQL语句中不可以出现与表有关的内容,而是对象的属性 实体类(注意配置文件): package domain; import java.util.HashSet; import java.util.S ...
- hibernate框架学习笔记6:事务
MySQL的事务.JDBC事务操作: 详细见这篇文章:比较详细 http://www.cnblogs.com/xuyiqing/p/8430214.html 如何在hibernate中配置隔离级别: ...
- hibernate框架学习笔记3:API详解
Configuration对象: package api; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configur ...
- Hibernate个人学习笔记(1)
连接池c3p0所需jar包:Hiberbate开发包-lib-optional-c3p0下全部Jar包 Hiberbate连接池参数配置:Hiberbate开发包-project-etc-hibern ...
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]
引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-1]
示例数据库test,用户信息表
随机推荐
- U3D外包、Unreal4外包、VR外包就找北京动点飞扬软件
北京动点软件长年承接Unity3D(U3D外包)项目,我们制作各类型VR/AR游戏,虚拟现实,增强现实项目! 品质保证,售后完备. 联系请加QQ:372900288 电话:13911652504 我 ...
- PHP导出数据到excel的方法
很简单,看内容,不多说了: <?php //设置标题 $header[] = "ID"; $header[] = "订单"; $header[] = &q ...
- CRM 403错误
1 IIS 正常 2 CRM 各项服务正常. 3 应该程序池--CRMAppPool 停止
- Blackfin DSP(五):BF533的SPI接口
533SPI的特性 最高速度可达SCLK/4: 支持主模式和从模式: 可使用8个GPIO口作为从选择线: 1 slave select input pins 7 slave select output ...
- 压测 linux + jexus + mono + asp.net mvc
环境: 1.centos 7 + jexus 5.8.1 + mono 4.4.2 + asp.net mvc 4 做了一点小优化: 一.调整文件描述符数量限制编辑 /etc/security/lim ...
- [EventBus源码解析] EventBus.register 方法详述
前情概要 在上一篇中,介绍了EventBus的基本使用方法,以及一部分进阶技巧.本篇及以后的几篇blog将会集中解析EventBus.java,看看作者是如何优雅地实现这个看似简单的事件分发/接收机制 ...
- linux服务器下添加字体
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 引言:这两天在开发一个动态生成海报的东西(图片拼接,图片水印),开发在windows下没有问题,图片和文字都能正常的生成出来. ...
- WinServer2008R2 部署.NET4.0程序 注意事项
部署注意事项: 1.IIS应用程序池 集成模式 2.在web.config中的system.webServer下,添加 <modules runAllManagedModulesForA ...
- 相同的问题又出现了,struts2取不出数值
debug里面是有数值的,不知道是不是又是表示错了.全部改成了小写也无济于事.正在想法解决中... 问题解决了,因为自己的不仔细,问题还是出在了action的set,get方法里,不是大小写没注意,改 ...
- Java获取系统时间
Java可以通过SimpleDateFormat格式化类对Date进行格式话获取时间. import java.util.*; import java.text.*; public class Tes ...