搭载Hibernate框架以及入门程序

1、环境要求

JDK1.7+myeclipse

2、运行jar包

http://download.csdn.net/download/qq_24818689/9974606

3、工作环境包的创建


4、配置hibernate.cfg.xml

<!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 >
<!-- 方言   数据库连接 映射文件 Hibernate基本参数配置 -->

<!-- 方言 针对不同的数据库采用不同的语言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 数据库配置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>

<!--连接池配置  -->
<property name="c3p0.max_size">50</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">120000</property>

<!-- Hibernate基本参数配置 -->
<property name="show_sql">true</property>    <!-- 显示sq l -->
<property name="format_sql">true</property>   <!-- 格式化SQL语句显示 -->
<property name="current_session_context_class">thread</property> <!--当前线程事务  -->
<property name="hbm2ddl.auto">update</property>  <!-- 可以更新数据库中的表结构 -->
<!--映射文件  -->
<!-- 有多少映射文件就会出现多少个配置 -->
<mapping resource="com/etc/entity/Grade.hbm.xml"/>
</session-factory>
</hibernate-configuration>

5、配置类的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="com.etc.entity">
<!-- 
name:对应实体类型 
table:对应表名
id:表示主键
property:表示属性
type:字段的类型 string表示数据库中的的varchar
name:实体属性名
column:表示表字段名
-->
 
    <class name="Grade" table="grade" >
        <id name="gradeId" column="gradeId" type="int">
        <!--主键生成策略  在mysql中表示主键自动增长-->
            <generator class="native"/>
        </id>
        <property name="gradeName" column="gradeName" type="string" length="20"></property>
    </class>
</hibernate-mapping>

6、创建单例工具类获取session

public class SessionFactoryUtil {
private SessionFactoryUtil(){

}
private static Configuration configuration;
private static SessionFactory sf;
static{
//1 加载主配置文件 如果主配置文件在默认路径名 且是默认文件 这可以省略
configuration=new Configuration().configure("hibernate.cfg.xml");

//2 取SessionFactory

sf=configuration.buildSessionFactory();
}
public static Session getSession(){

return sf.getCurrentSession();
}
}

7、按照mvc模式进行基本的增删查改操作



我们主要来看实现类
public class GradeDaoImpl implements GradeDao {
Session session=SessionFactoryUtil.getSession();
@Override
public List<Grade> getGrades() {


// 开启事务
Transaction tx=  session.beginTransaction();

//5 数据操作 HQL:Grade的是实体类名
List<Grade> grades=session.createQuery("from Grade").list();
//6 事务提交
tx.commit();
//7 关闭
//session.close();

return grades;
}


@Override
public Grade getGradeById(int id) {
Transaction tx=session.beginTransaction();
//取单个对象 类型,主键值
Grade grade =session.get(Grade.class, id);
//get如果没找到系统就返回null 而load则会抛出异常
tx.commit();
return grade;
}


@Override
public void addGrade(Grade grade) {
Transaction tx=null;
try {
tx=session.beginTransaction();
//添加对象
session.save(grade);
tx.commit();
} catch (Exception e) {
tx.rollback();
}

}
@Override
public void deleteGrade(int id) {
Transaction tx=null;
try {
tx=session.beginTransaction();
//根据主键值取到对象 然后再删除
if(session.get(Grade.class, id)!=null)
{
session.delete(session.get(Grade.class, id));
tx.commit();
}
//此时对象处于瞬时状态session.get(Grade.class, id)
} catch (Exception e) {
tx.rollback();
}
}


@Override
public void updateGrade(Grade grade) {
Transaction tx=null;
try {
tx=session.beginTransaction();

//从数据库中拿出来的处于持久状态并与session关联
Grade grade2=session.get(Grade.class, grade.getGradeId());
if(grade2!=null){
//不能直接赋值 grade2=grade; 地址会指向上层,这样grade2的地址发生改变
//不受session的控制
grade2.setGradeName(grade.getGradeName());//重新赋值
session.update(grade2);
tx.commit();
}
//事务提交grade2就处于游离状态
System.out.println(grade2);

} catch (Exception e) {
tx.rollback();
}
}
}

8、用Test类测试的结果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值