Hibernate框架学习(搭建)

1.先建议一个数据表student


2.将hibernate架包导进项目
3.配置hibernate.cfg.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<!-- 该头文件和hibernate-configuration-3.0.dtd的一样 -->
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>


<session-factory>
   <!-- 规定用哪一种数据库 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 数据库连接URL -->
<property name="connection.url">
jdbc:mysql://localhost:3306/student
</property>
<!-- 数据库的连接名和密码,没有密码不配置 -->
<property name="connection.username">root</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<!-- 格式化数据库语句 -->
<property name="format_sql">true</property>
<!-- 是否生成sql语句 -->
<property name="show_sql">true</property>
<!-- 映射资源  类与数据库相互映射 -->
<mapping resource="com/Student.hbm.xml" />
<mapping resource="com/StudentType.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.写一个pojo javabean类
package com;
/**
 * 简单的pojo类型javabean
 * @author xieyongxue
 *
 */


public class Student {


private String stuId;
private String stuName;
private String stuGener;
private String stuAge;
private StudentType studenttype;


public StudentType getStudenttype() {
return studenttype;
}


public void setStudenttype(StudentType studenttype) {
this.studenttype = studenttype;
}


public Student() {
}


public Student(String stuId) {
this.stuId = stuId;
}


public Student(String stuId, String stuName, String stuGener, String stuAge) {
this.stuId = stuId;
this.stuName = stuName;
this.stuGener = stuGener;
this.stuAge = stuAge;
}


public String getStuId() {
return this.stuId;
}


public void setStuId(String stuId) {
this.stuId = stuId;
}


public String getStuName() {
return this.stuName;
}


public void setStuName(String stuName) {
this.stuName = stuName;
}


public String getStuGener() {
return this.stuGener;
}


public void setStuGener(String stuGener) {
this.stuGener = stuGener;
}


public String getStuAge() {
return this.stuAge;
}


public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}


}
5.配置Student.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 此头文件与hibernate-mapping-3.0.dtd相同-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com">
     <!-- 熟悉名与表的字段名 -->
    <!-- catalog="student"可以理解为数据库 -->
    <class name="Student" table="student" catalog="student">
        <id name="stuId" type="java.lang.String">
            <column name="STU_ID" length="50" />
            <!-- generator class="incremet"ID生成器 -->
            <generator class="assigned" />
        </id>
        <property name="stuName" type="java.lang.String">
            <column name="STU_NAME" length="50" />
        </property>
        <property name="stuGener" type="java.lang.String">
            <column name="STU_GENER" length="50" />
        </property>
        <property name="stuAge" type="java.lang.String">
            <column name="STU_AGE" length="50" />
        </property>
        <many-to-one name="studenttype" column="stutypeid" class="StudentType" cascade="all" fetch="join"></many-to-one>
    </class>
</hibernate-mapping>
6.写一个简单的插入和查询测试类
package com;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateDemo {
public static void main(String[] args) {
//创建hibenate配置对象
Configuration conf=new Configuration();
//加载处理配置文件的hibenate.cfg.xml
conf.configure();
//生成session工厂对象
SessionFactory sf=conf.buildSessionFactory();
//session是持久化最关键的一个类
Session s=sf.openSession();
//开启事务
Transaction tx=s.beginTransaction();
//创建一个对象
Student student=new Student();
student.setStuId("12345522333");
student.setStuName("xieyongxue");
//执行插入
s.save(student);
//执行查询
Query q=s.createQuery("from Student");
ArrayList<Student> alg=(ArrayList<Student>)q.list();
for(Student stu:alg){
System.out.println(stu.getStuName());
}
tx.commit();
s.close();
sf.close();
}


}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值