Hibernate-------<1>初步学习
javaBean------Student.java 数据库-----students表
public class Student { create database hib character set utf8;
private String studId; use hib;
private String studName; create table students(
private Integer studAge; id varchar(8) primary key,
private String deptId; name varchar(40),
public String getStudId() { age int,
return studId; deptId varchar(8) );
}
public void setStudId(String studId) {
this.studId = studId;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public Integer getStudAge() {
return studAge;
}
public void setStudAge(Integer studAge) {
this.studAge = studAge;
}
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
}
mapping file------Student.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.hncu.demo.domain">
<!--要配置对象所在的包 -->
<class name="Student" table="students" catalog="hib">
<!--catalog 指定数据库-->
<id name="studId" type="java.lang.String">
<!-- 只有主键字段对应的属性变量才能用id,其它字段对应的属性变量都用property -->
<column name="id" length="8"></column>
</id>
<property name="studName" type="java.lang.String">
<column name="name" length="40"></column>
</property>
<property name="studAge" type="java.lang.Integer">
<column name="age"></column>
</property>
<property name="deptId" type="java.lang.String">
<column name="deptId" length="8"></column>
</property>
</class>
</hibernate-mapping>
Hibernate configuration----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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hib</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- 设置Dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 把mapping file 配置进来 -->
<mapping resource="cn/hncu/demo/domain/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
写一个工具类拿Session-----HibernateSessionFactory.java
public class HibernateSessionFactory {
private static SessionFactory sessionFactory=null;
private static ThreadLocal<Session> t=new ThreadLocal<Session>();
static{
try {
Configuration config=new Configuration().configure();
StandardServiceRegistry serviceRegistry=new
StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
sessionFactory=config.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession(){
Session session=t.get();
if(session==null||session.isOpen()){
session = (sessionFactory!=null)? sessionFactory.openSession():
null;
t.set(session);
}
return session;
}
public static void clear(){
t.set(null);
}
}
测试类------StudentManager.java
public class StudentManager {
public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();
//开启事务
session.beginTransaction();
Student stud=new Student();
stud.setStudId("S006");
stud.setStudName("刘备");
stud.setStudAge(23);
stud.setDeptId("D003");
session.save(stud);
Student stud2=new Student();
stud2.setStudId("S007");
stud2.setStudName("刘三");
stud2.setStudAge(26);
stud2.setDeptId("D004");
session.save(stud2);//
session.getTransaction().commit();//事务提交;如果出异常Hibernate会帮我们
回滚的,我们不用写rollback() ;
//session.close();Hibernate中session是不能关,也关不了
}
}