Hibernate开发步骤
1、创建hibernate配置文件
<?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>
<!-- hibernate.connection.driver_class : 连接数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- hibernate.connection.username : 连接数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- hibernate.connection.password : 连接数据库的密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- hibernate.connection.url : 连接数据库的地址,路径 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/shop</property>
<!-- show_sql: 操作数据库时,会 向控制台打印sql语句 -->
<property name="show_sql">true</property>
<!-- format_sql: 打印sql语句前,会将sql语句先格式化 -->
<property name="format_sql">true</property>
<!-- hbm2ddl.auto: 生成表结构的策略配置
update(最常用的取值): 如果当前数据库中不存在表结构,那么自动创建表结构.
如果存在表结构,并且表结构与实体一致,那么不做修改
如果存在表结构,并且表结构与实体不一致,那么会修改表结构.会保留原有列.
create(很少):无论是否存在表结构.每次启动Hibernate都会重新创建表结构.(数据会丢失)
create-drop(极少): 无论是否存在表结构.每次启动Hibernate都会重新创建表结构.每次Hibernate运行结束时,删除表结构.
validate(很少):不会自动创建表结构.也不会自动维护表结构.Hibernate只校验表结构. 如果表结构不一致将会抛出异常.
-->
<property name="hbm2ddl.auto">update</property>
<!-- 数据库方言配置
org.hibernate.dialect.MySQLDialect (选择最短的)
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- hibernate.connection.autocommit: 事务自动提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 引入ORM 映射文件
填写src之后的路径
-->
<mapping resource="hibernate/hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2、创建实体类
1 package hibernate;
2
3 import java.sql.Date;
4
5 public class News {
6 private Integer id;
7 private String title;
8 private String author;
9
10 public News() {
11
12 }
13
14 public News(String title, String author) {
15 super();
16 this.title = title;
17 this.author = author;
18 }
19
20
21
22 public Integer getId() {
23 return id;
24 }
25
26 public void setId(Integer id) {
27 this.id = id;
28 }
29
30 public String getTitle() {
31 return title;
32 }
33
34 public void setTitle(String title) {
35 this.title = title;
36 }
37
38 public String getAuthor() {
39 return author;
40 }
41
42 public void setAuthor(String author) {
43 this.author = author;
44 }
45
46
47 @Override
48 public String toString() {
49 return "News [id=" + id + ", title=" + title + ", author=" + author + ", ]";
50 }
51
52
53 }
3、创建对象-关系对应文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.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>
</class>
</hibernate-mapping>
4、通过Hibernate API编写访问数据库的代码
1 public void add() {
2 /**创建Session工厂*/
3 SessionFactory sessionFactory = null;
4 /**configure():读取hibernate配置文件,*/
5 Configuration configuration = new Configuration().configure();
6
7 /**创建Session工厂*/
8 sessionFactory = configuration.buildSessionFactory();
9
10 /**获取Session对象*/
11 Session session = sessionFactory.openSession();
12 /**开启事物*/
13 Transaction transaction = session.beginTransaction();
14 /**执行保存操作*/
15 News news = new News("lisi","数学");
16
17 session.save(news);
18 /**提交事物*/
19 transaction.commit();
20 session.close();
21 sessionFactory.close();
22 }