2021-05-11

Hibernate的配置与添加


 

新建一个Java程序

导包




添加hibernate的jar包


添加数据库驱动jar包

添加日志jar包


添加配置文件

 

找到相关的配置信息

 

进行方言、连接数据库驱动、统一资源定位器、的配置

 

选择MySQL

 

 

创建数据库

 

测试URL是否正确

 

完成hibernate.cfg.xml中的配置

 

<hibernate-configuration>

<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">123456</property>

<property name="show_sql">true</property>

<mapping resource="cn/hrbust/pojo/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

 

 

 

添加实体类和映射文件

src下创建User类

 

 

package cn.hrbust.pojo;

 

import java.sql.Date;

 

public class User {

private int id;

private String name;

private String gender;

int age;

Date birthday;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

}

 

 

 

创建数据库T_USER表

 

 

<hibernate-mapping>

 

    <class name="cn.hrbust.pojo.User" table="t_user">

        <id name="id" column="id">

            <generator class="native"/> <!-- 根据底层数据库自动选择 -->

        </id>

        <property name="name"/>

        <property name="gender"/>

        <property name="age"/>

        <property name="birthday"/>

    </class>

 

</hibernate-mapping>

 

 

 

package cn.hrbust.dao;

 

import java.sql.Date;

 

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

 

import cn.hrbust.pojo.User;

 

public class manageUser {

public static void main(String[] args) {

Configuration cfg = null;

SessionFactory sf = null;

Session session = null;

Transaction ts = null;

//创建user

User u = new User();

u.setName("李昕玥");

u.setGender("女");

u.setAge(21);

u.setBirthday(Date.valueOf("2000-4-28"));

try {

//创建Configuration对象

cfg = new Configuration().configure();

//创建Session工厂

sf = cfg.buildSessionFactory();

//创建Session

session = sf.openSession();

//创建事务

ts = session.beginTransaction();

//持久化操作:session保存对象

session.save(u);

//提交事务

ts.commit();

} catch (HibernateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

if(ts != null) {

ts.rollback();

}

}finally {

//关闭session

session.close();

sf.close();

}

}

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值