How to create a Maven project with Hibernate libs?

This blog will be a demo to describe this issue.

Using tools:

1. eclipse

2. maven

Now, we should look at the project structure :

the result at the console :

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hibernate-4-maven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hibernate-4-maven ---
[INFO] Deleting D:\Development\j2ee\workspace\hibernate-4-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hibernate-4-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hibernate-4-maven ---
[INFO] Compiling 2 source files to D:\Development\j2ee\workspace\hibernate-4-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hibernate-4-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Development\j2ee\workspace\hibernate-4-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hibernate-4-maven ---
[INFO] Compiling 1 source file to D:\Development\j2ee\workspace\hibernate-4-maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hibernate-4-maven ---
[INFO] Surefire report directory: D:\Development\j2ee\workspace\hibernate-4-maven\target\surefire-reports -------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.b510.hongten.hibernate4maven.TestUser
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate:
select
user0_.id as id0_,
user0_.age as age0_,
user0_.email as email0_,
user0_.gender as gender0_,
user0_.name as name0_
from
tab_user user0_
Id : 1 Name : Hongten Gender : M Age : 18 email : hongtenzone@foxmail.com
Hibernate:
insert
into
tab_user
(age, email, gender, name)
values
(?, ?, ?, ?)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.298 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.151 s
[INFO] Finished at: 2014-11-01T23:32:56+08:00
[INFO] Final Memory: 11M/27M
[INFO] ------------------------------------------------------------------------

the result at the database :

==========================================================

// Source Code:

==========================================================

/hibernate-4-maven/src/main/java/com/b510/hongten/hibernate4maven/User.java

/**
*
*/
package com.b510.hongten.hibernate4maven; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; /**
* @author Hongten
* @created 2014年11月1日
*/
@Entity
@Table(name = "tab_user")
public class User { @Id
@GeneratedValue
private Integer id;
private String name;
@Column(columnDefinition = "int default 0")
private Integer age;
private String gender;
private String email; 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 Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
}
}

/hibernate-4-maven/src/main/java/com/b510/hongten/hibernate4maven/util/HibernateUtil.java

/**
*
*/
package com.b510.hongten.hibernate4maven.util; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration; /**
* @author Hongten
* @created 2014年11月1日
*/
public class HibernateUtil { private static final SessionFactory sessionFactory; static {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} public static Session getOpenSession() {
return sessionFactory.openSession();
} public static Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}

/hibernate-4-maven/src/test/java/com/b510/hongten/hibernate4maven/TestUser.java

/**
*
*/
package com.b510.hongten.hibernate4maven; import java.util.List;
import java.util.Random; import junit.framework.Assert; import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test; import com.b510.hongten.hibernate4maven.util.HibernateUtil; /**
* @author Hongten
* @created 2014年11月1日
*/
public class TestUser { @Test
public void testSave() {
Session session = HibernateUtil.getOpenSession();
session.beginTransaction();
User user = new User();
user.setAge(new Random().nextInt(22));
user.setName("Hongten");
user.setGender("M");
user.setEmail("hongtenzone@foxmail.com"); session.save(user); Assert.assertEquals(true, user.getId() > 0); session.getTransaction().commit();
} @Test
public void testUpdate() {
Session session = HibernateUtil.getOpenSession();
session.beginTransaction();
Query query = session.createQuery("from User");
List<User> list = query.list();
for (User u : list) {
System.out.println("Id : " + u.getId() + " Name : " + u.getName()
+ " Gender : " + u.getGender() + " Age : " + u.getAge()
+ " email : " + u.getEmail());
}
session.getTransaction().commit();
}
}

/hibernate-4-maven/src/main/resources/hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/users
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property> <!-- Add POJO to here -->
<mapping class="com.b510.hongten.hibernate4maven.User" /> </session-factory> </hibernate-configuration>

/hibernate-4-maven/src/main/resources/log4j.properties

 log4j.rootLogger=debug,consolelog4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%r  [%t]    %p - %c - %l - %m%n

/hibernate-4-maven/pom.xml

<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.b510.hongten</groupId>
<artifactId>hibernate-4-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>hibernate-4-maven</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<!-- Configuration for Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!-- Configuration for mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
<!-- Configuration for log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Configuration for junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Source Code Download: http://files.cnblogs.com/hongten/hibernate-4-maven.rar

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

Hibernate4Maven的更多相关文章

随机推荐

  1. Ultra-QuickSort【归并排序典型题目】

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34470   Accepted: 12382 ...

  2. 基于MyEclipse6.5的ssh整合

    1.编写目的 为了学习,为了更好的学习java. 为了让想要学习这个整合的人少走弯路! ! ! 2.实验环境 l MyEclipse6.5 l JBoss4.2.1 l SQL2005 l 数据库脚本 ...

  3. 时间编程,王明学learn

    时间编程 一.时间类型 Coordinated Universal Time(UTC):世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT) Calend ...

  4. Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)

    题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...

  5. 智能车学习(八)——菜单的实现

    一.代码分享 1.头文件 #ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu ...

  6. Ubuntu14 搭载vim环境查看源码

    首先是下载完整的vim74,然后编译安装.遗憾的是当编译时,没有开启图形界面. 在安装新版本的Vim之前,你需要卸载原来安装的老版本Vim,依次在终端下执行下列命令: sudo apt-get rem ...

  7. LoadRunner 一些快捷键

    F4   Runtime Setting F5   运行 Ctrl Alt C  注释 Ctrl Alt U  取消注释 Tab  缩进 Shift Tab  取消缩进 Ctrl L  打开参数管理对 ...

  8. LR连接oracle时出现:SQLState=28000[Oracle][ODBC][Ora]ORA-01017:invalid username/password;logon denied

    出现的现象:

  9. strust.xml

    使用strust2框架,实现跳转,请求对应路径 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTY ...

  10. 简单区分VMware的三种网络连接模式(bridged、NAT、host-only)

    艺搜简介 VMware在安装时默认安装了两块虚拟网卡,VMnet1和VMnet8,另外还有VMnet0.这些虚拟网卡的配置都是由Vmware虚拟机自动生成的,一般来说不需要用户自行设置. Vmware ...