使用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;
}
}