第一个hibernate程序链接mysql hibernate4.0
来源:互联网 发布:c语言手势识别算法 编辑:程序博客网 时间:2024/06/05 05:56
注意目录结构:
package com.kinsey.hibernate.helloworld;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;public class HibernateTest {public static void main(String[] args) {//创建一个SessionFactory 对象SessionFactory sessionFactory = null;Configuration configuration = new Configuration().configure();configuration.configure();configuration.addClass(News.class);ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory =configuration.buildSessionFactory(serviceRegistry);Session session=sessionFactory.openSession();News news = new News("JAVA", "Liaoxuefeng");session.save(news);session.beginTransaction().commit();;session.close();sessionFactory.close();}}
package com.kinsey.hibernate.helloworld;public class News {private Integer id;private String title;private String author;public News(String title, String author) {super();this.title = title;this.author = author;}public News() {super();}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;}}
<?xml version="1.0" encoding="UTF-8"?><!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.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate5</property> <!-- 配置hibernate基本信息 --> <!-- 配置hibernate所使用的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect </property> <!-- 操作时是否在控制台打印SQL --> <property name="hibernate.show_sql">true</property> <!-- 是否格式化SQL语句 --> <property name="hibernate.format_sql">true</property> <!-- 指定生成数据表的策略 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 指定关联的.hbm.xml文件 --><mapping resource="com/kinsey/hibernate/helloworld/News.hbm.xml" /> </session-factory> </hibernate-configuration>
<?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 2016-8-26 19:49:09 by Hibernate Tools 3.5.0.Final --><hibernate-mapping> <class name="com.kinsey.hibernate.helloworld.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="increment" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="author" type="java.lang.String"> <column name="AUTHOR" /> </property> </class></hibernate-mapping>
存在的问题:
一开始看视频:
视频中ServiceRegistry建立方式与下面不一样,一直报错上网查说hibernate5.x 不支持ServiceRegistryBuilder。
后在网上查资料修改之。不能太相信视频。
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
在配置ID主键时,选取assgined居然报错,然后改为increment,后面需要研究研究,怎么回事。
第一次运行,提示数据库没有news表,感到奇怪,后来手动在数据库中见表的。
0 0
- 第一个hibernate程序链接mysql hibernate4.0
- 【hibernate4.3】构建第一个hibernate程序
- Hibernate基础之一:Hibernate4.1.6第一个程序
- 【hibernate4.3】annotation 建立第一个程序
- Hibernate4入门到精通一——————第一个Hibernate程序
- 第一个Hibernate程序
- 第一个hibernate程序
- hibernate第一个程序
- 第一个hibernate程序
- 第一个hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- hibernate 第一个程序
- 第一个hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- Makefile
- 灾后重建
- 用递归实现判断一个字符串是否是回文的方法
- Unable to locate Spring NamespaceHandler for XML schema namespace问题解决
- 安装Caffe Anaconda的时出现问题的解决方法
- 第一个hibernate程序链接mysql hibernate4.0
- HIHO #1287 : 数论一·Miller-Rabin质数测试
- Linux下安装和使用boost库
- 用java连接Oracle数据库并取出表中的信息显示出来
- JDK5.0相关
- 论文笔记 Bootstrapping Face Detection with Hard Negative Examples
- 测试音乐分享
- HTTP协议介绍
- 计算机原理浅浅分析