hibernate4.3入门教程,使用maven配置
来源:互联网 发布:php soapheader详解 编辑:程序博客网 时间:2024/06/04 07:22
本文介绍hibernate4的maven配置,
pom.xml:
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
59
60
61
62
63
64
65
66
67
68
69
70<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abigdreamer.ark</groupId>
<artifactId>data-permission</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>data-permission</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.hibernate.version>4.1.2</org.hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- hibernate 4.3.0 start -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Alpha1</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.4.Final</version>
</dependency>
<!-- hibernate 4.3.0 end -->
</dependencies>
</project>
Hibernate.cfg.xml的配置:
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<?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,Sql2000与2005有区别,此处是SQL2005的 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/permission</property>
<property name="connection.username">root</property>
<property name="connection.password">darkness</property>
<!-- JDBC connection pool (use the built-in) Hibernate自带的连接池,一般不用,注释掉! -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect(方言)用于指定何种数据库,可以在Hibernate的文档中查到 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache 二级缓存,优化Hibernate时再说! -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout 是否显示生成的SQL生成语句!开发时,一定要显示,方便调试! -->
<property name="show_sql">true</property>
<!-- 使显示的sql格式化 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup hbm是Hibernate Maping
ddl是数据定义语句 这句话的意思是:是否要Hibernate指定映射成数据定义语句。 -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<!--设置自动生成数据库表,update时自动更新表结构
<property name="hibernate.hbm2ddl.auto">create</property>
-->
<mapping resource="com/abigdreamer/ark/permission/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
配置好hibernate后,我们来写一个测试,需要一个Entity类User,代码如下:
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
46package com.abigdreamer.ark.permission;
/**
*
*
* @author Darkness
* @date 2013-6-7 上午11:12:55
* @website http://www.abigdreamer.com
* @version V1.0
*/
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public User() {
}
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;
}
}
User的hbm配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.abigdreamer.ark.permission">
<class name="User" table="user">
<!-- 如果是Model类和表名一样,则table属性可以省略 -->
<id name="id" column="id"></id>
<!-- 指定数据库主键, name指定Model类中的属性,column属性指定表中的列。如果是Model类属性和列名一样,则column属性可以省略 -->
<property name="name" column="name"></property><!-- 指定其他属性 -->
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
最终写个测试类来测试配置是否成功,UserTest代码如下:
注:4.3中create session的方法又改变了
声明: 本文由(造梦师)整理编译,转载请保留链接:http://www.abigdreamer.com/programming/frameworkstudy/maven-configuration-hibernate4.html1
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
59
60package com.abigdreamer.ark.permission;
import org.hibernate.HibernateException;
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.Test;
import com.abigdreamer.ark.permission.User;
/**
*
* @author Darkness
* @date 2013-6-7 上午11:14:23
* @website http://www.abigdreamer.com
* @version V1.0
*/
public class UserTest {
@Test
public void insertUser() {
User s = new User();
s.setId(3);
s.setName("darkness");
s.setAge(1);
Transaction tx = null;
try {
// 默认读取的是sre下面的hibernate.cfg.xml
/** 此方法在Hibernate4中被标记为过时 */
// SessionFactory sf = new Configuration().configure().buildSessionFactory();
/**Hibernate4取得SessionFactory的方法 */
Configuration cfg = new Configuration().configure();
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); StandardServiceRegistry sr = srb.build(); SessionFactory factory = cfg.buildSessionFactory(sr);
Session session = factory.openSession(); tx = session.beginTransaction();
session.save(s);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
}
}
}
0 0
- hibernate4.3入门教程,使用maven配置
- hibernate4.3入门教程,使用maven配置
- Maven+Hibernate4.3.5+c3p0配置
- maven使用入门教程
- Spring4+Hibernate4+Mybatis Maven的pom配置
- Hibernate4.3环境配置
- Hibernate4.2.2使用Annotation配置
- 使用maven整合struts2+hibernate4+spring3
- maven详细安装配置入门教程
- Hibernate4入门教程:Hello World!
- Spring+Hibernate4配置使用cache的问题
- hibernate4简要配置及基本使用
- 使用注解配置ehcache,环境:Spring4、hibernate4
- Hibernate4之二级缓存配置与使用
- hibernate-配置使用二级缓存(hibernate4以下版本)
- hibernate4配置
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- 特征码免杀
- Servlet
- MFC DLL静态链接
- hadoop学习.分布式计算开源框架Hadoop介绍
- SGU110 计算几何 Computational Geometry
- hibernate4.3入门教程,使用maven配置
- 基督敎의 敎理
- 三种查看iPhone和iPad的UDID的简单方法
- 三敎 敎理의 相互關聯
- 为SQLPLUS加上Linux命令行的功能
- u-boot移植
- 浅谈 Oracle 监听器
- 硬盘引导扇区、多分区图、不通硬盘的LINUX逻辑分区数量
- WMIC命令