您的位置:首页 > 其它

SSH:hiberate实现数据添加

2019-01-01 20:42 99 查看

1,定义持久化类---User

[code]package Test;

import java.util.Date;

public class User {
private Integer id;
private String name;
private String password;
private Integer age;
private String gender;
private Date birthday;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.id;
return result;
}
}

2,配置映射文件---User.hub.xml

[code]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Test.User" table="User">
<id name="id" column="id" type="int">
<generator class="native"/>
</id>
<property name="name" type="string" not-null="true" length="50">
<column name="name"/>
</property>
<property name="password" type="string" not-null="true" length="50">
<column name="password"/>
</property>
<property name="age">
<column name="age"/>
</property>
<property name="gender" type="string" not-null="true" length="50">
<column name="gender"/>
</property>
<property name="birthday" type="date" >
<column name="birthday"/>
</property>
</class>
</hibernate-mapping>

3,配置hibernate.cfg.xml文件---自动建表之核心

[code]
<!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="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/Hibernatedatabase3?serverTimezone=GMT</property>

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

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

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

<property name="hbm2ddl.auto" >update</property>    //自动建表核心,但是需要类的最大化配合

<mapping resource="User.hbm.xml" /> //User的映射
</session-factory>

</hibernate-configuration>

4,User测试

[code]package Test;

import java.util.Date;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class UserTest {
public static void main(String[] args) {
SessionFactory sf = null; // 负责Session的实现和建立
Session session = null; // Session与数据库之间的一个会话,是核心
Configuration cfg = new Configuration().configure();// 主要负责加载和管理Hiberate的配置信息,也可加载映射文件信息
sf = cfg.buildSessionFactory();
session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction(); // 事务开始
User user = new User();
user.setName("张三");
user.setPassword("12345");
user.setAge(20);
user.setGender("男");
user.setBirthday(new Date());
session.save(user);
tx.commit();
}
}

5,结果:

自动建表,非手动建立

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: