Hibernate初印象(helloworld)-1
来源:互联网 发布:淘宝有小口栓卖吗 编辑:程序博客网 时间:2024/06/11 09:36
参考文档:
http://docs.jboss.org/hibernate/orm/4.2/quickstart/en-US/html_single/#d5e57
JPA与Hibernate、ibatis等的关系有点类似于JDBC和mysqlDriver,oracleDriver的关系
JPA定义了一套标准的接口规范,Hibernate等框架去按照这套规范实现,Hibernate的作者也参与了JPA规范的定义。
mysql中创建表student
hibernate.cfg.xml
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate?serverTimezone=UTC</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) 一般用jndi--> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect 方言--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management 暂时注释掉 3.2以后--> <!--<property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache 禁用二级缓存--> <property name="cache.provider_class">org.hibernate.cache.NoCacheRegionFactoryAvailableException</property> <!-- Echo all executed SQL to stdout 要不要把生成的sql打印出来--> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup hbm:hibernatemapping ddl建表语句 要不要用hibernate自动生成建表语句 暂时注释--> <!--<property name="hbm2ddl.auto">update</property>--> <mapping resource="model/Student.hbm.xml"/> </session-factory></hibernate-configuration>
student.java
package model;/** * Created by dongyu.liu on 2017-5-22. */public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Student.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="model"> <!--table 属性不配就是默认--> <class name="Student" > <!-- id指属性 name 代表类中的属性 column可省略代表name和数据库名字一致--> <id name="id" > <generator class="increment"/> </id> <property name="name" /> <property name="age"/> </class></hibernate-mapping>
测试
package model;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * Created by dongyu.liu on 2017-5-22. */public class TestStudent { public static void main(String[] args) { Student s = new Student(); s.setId(1); s.setName("s1"); s.setAge(1); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session sessions = sf.openSession(); sessions.beginTransaction(); sessions.save(s); sessions.getTransaction().commit(); sf.close(); }}
遇到的问题
1 .xml文件无法编译到class文件夹中,在pom.xml中增加配置
<build> <finalName>hibernatetest</finalName> <!-- xml文件正常编译到class目录中--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
- The server time zone value ‘XXXXXX’ is unrecognized or represents more than one time zone.这个问题是时区不同步造成的,可以在hibernate.cfg.xml文件中在url后面添加参数:serverTimezone=UTC
<property name="connection.url">jdbc:mysql://localhost/hibernate?serverTimezone=UTC</property>
阅读全文
0 0
- Hibernate初印象(helloworld)-1
- 01hibernate初印象
- [笔记]软件体系结构(1)--模式初印象
- ITOO4.1之MongoDB初印象
- spring,hibernate和struts2三大框架整合初印象
- PB接触初印象
- VC++初印象
- ABAP/4初印象
- Servlet初印象
- Ruby初印象
- Rails初印象
- CSTC初印象
- WebLoad初印象
- C#初印象
- java初印象
- AndroidUI开发初印象
- Git初印象
- Macbookpro 初印象
- QT:日历(QCalendarWidget)
- 51nod1639 绑鞋带
- Installation failed with message INSTALL_CANCELED_BY_USER.
- 453. Minimum Moves to Equal Array Elements
- 基于PS和AI软件的PSB转AI再转SVG流程
- Hibernate初印象(helloworld)-1
- BZOJ1611: [Usaco2008 Feb]Meteor Shower流星雨
- SQLiteCantOpenDatabaseException: unable to open database file
- Javascript 题目01
- CNN中的梯度的求法和反向传播过程
- sqf开发环境配置
- ECSHOP编辑器Fckeditor上传图片中文名称乱码的解决方法
- Docker的安装配置及使用详解
- 【scrapy】学习Scrapy入门