使用Hibernate实现用户添加

这篇博客讲述了作者在IDEA中使用Hibernate进行用户添加操作时遇到的数据库连接问题。作者配置了hibernate.cfg.xml和User.xml文件,并在manageUser类中尝试保存User对象到数据库,但遇到了错误。错误信息未给出。解决方案可能涉及检查数据库连接参数、Hibernate配置文件以及XML映射文件的正确性。
摘要由CSDN通过智能技术生成

使用Hibernate实现用户添加

idea使用hibernate时遇见问题。

idea连接数据库时遇见问题:

报错

Dao manageUser

package cn.hrbust.dao;

import cn.hrbust.pojo.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.sql.Date;


public class manageUser {
    public static void main(String[] args) {
        User u = new User();
        u.setName("张麻子");
        u.setAge(21);
        u.setGender("女);
        u.setBirthday(Date.valueOf("2001-1-1"));
        Configuration cfg = null;
        SessionFactory sf = null;
        Session session = null;
        Transaction ts = null;
        try {
            cfg = new Configuration().configure();
            sf = cfg.buildSessionFactory();
            session= sf.openSession();
            ts = session.beginTransaction();
            session.save(u);
            ts.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(ts !=null){
                ts.rollback();
            }
        }finally {
            session.close();
            sf.close();
        }
    }
}
User.xml

<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">

    <class name="cn.hrbust.pojo.User" table="T_USER">
        <id name="id" type="int" column="id_">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="gender"/>
        <property name="age" />
        <property name="birthday" />

    </class>
    

</hibernate-mapping>
hibernate.cfg.xml

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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 >

      <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.xml"/>

   </session-factory>
</hibernate-configuration>
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;
    }
}
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值