hibernate初学helloworld
来源:互联网 发布:下载keep健身软件 编辑:程序博客网 时间:2024/06/07 04:47
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!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="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/scott</property> <!-- 数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 是否在控制台打印sql --> <property name="show_sql">true</property> <!-- 是否对sql格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定程序需要的关联的hbm.xml文件,注意不是包名而是路径 --> <mapping resource="cn/ls/vo/News.hbm.xml"></mapping> </session-factory></hibernate-configuration>
持久化类
package cn.ls.vo;import java.sql.Date;public class News { private Integer id; private String title; @Override public String toString() { return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } private String author; private Date date; public News() { } public News(String title, String author, Date date) { super(); this.title = title; this.author = author; this.date = date; }}
News.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2017-8-4 19:39:30 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="cn.ls.vo.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <!-- 指定主键的生成方式 ,native使用数据库本地的方式--> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="author" type="java.lang.String"> <column name="AUTHOR" /> </property> <property name="date" type="java.sql.Date"> <column name="DATE" /> </property> </class></hibernate-mapping>
Test类
package cn.ls.vo;import static org.junit.Assert.*;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class HibernateTest { @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { //1.创建一个SessionFactory对象 SessionFactory sf=null; //1)创建configuretion对象,对应hibernate的基本配置信息和对象关系映射信息 Configuration con= new Configuration().configure(); //4.0前这样创建 //sf =con.buildSessionFactory(); //2)创建一个ServiceRegistry对象,hibernate的任何配置和服务都需要在该对象中注册后有效 Configuration configuration=new Configuration().configure(); ServiceRegistry service= new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sf=configuration.buildSessionFactory(service); //2.创建一个session对象 Session session=sf.openSession(); //3.开启事务 Transaction tr=session.beginTransaction(); //4.执行保存操作 News news= new News("java","ATGUIGU",(java.sql.Date) new Date()); //5.提交事务 tr.commit(); //6关闭session对象 session.close(); //7.关闭SessionFactory对象 sf.close(); }}
阅读全文
1 0
- hibernate初学helloworld
- hibernate helloworld
- Helloworld!Hibernate
- hibernate-helloworld
- Hibernate-HelloWorld
- Hibernate-helloWorld
- HelloWorld常见问题(Java初学)
- MTK 初学,建HelloWorld
- 初学Java,HelloWorld(一)
- 初学Java,HelloWorld(一)
- nodejs初学-----helloworld
- 初学RabbitMQ之HelloWorld
- 初学MyBatis之HelloWorld
- scala初学之helloWorld
- Hibernate初学
- Hibernate初学
- hibernate 初学
- Hibernate 初学
- 一起从零学Kotlin-20170805
- hdu 5973 大数威佐夫博弈
- 问题 H: 循环左移
- JS:文本框失去焦点事件、获得焦点事件
- JAVA线程生命周期
- hibernate初学helloworld
- Python GUI 使用之wxpython的安装
- 银河英雄传说
- 务实java基础之集合总结
- 磁盘2
- 【Codeforces 835 C. Star sky】+ dp
- 初识node.js2
- 石子合并问题--直线版(矩阵相乘)
- 学好C++必须要注意的十八个问题