Hibernate框架搭建流程(使用IEDA的maven搭建)
1.创建项目与添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>cn.bdqn.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/antlr/antlr -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.6rc1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javassist/javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.0.GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.transaction/jta -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.Facetst添加Hibernate
3.连接数据库
4.自动生成对应的pojo类与xml
5.完善hibernate.cfg.xml配置文件
6.Util包下创建HibernateUtil类
package cn.bdqn.test.Util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static Configuration configuration;
public final static SessionFactory sessionFactory;
//初始化Configuration和SessionFactory
static {
try {
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}catch (HibernateException ex){
throw new ExceptionInInitializerError(ex);
}
}
public HibernateUtil(){};
//获取session对象
public static Session curren*tSession(){
return sessionFactory.getCurrentSession();
}
}
7.Dao的实现类编写
ps :语句必须写在事务中
事务可以写在Dao的实现类或service的实现类
package cn.bdqn.test.dao;
import cn.bdqn.test.Util.HibernateUtil;
import cn.bdqn.test.pojo.District;
import cn.bdqn.test.pojo.Users;
import cn.bdqn.test.service.UsersService;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class UsersDaoImpl implements UsersDao {
@Override
public Users getUsers(int id) {
Transaction tx = null;
Users users = null;
try{
//开启事务
// Session session = HibernateUtil.currentSession();
tx = HibernateUtil.currentSession().beginTransaction();
users = (Users) HibernateUtil.currentSession().get(Users.class,id);
tx.commit();
}catch (HibernateException e){
e.printStackTrace();
if (tx!=null){
tx.rollback();
}
}
return users;
}
@Override
public boolean insertUsers(Users users) {
Transaction tx = null;
try{
//开启事务
// Session session = HibernateUtil.currentSession();
tx = HibernateUtil.currentSession().beginTransaction();
HibernateUtil.currentSession().save(users);
tx.commit();
return true;
}catch (HibernateException e){
e.printStackTrace();
if (tx!=null){
tx.rollback();
}
return false;
}
}
@Override
public boolean delete(Users users) {
Transaction tx = null;
try{
//开启事务
// Session session = HibernateUtil.currentSession();
tx = HibernateUtil.currentSession().beginTransaction();
Users users1 =(Users) HibernateUtil.currentSession().get(Users.class,users.getId());
if(users1!=null){
HibernateUtil.currentSession().delete(users1);
}
tx.commit();
return true;
}catch (Exception e){
e.printStackTrace();
if (tx!=null){
tx.rollback();
}
return false;
}
}
@Override
public boolean update(Users users) {
Transaction tx = null;
try{
//开启事务
tx = HibernateUtil.currentSession().beginTransaction();
HibernateUtil.currentSession().update(users);
HibernateUtil.currentSession().saveOrUpdate(users);
HibernateUtil.currentSession().merge(users);
tx.commit();
return true;
}catch (HibernateException e){
e.printStackTrace();
if (tx!=null){
tx.rollback();
}
return false;
}
}
}