#Database related properties
database.driverClass=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/letv_test
database.username=root
database.password=123456
#Hibernate related properties
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
#Connection pool related properties
connection.acquireIncrement=2
connection.minPoolSize=20
connection.maxPoolSize=50
connection.maxIdleTime=600
7、TestEntity.java:
package com.busymonkey.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Employees")
public class TestEntity implements Serializable{
private static final long serialVersionUID = -7988799579036225137L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
@Column
private int age;
@Column
private float salary;
public TestEntity() {
super();
}
public TestEntity(String name, int age, float salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public long getId() {
return id;
}
public void setId(long 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;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
8、TestUtils.java:
package com.busymonkey.utils;
import java.io.Serializable;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class TestUtils {
@Autowired
private SessionFactory sessionFactory;
public Serializable create(final T entity) {
return sessionFactory.getCurrentSession().save(entity);
}
public T update(final T entity) {
sessionFactory.getCurrentSession().update(entity);
return entity;
}
public void delete(final T entity) {
sessionFactory.getCurrentSession().delete(entity);
}
public void delete(Serializable id, Class entityClass) {
T entity = fetchById(id, entityClass);
delete(entity);
}
@SuppressWarnings("unchecked")
public List fetchAll(Class entityClass) {
return sessionFactory.getCurrentSession().createQuery(" FROM "+entityClass.getName()).list();
}
@SuppressWarnings("unchecked")
public List fetchAll(String query) {
return sessionFactory.getCurrentSession().createQuery(query).list();
}
@SuppressWarnings("unchecked")
public T fetchById(Serializable id, Class entityClass) {
return (T)sessionFactory.getCurrentSession().get(entityClass, id);
}
}
9、TestDao.java:
package com.busymonkey.dao;
import java.util.List;
import com.busymonkey.entity.TestEntity;
public interface TestDao {
public long createEmployee(TestEntity employee);
public TestEntity updateEmployee(TestEntity employee);
public void deleteEmployee(long id);
public List getAllEmployees();
public TestEntity getEmployee(long id);
public List getAllEmployees(String employeeName);
}
10、TestDaoImpl.java:
package com.busymonkey.dao.impl;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.busymonkey.dao.TestDao;
import com.busymonkey.entity.TestEntity;
import com.busymonkey.utils.TestUtils;
@Repository
public class TestDaoImpl implements TestDao {
public TestDaoImpl() {
System.out.println("EmployeeDAOImpl");
}
@Autowired
private TestUtils hibernateUtil;
@Override
public long createEmployee(TestEntity employee) {
return (Long) hibernateUtil.create(employee);
}
@Override
public TestEntity updateEmployee(TestEntity employee) {
return hibernateUtil.update(employee);
}
@Override
public void deleteEmployee(long id) {
TestEntity employee = new TestEntity();
employee.setId(id);
hibernateUtil.delete(employee);
}
@Override
public List getAllEmployees() {
return hibernateUtil.fetchAll(TestEntity.class);
}
@Override
public TestEntity getEmployee(long id) {
return hibernateUtil.fetchById(id, TestEntity.class);
}
@SuppressWarnings("unchecked")
@Override
public List getAllEmployees(String employeeName) {
String query = "SELECT e.* FROM Employees e WHERE e.name like '%"+ employeeName +"%'";
List
11、TestService.java:
package com.busymonkey.service;
import java.util.List;
import com.busymonkey.entity.TestEntity;
public interface TestService {
public long createEmployee(TestEntity employee);
public TestEntity updateEmployee(TestEntity employee);
public void deleteEmployee(long id);
public List getAllEmployees();
public TestEntity getEmployee(long id);
public List getAllEmployees(String employeeName);
}
12、TestServiceImpl.java:
package com.busymonkey.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.busymonkey.dao.TestDao;
import com.busymonkey.entity.TestEntity;
import com.busymonkey.service.TestService;
@Service
@Transactional
public class TestServiceImpl implements TestService {
public TestServiceImpl() {
System.out.println("EmployeeServiceImpl()");
}
@Autowired
private TestDao employeeDAO;
@Override
public long createEmployee(TestEntity employee) {
return employeeDAO.createEmployee(employee);
}
@Override
public TestEntity updateEmployee(TestEntity employee) {
return employeeDAO.updateEmployee(employee);
}
@Override
public void deleteEmployee(long id) {
employeeDAO.deleteEmployee(id);
}
@Override
public List getAllEmployees() {
return employeeDAO.getAllEmployees();
}
@Override
public TestEntity getEmployee(long id) {
return employeeDAO.getEmployee(id);
}
@Override
public List getAllEmployees(String employeeName) {
return employeeDAO.getAllEmployees(employeeName);
}
}
lua:
local access_token = ngx.var.cookie_SGAccessToken
if access_token then
ngx.header["Set-Cookie"] = "SGAccessToken="..access_token.."; path=/;Max-Age=3000"
end