Maven集成Hibernate示例
...大约 5 分钟
Maven集成Hibernate示例
1. 集成步骤
步骤1:POM添加依赖
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<!-- Joda-Time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<!-- To map JodaTime with database type -->
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>3.0.0.CR1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
主要添加了Spring,Hibernate,和 Mysql 连接器相关依赖。另外,由于我们使用了joda-time库来处理时间,所以也引入了joda-time依赖。usertype-core库引入是为了提供数据库时间类型与joda-time LocalDate之间的映射。
步骤2:配置Hibernate
package com.zszdevelop.hibernatedemo.configuration;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
/**
* @author 作者: zhangshengzhong
* @文件名: HibernateConfiguration
* @版本号:1.0
* @创建日期: 2020/10/9 11:37
* @描述:
*/
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.zszdevelop.hibernatedemo.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.zszdevelop.hibernatedemo.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
属性配置文件
application.properties/src/main/resources/application.properties
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mytest
jdbc.username = myuser
jdbc.password = mypassword
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = false
hibernate.format_sql = false
步骤3:Spring配置
@Configuration
@ComponentScan(basePackages = "com.zszdevelop.hibernatedemo")
public class AppConfig {
}
在我们这个示例中,即使该配置类内部是空的,但是使用了@ComponentScan注解,可以自动检测到对应包下所有的beans。
步骤4:DAO层
package com.zszdevelop.hibernatedemo.dao;
public class AbstractDao {
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public void persist(Object entity) {
getSession().persist(entity);
}
public void delete(Object entity) {
getSession().delete(entity);
}
}
注意,我们在步骤二创建的SessionFactory会被自动装配到这里,这个类将作为基类用于执行数据库相关操作。****
import com.zszdevelop.hibernatedemo.model.Employee;
import java.util.List;
public interface EmployeeDao {
void saveEmployee(Employee employee);
List<Employee> findAllEmployees();
void deleteEmployeeBySsn(String ssn);
Employee findBySsn(String ssn);
void updateEmployee(Employee employee);
}
package com.zszdevelop.hibernatedemo.dao.impl;
import java.util.List;
import com.zszdevelop.hibernatedemo.dao.AbstractDao;
import com.zszdevelop.hibernatedemo.dao.EmployeeDao;
import com.zszdevelop.hibernatedemo.model.Employee;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
@Repository("employeeDao")
public class EmployeeDaoImpl extends AbstractDao implements EmployeeDao {
public void saveEmployee(Employee employee) {
persist(employee);
}
@SuppressWarnings("unchecked")
public List<Employee> findAllEmployees() {
Criteria criteria = getSession().createCriteria(Employee.class);
return (List<Employee>) criteria.list();
}
public void deleteEmployeeBySsn(String ssn) {
Query query = getSession().createSQLQuery("delete from Employee where ssn = :ssn");
query.setString("ssn", ssn);
query.executeUpdate();
}
public Employee findBySsn(String ssn){
Criteria criteria = getSession().createCriteria(Employee.class);
criteria.add(Restrictions.eq("ssn",ssn));
return (Employee) criteria.uniqueResult();
}
public void updateEmployee(Employee employee){
getSession().update(employee);
}
步骤5:添加Service代码
import com.zszdevelop.hibernatedemo.model.Employee;
import java.util.List;
public interface EmployeeService {
void saveEmployee(Employee employee);
List<Employee> findAllEmployees();
void deleteEmployeeBySsn(String ssn);
Employee findBySsn(String ssn);
void updateEmployee(Employee employee);
}
package com.zszdevelop.hibernatedemo.service.impl;
package com.zszdevelop.hibernatedemo.service.impl;
import com.zszdevelop.hibernatedemo.dao.EmployeeDao;
import com.zszdevelop.hibernatedemo.model.Employee;
import com.zszdevelop.hibernatedemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author 作者: zhangshengzhong
* @文件名: EmployeeServiceImpl
* @版本号:1.0
* @创建日期: 2020/10/10 10:25
* @描述:
*/
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao dao;
public void saveEmployee(Employee employee) {
dao.saveEmployee(employee);
}
public List<Employee> findAllEmployees() {
return dao.findAllEmployees();
}
public void deleteEmployeeBySsn(String ssn) {
dao.deleteEmployeeBySsn(ssn);
}
public Employee findBySsn(String ssn) {
return dao.findBySsn(ssn);
}
public void updateEmployee(Employee employee){
dao.updateEmployee(employee);
}
}
步骤6:创建实体类POJO
package com.zszdevelop.hibernatedemo.model;
import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* @author 作者: zhangshengzhong
* @文件名: Employee
* @版本号:1.0
* @创建日期: 2020/10/10 10:24
* @描述:
*/
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "NAME", nullable = false)
private String name;
@Column(name = "JOINING_DATE", nullable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate joiningDate;
@Column(name = "SALARY", nullable = false)
private BigDecimal salary;
@Column(name = "SSN", unique=true, nullable = false)
private String ssn;
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 LocalDate getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(LocalDate joiningDate) {
this.joiningDate = joiningDate;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((ssn == null) ? 0 : ssn.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Employee))
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
if (ssn == null) {
if (other.ssn != null)
return false;
} else if (!ssn.equals(other.ssn))
return false;
return true;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", joiningDate="
+ joiningDate + ", salary=" + salary + ", ssn=" + ssn + "]";
}
}
这是一个标准的实体类,基于JPA注解@Entity
, @Table
, @Column以及hibernate注解@Type(用于提供数据库类型与Joda-Time LocalDate的映射)。
步骤7:在数据库里创建表
CREATE TABLE EMPLOYEE(
id INT NOT NULL auto_increment,
name VARCHAR(50) NOT NULL,
joining_date DATE NOT NULL,
salary DOUBLE NOT NULL,
ssn VARCHAR(30) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
步骤8:创建main方法执行程序
package com.zszdevelop.hibernatedemo;
import com.zszdevelop.hibernatedemo.configuration.AppConfig;
import com.zszdevelop.hibernatedemo.model.Employee;
import com.zszdevelop.hibernatedemo.service.EmployeeService;
import org.joda.time.LocalDate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import java.math.BigDecimal;
import java.util.List;
/**
* @author 作者: zhangshengzhong
* @文件名: AppMain
* @版本号:1.0
* @创建日期: 2020/10/10 10:26
* @描述:
*/
public class AppMain {
public static void main(String args[]) {
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
EmployeeService service = (EmployeeService) context.getBean("employeeService");
/*
* Create Employee1
*/
Employee employee1 = new Employee();
employee1.setName("Han Yenn");
employee1.setJoiningDate(new LocalDate(2010, 10, 10));
employee1.setSalary(new BigDecimal(10000));
employee1.setSsn("ssn00000001");
/*
* Create Employee2
*/
Employee employee2 = new Employee();
employee2.setName("Dan Thomas");
employee2.setJoiningDate(new LocalDate(2012, 11, 11));
employee2.setSalary(new BigDecimal(20000));
employee2.setSsn("ssn00000002");
/*
* Persist both Employees
*/
service.saveEmployee(employee1);
service.saveEmployee(employee2);
/*
* Get all employees list from database
*/
List<Employee> employees = service.findAllEmployees();
for (Employee emp : employees) {
System.out.println(emp);
}
/*
* delete an employee
*/
service.deleteEmployeeBySsn("ssn00000002");
/*
* update an employee
*/
Employee employee = service.findBySsn("ssn00000001");
employee.setSalary(new BigDecimal(50000));
service.updateEmployee(employee);
/*
* Get all employees list from database
*/
List<Employee> employeeList = service.findAllEmployees();
for (Employee emp : employeeList) {
System.out.println(emp);
}
context.close();
}
}
运行以上程序,结果如下:
Employee [id=1, name=Han Yenn, joiningDate=2010-10-10, salary=10000, ssn=ssn00000001]
Employee [id=2, name=Dan Thomas, joiningDate=2012-11-11, salary=20000, ssn=ssn00000002]
Employee [id=1, name=Han Yenn, joiningDate=2010-10-10, salary=50000, ssn=ssn00000001]
参考文章
赞助