hibernate案例数据库增加数据
来源:互联网 发布:未转变者服务器端口 编辑:程序博客网 时间:2024/06/05 15:09
原项目下载传送门: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测试类详解
阅读全文
0 0
- hibernate案例数据库增加数据
- 数据库数据的增加
- 学习日记(二)Hibernate用法:针对数据库表数据的增加,查询,更新,删除
- 数据库表的增加数据
- Hibernate快速入门案例——手动增加记录
- 在hibernate中增加一个数据库字段
- hibernate简单的增加数据(笔记)
- Hibernate基础:快速入门(4):增加数据
- Hibernate------>JDBC第一个案例(连接数据库)
- hibernate 链接Mysql数据库小案例
- Hibernate -- 连接oracle 数据库小型案例
- django项目中的数据库中增加数据
- C#大数据批量增加到数据库
- 给数据库增加数据的存储过程
- MySQL 数据库增量数据恢复案例
- MySQL 数据库增量数据恢复案例
- 关于excel数据导入到数据库案例
- 为struts+spring+hibernate增加显示数据库操作语句功能
- Python3 SM3 encoder
- js中获取时间new date()的用法
- 安卓Eclipse项目导入Android Studio完美解决方案
- 12.1 第一个Spirng应用:HelloSpirng
- 第四周项目三(3)—判断单链表是否递增
- hibernate案例数据库增加数据
- iOS 获取APP名称 版本等
- 防止xss攻击
- cookies,sessionStorage 和 localStorage
- 解析微服务架构(一):什么是微服务
- 哈哈
- Retrofit2源码阅读
- elasticsearch 5.x 搭建学习过程
- linux系统下程序编译过程