享受痛苦就是走向成功的第一步。
一、创建一个项目(lib里面是需要的夹包小奶瓶要导包)
二、书写大配置文件
大配置文件必须放置在项目根目录(专业classpath下):界定:就是src
1名称: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"> <hibernate-configuration> <session-factory> <!-- Database connection settings 数据库连接设置-->
<!-- 驱动类 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost::orcl</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property> <!-- SQL dialect (sql的方言)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout 在控制台打印后台的sql语句-->
<property name="show_sql">true</property>
<!-- 格式化显示sql -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup 序列化-->
<property name="hbm2ddl.auto">update</property> <mapping resource="hibernate.hbm.xml" /> </session-factory> </hibernate-configuration>
三、创建小配置
1、名称:hibernate.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity">
<class name="Student" table="STUDENT">
<id name="sid" column="SID">
<!-- 主键生成策略:native: native:如果后台是Oracle 后台是MySQL,自动应用自增 -->
<generator class="native" />
</id>
<property name="name" type="string" column="NAME" />
<property name="age" />
</class> </hibernate-mapping>
四、测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.happy.entity.Student; public class Test { /**
* @param args
*/
public static void main(String[] args) {
//addAll();//添加信息
//getAll();//查看信息
//deleteAll();//删除信息
UpdateAll();//修改信息
}
//添加信息
public static void addAll(){
Student stu = new Student();
//stu.setSid(2);
stu.setName("聆听");
stu.setAge();
// 1.1读取大配置文件,获取要连接的数据库信息
Configuration cfg = new Configuration().configure();
// 1.2创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory(); // 1.3加工session
Session session = factory.openSession(); Transaction tx = session.beginTransaction(); // 02.Hibernate 帮我保存 session.save(stu); tx.commit();
System.out.println("save ok!"); }
//查看全部信息
public static void getAll(){
//读取配置文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory sf=conf.buildSessionFactory();
//打开session
Session se=sf.openSession();
//加载数据
Student dept=(Student)se.get(Student.class, new Integer( ));
System.out.println(dept.getName());//输入数据
System.err.println(dept.getAge());
//关闭会话
if(se!=null){
se.close();
} }
//删除信息
public static void deleteAll(){
//读取数据文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory se=conf.buildSessionFactory();
//打开session
Session session=se.openSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//删除对象(持久化操作)
session.delete(stu);
//提交事务
tx.commit();
System.out.println("删除成功");
//回滚事务
tx.rollback();
System.out.println("删除回滚");
//关闭session
if(session!=null){
session.close();
}
}
public static void UpdateAll(){
//读取数据文件
Configuration conf=new Configuration().configure();
//创建SessionFactory
SessionFactory se=conf.buildSessionFactory();
//打开session
Session session=se.openSession();
//开始一个事务
Transaction tx=session.beginTransaction();
//获取部门的对象
Student stu=(Student)session.get(Student.class, new Integer());
//修改信息
stu.setName("女王");
//提交事务
tx.commit();
/*http://blog.csdn.net/woxueliuyun/article/details/3930335*/
System.out.println("修改成功");
//回滚事务
tx.rollback();
System.out.println("修改回滚");
//关闭session
if(session!=null){
session.close();
}
}
}