hibernate案例数据库增加数据
2017-09-21 11:16
525 查看
原项目下载传送门:http://download.csdn.net/download/six_666666/9988353
项目架构
Person.java
package yw;
public class Person {
private Integer id;
private String name;
private int password;
public Person(Integer id,String name, int password) {
super();
this.id=id;
this.name = name;
this.password = password;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
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;
}
}
Test.java
package yw;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import yw.Person;
public class Test {
static Test mt = new Test();
public static void main(String[] args) {
Configuration config = new Configuration().configure();
//原来configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印 如下信息并抛出HibernateException异常。
SessionFactory sessionFactory = config.buildSessionFactory();
//此方法已过时4.0中已不再用被ServiceRegistry替代
Session session = sessionFactory.openSession();
//打开一个新的session对象
Transaction tx = session.beginTransaction();
//开启事务
session.save(mt.add());
//保存新创建的对象
tx.commit();
//提交
//session.close();
sessionFactory.close();//一定 不 要 关闭.这里只是写出来做例子
/*注意:sessionFactory一定不要关闭.原因是:
它线程安全,所以一个程序只需要获取一次.如果每次都关闭, 重新获取执行效率降低!!!
*/
}
private Person add() {
Person p = new Person(1,"张三", 123);
return p;
}
}
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="yw">
<class name="Person" table="person">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<!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节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hb?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="yw/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试结果(第一次的时候会连表一起创建出来,后面每运行一次会创建一条数据)
调试中出现的问题
slf4j的jar包存在不匹配问题,更换jar包后解决问题
hibernate.cfg.xml的文件无法找到,需要更换目录到src下
jar包缺损找不到导致的问题
Test.java测试类详解
项目架构
Person.java
package yw;
public class Person {
private Integer id;
private String name;
private int password;
public Person(Integer id,String name, int password) {
super();
this.id=id;
this.name = name;
this.password = password;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
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;
}
}
Test.java
package yw;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import yw.Person;
public class Test {
static Test mt = new Test();
public static void main(String[] args) {
Configuration config = new Configuration().configure();
//原来configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印 如下信息并抛出HibernateException异常。
SessionFactory sessionFactory = config.buildSessionFactory();
//此方法已过时4.0中已不再用被ServiceRegistry替代
Session session = sessionFactory.openSession();
//打开一个新的session对象
Transaction tx = session.beginTransaction();
//开启事务
session.save(mt.add());
//保存新创建的对象
tx.commit();
//提交
//session.close();
sessionFactory.close();//一定 不 要 关闭.这里只是写出来做例子
/*注意:sessionFactory一定不要关闭.原因是:
它线程安全,所以一个程序只需要获取一次.如果每次都关闭, 重新获取执行效率降低!!!
*/
}
private Person add() {
Person p = new Person(1,"张三", 123);
return p;
}
}
Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="yw">
<class name="Person" table="person">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name"/>
<property name="password" column="password"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<!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节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hb?useUnicode=true&characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="yw/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试结果(第一次的时候会连表一起创建出来,后面每运行一次会创建一条数据)
调试中出现的问题
slf4j的jar包存在不匹配问题,更换jar包后解决问题
hibernate.cfg.xml的文件无法找到,需要更换目录到src下
jar包缺损找不到导致的问题
Test.java测试类详解
相关文章推荐
- 学习日记(二)Hibernate用法:针对数据库表数据的增加,查询,更新,删除
- Hibernate中利用Criteria进行数据库表数据的增加查询。
- 数据库中两张表之间的数据同步增加、删除
- 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 神经网络分析算法原理篇)
- Spring入门之JdbcTemplate【一】增加、删除、修改数据库表数据
- oracle 恢复学习 案例1 一个数据文件丢失 完全恢复数据库
- MySQL 数据库增量数据恢复案例
- php实现批量上传数据到数据库(.csv格式)的案例
- 使用hibernate查询时,查询的数据和数据库的不一样问题
- hibernate 连接数据库操作步骤以及对数据的增删查改
- 在ArcGIS空间数据库中增加点数据的方法
- (原创)大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 关联规则分析算法)
- Delphi(ADOConncction+ADOQuery+DataSoure+TListView)TListView显示数据库查询数据!!!--演示案例
- 开启事务后hibernate缓存自动更新数据到数据库?
- 解决getHibernateTemplate().save ()不能将数据保存到数据库的问题
- 数据的保存顺序与Hibernate对数据库操作的影响
- jdbc,数据库案例:客户信息管理系统:jsp部分,数据库数据封装类,dao接口和实现
- hibernate 配置问题 造成数据库的数据丢失
- Realm数据库使用教程(二):增加数据
- 大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 聚类分析算法)