第一个Hibernate项目

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

一、构建Hibernate项目

    1.新建Java项目HibernateDemo1

    2.导入Hibernate下的jar包(lib->required下的所有jar包)+jdbc驱动包

    3.导入hibernate.cfg.xml文件到src目录下(在Hibernate文件目录中搜索*.cfg.xml)

     配置该文件如下:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
         <!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.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</ property >
             < property  name = "hibernate.connection.driver_class" >com.mysql.jdbc.Driver</ property >
             < property  name = "hibernate.connection.url" >jdbc:mysql:///hibernate_db</ property >
             < property  name = "hibernate.connection.username" >root</ property >
         < property  name = "hibernate.connection.password" >root</ property >
         < property  name = "hibernate.hbm2ddl.auto" >update</ property >
         < property  name = "hibernate.show_sql" >true</ property >
         < property  name = "hibernate.format_sql" >true</ property >
         < mapping  resource = "com\eduask\pojo\Person.hbm.xml"  />
         </ session-factory >
         </ hibernate-configuration >

     4.建立mysql数据库hibernate_db

    5.在src目录下建两个包com.eduask.pojo、com.eduask.test

      pojo包下建一个Person类(Person.java),内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package  com.eduask.pojo;
 
import  java.util.Date;
 
public  class  Person {
     private  Integer id;
     private  String name;
     private  int  password;
     private  Date birthday;
     
     public  Person() {}
 
     public  Person(String name,  int  password, Date birthday) {
         super ();
         this .name = name;
         this .password = password;
         this .birthday = birthday;
     }
 
     @Override
     public  String toString() {
         return  "Person [id="  + id +  ", name="  + name +  ", password="  + password +  ", birthday="  + birthday +  "]" ;
     }
 
     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;
     }
 
     public  int  getPassword() {
         return  password;
     }
 
     public  void  setPassword( int  password) {
         this .password = password;
     }
 
     public  Date getBirthday() {
         return  birthday;
     }
 
     public  void  setBirthday(Date birthday) {
         this .birthday = birthday;
     }
     
     
}

    pojo包下引入xml文件Person.hbm.xml,(Hibernate包中搜索),修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
<? xml  version = "1.0" ?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
     < class  name = "com.eduask.pojo.Person"  table = "t_person" >
         < id  name = "id" >
             < generator  class = "native" />
         </ id >
         < property  name = "name"  column = "t_name" ></ property >
         < property  name = "password"  length = "6" ></ property >
         < property  name = "birthday" ></ property >
     </ class >
</ hibernate-mapping >

    test包中新建测试类HibernateTest.java,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package  com.eduask.test;
 
import  java.util.List;
 
import  org.hibernate.Criteria;
import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;
 
import  com.eduask.pojo.Person;
 
public  class  HibernateTest {
     public  static  void  main(String[] args) {
         Configuration config =  new  Configuration().configure();
         SessionFactory factory = config.buildSessionFactory();
         Session session = factory.openSession();
         Transaction tx = session.beginTransaction();
         Person p =  new  Person( "admin" , 123456 , new  java.util.Date()); 
         Criteria c = session.createCriteria(Person. class );
         List<Person> lists = c.list();
         System.out.println(lists.get( 0 ).getName());
         tx.commit();
         session.close();
         factory.close();
     }
}

    运行测试类,结果如下:

Hibernate: 

    insert 

    into

        t_person

        (t_name, password, birthday) 

    values

        (?, ?, ?)

八月 11, 2016 5:17:14 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop

INFO: HHH000030: Cleaning up connection pool [jdbc:mysql:///hibernate_db]



同时在数据库中可以看到t_person表已被创建以及插入的响应数据。



本文转自yeleven 51CTO博客,原文链接:http://blog.51cto.com/11317783/1836998
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
SQL Java 数据库连接
从理论到实践:Hibernate与JPA在Java项目中的实际应用
本文介绍了Java持久层框架Hibernate和JPA的基本概念及其在具体项目中的应用。通过一个在线书店系统的实例,展示了如何使用@Entity注解定义实体类、通过Spring Data JPA定义仓库接口、在服务层调用方法进行数据库操作,以及使用JPQL编写自定义查询和管理事务。这些技术不仅简化了数据库操作,还显著提升了开发效率。
272 3
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
496 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
SQL Java 数据库连接
从理论到实践:Hibernate与JPA在Java项目中的实际应用
【6月更文挑战第25天】在Java持久层,Hibernate与JPA提供ORM及数据库操作简化。JPA是EE规范,定义ORM接口;Hibernate是其实现,功能丰富。在一个在线书店项目中,使用@Entity标注实体类如Book,通过JpaRepository接口(如BookRepository)进行数据访问。服务层调用仓库接口方法,如搜索书籍。当需自定义查询时,可使用JPQL或SQL。Spring的@Transactional注解处理事务管理,展示出高效开发流程。
139 0
|
XML Java 关系型数据库
Spring 项目快速整合 Hibernate
前言 Hibernate 作为前些年广为流行的 ORM 框架,Spring 在诞生之初也进行了支持,并且抽象出一个 spring-orm 模块。
611 0
|
Java 数据库连接 Spring
使用Hibernate JSP 303 整合到SSM项目
使用Hibernate JSP 303 整合到SSM项目
116 0
|
Java 数据库连接 Spring
使用Hibernate JSP 303 整合到SSM项目
下面是具体的工具类,当然不一定需要使用ApplicationContextAware进行校验替换,也可以直接作为一个validater的工具使用,但是建议使用bean的形式而不是使用静态方法。
209 0
|
Java 数据库连接 Spring
使用Hibernate JSP 303 整合到SSM项目
使用Hibernate JSP 303 整合到SSM项目
526 0
|
SQL Java 数据库连接
SSH项目,hibernate的查询操作出错org.hibernate.hql.ast.QuerySyntaxException
SSH项目,hibernate的查询操作出错org.hibernate.hql.ast.QuerySyntaxException
468 0
|
Java 数据库连接 应用服务中间件
项目集成hibernate后,启动时报错java.lang.NoSuchMethodError
在项目集成hibernate后,启动时报错 org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'applicationInitializer' defined in file[D:\地址.
1673 0
|
Web App开发 Java 数据库连接
【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过程中介绍。
2205 0

热门文章

最新文章