新增改查的操作

一、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)的更多相关文章

  1. j2ee开发之hibernate框架学习笔记

    hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换 ...

  2. Hibernate 系列 学习笔记 目录 (持续更新...)

    前言: 最近也在学习Hibernate,遇到的问题差不多都解决了,顺便把学习过程遇到的问题和查找的资料文档都整理了一下分享出来,也算是能帮助更多的朋友们了. 最开始使用的是经典的MyEclipse,后 ...

  3. hibernate框架学习笔记11:Criteria查询详解

    创建实体类对象: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public class Custome ...

  4. hibernate框架学习笔记10:HQL查询详解

    HQL语句中不可以出现与表有关的内容,而是对象的属性 实体类(注意配置文件): package domain; import java.util.HashSet; import java.util.S ...

  5. hibernate框架学习笔记6:事务

    MySQL的事务.JDBC事务操作: 详细见这篇文章:比较详细 http://www.cnblogs.com/xuyiqing/p/8430214.html 如何在hibernate中配置隔离级别: ...

  6. hibernate框架学习笔记3:API详解

    Configuration对象: package api; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configur ...

  7. Hibernate个人学习笔记(1)

    连接池c3p0所需jar包:Hiberbate开发包-lib-optional-c3p0下全部Jar包 Hiberbate连接池参数配置:Hiberbate开发包-project-etc-hibern ...

  8. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]

    引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...

  9. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-1]

    示例数据库test,用户信息表

随机推荐

  1. ASP.NET学习笔记1—— MVC

    MVC项目文件夹说明 1.App_Data:用来保存数据文件 2.App_Start:包含ASP.NET-MVC系统启动的相关类文件 3.Controllers:存放整个项目"控制器&quo ...

  2. phpstorm8.0汉化版下载

    下载地址http://www.52z.com/soft/161911.html 汉化包:http://www.7down.net/soft/20586.html phpStorm汉化方法 1.安装原版 ...

  3. linux删除某个php程序进程的组合命令

    如,想要杀死正在运行的所有  main.php 进程: ps aux |grep main.php|grep -v grep|awk '{print $2}'|xargs kill -9 解析: ps ...

  4. SQL Server 2008 数据库通过镜像同步备份(数据库热备)

    数据库镜像运行模式: 异步数据库镜像模式(异步,高性能模式) 同步数据库镜像模式(同步,高安全模式) 参考资料: http://technet.microsoft.com/zh-cn/library/ ...

  5. OSX 10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods

    10.11 cocoapods安装命令: sudo gem install -n /usr/local/bin cocoapods

  6. ChartControl

    <dxc:ChartControl Name="chartControl1">            <dxc:ChartControl.Diagram>  ...

  7. PNG图片压缩工具

    https://tinypng.com/ 效果非常不错. 340k的图能压缩到140k左右. 视觉效果差距不大

  8. [PAT]求集合数据的均方差(15)

    #include "stdio.h" #include "malloc.h" #include "math.h" int *getinput ...

  9. zhuang 定制iOS 7中的导航栏和状态栏

    近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也就意味着导航栏 ...

  10. SQL SERVER触发器游标小记

    今天接到个需求用触发器来实现通过条件对其他表的更新.好久没摸SQL SERVER,电脑里也没SQL SERVER安装包,同事遂发来个安装包,一看吓一跳,3.6G!!!!经过漫长等待后,开始作业.需求如 ...