Crud for a generic DAO based on annotated SSH
Configuration file
Web.xml configuration file
———————————————————————————————————
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
springUtf8Encoding
org.springframework.web.filter.CharacterEncodingFilter
springUtf8Encoding
/*
osiv
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
osiv
*.action
*.jsp
< listener > < listener – class > org. Springframework. Web. Util. IntrospectorCleanupListener < / listener – class >
JDBC configuration properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tour
jdbc.username=root
jdbc.password=1111
———————————————————————————————————
Log4j properties configuration slightly
ehcache.xml
———————————————————————————————————
Struts. XML configuration file
< constant name = “struts. Serve. Static. BrowserCache” value = “false” > < / constant >
< constant name = “struts. The enable. DynamicMethodInvocation” value = “false” / >
< constant name = “struts. The configuration. The XML. Reload” value = “false” > < / constant >
/list.jsp
/update.jsp
/input.jsp
</struts>
Description: Basic CRUD
———————————————————————————————————
Spring configuration file
bean.xml
Note: Based on IOC annotations, transactions are configuration files, Hibernate configuration is also in XML, entities are based on JPA annotations
< beans XMLNS = “www.springframework.org/schema/bean…” XMLNS: xsi = “www.w3.org/2001/XMLSch…” XMLNS: context = “www.springframework.org/schema/cont…” xmlns:aop=”www.springframework.org/schema/aop” xmlns:tx=”www.springframework.org/schema/tx” Xsi: schemaLocation = “HTTP: / / www.springframework.org/schema/bean… www.springframework.org/schema/bean… www.springframework.org/schema/cont… www.springframework.org/schema/cont… www.springframework.org/schema/aop www.springframework.org/schema/aop/… www.springframework.org/schema/tx www.springframework.org/schema/tx/s…” >
<context:component-scan base-package=”com.lm”/>
<context:annotation-config></context:annotation-config>
< context: the property – placeholder location = “classpath: JDBC. Properties” / >
< bean id = “dataSource” destroy – method = “close” class = “com.mchange.v2.c3p0.Com boPooledDataSource” >
com.lm.entity
</beans>
Description:
Why are transactions typically configured in services?
The essence of a transaction is that you need to operate the database from the same time, either complete, or rollback undo all operations, in the DAO layer of the database operation is usually simple, there is no business logic, so for this kind of added a transaction is also ok, but not necessary; However, in the Service layer, the methods in the service layer usually contain some complicated logic. A method may call multiple dao methods, so transaction management must be implemented. Either all DAO methods in the service method are executed or all DAO methods are destroyed. In this way, the correctness of database data can be guaranteed.
———————————————————————————————————
Background architecture Java file
Physical entity:
package com.lm.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name=”stu_tab”)
public class Student implements Serializable{
private static final long serialVersionUID = -3791589693634535265L; @Id @GeneratedValue private int id; @Column(name=”name”) private String name; @Column(name=”age”) private int age; public Student() { // TODO Auto-generated constructor stub } 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; } @Override public String toString() { return “Student [id=” + id + “, name=” + name + “, age=” + age + “]”; }}
———————————————————————————————————
Dao layer
BaseDao:
package com.lm.dao.comm;
import java.util.List;
public interface BaseDao<T>{
public void add(T entity);
public void del(int id);
public void update(T entity);
public T get(int id);
public T load(int id);
List<T> selectAll();
}
———————————————————————————————————
BaseDaoImpl:
package com.lm.dao.comm.impl;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.lm.dao.comm.BaseDao;
public class BaseDaoImpl
implements BaseDao
{ @Autowired private SessionFactory sessionFactory; public Session getSession(){ return sessionFactory.getCurrentSession(); } private Class
clazz; ParameterizedType Type =(ParameterizedType) public BaseDaoImpl() {ParameterizedType =(ParameterizedType) this.getClass().getGenericSuperclass(); this.clazz=(Class
) type.getActualTypeArguments()[0]; } @Override public void add(T entity) { // TODO Auto-generated method stub getSession().save(entity); }
@Override
public void del(int id) {
// TODO Auto-generated method stub
T t=get(id);
getSession().delete(t);
}
@Override
public void update(T entity) {
// TODO Auto-generated method stub
getSession().update(entity);
}
@Override
public T get(int id) {
// TODO Auto-generated method stub
return (T)getSession().get(clazz, id);
}
@Override
public T load(int id) {
// TODO Auto-generated method stub
T t=(T)getSession().load(clazz, id);
return t;
}
@Override
public List<T> selectAll() {
System.out.println(“–“+this.clazz.getSimpleName());
String hql=”from “+this.clazz.getSimpleName();
Query query=getSession().createQuery(hql);
List<T> list=(List<T>)query.list();
return list;
}
}
StudentDao :
package com.lm.dao;
import com.lm.dao.comm.BaseDao;
import com.lm.entity.Student;
public interface StudentDao extends BaseDao<Student> {
}
StudentDaoImpl:
package com.lm.dao.impl;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.lm.dao.StudentDao;
import com.lm.dao.comm.impl.BaseDaoImpl;
import com.lm.entity.Student;
@Repository(“studentDao”) public class StudentDaoImpl extends BaseDaoImpl
implements StudentDao { @Override public Session getSession() { // TODO Auto-generated method stub return super.getSession(); }}
———————————————————————————————————
The service layer
StudentService:
package com.lm.service;
import java.util.List;
import com.lm.entity.Student;
public interface StudentService {
public Student get(int id);
public void save(Student stu);
public List<Student> findAll();
public void delete(int id);
public void update(Student stu);
}
StudentServiceImpl:
package com.lm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lm.dao.StudentDao;
import com.lm.entity.Student;
import com.lm.service.StudentService;
@Service(“studentService”)
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student get(int id) {
Student student = studentDao.get(id);
return student;
}
@Override
public void save(Student stu) {
studentDao.add(stu);
}
@Override
public List<Student> findAll() {
// TODO Auto-generated method stub
return studentDao.selectAll();
}
@Override
public void delete(int id) {
studentDao.del(id);
}
@Override
public void update(Student stu) {
studentDao.update(stu);
}
}
———————————————————————————————————
The Controller control layer
StudentAction:
package com.lm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.lm.entity.Student;
import com.lm.service.StudentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller(“studentAction”)
public class StudentAction extends ActionSupport implements ModelDriven<Student> {
private static final long serialVersionUID = 9325690977237111L;
public Student stu;
@Autowired
public StudentService studentService;
@Override
public Student getModel() {
if(stu==null)
{
stu=new Student();
}
return stu;
}
public String add(){
studentService.save(stu);
return list();
}
public String update(){
studentService.update(stu);
return list();
}
public String list(){
List<Student> list=(List<Student>)studentService.findAll();
ActionContext.getContext().put(“stuList”, list);
return “list”;
}
public String delete(){
studentService.delete(stu.getId());
return list();
}
public String edit(){
Student stu1=studentService.get(stu.getId());
ActionContext.getContext().put(“stu”, stu1);
return “update”;
}
}
———————————————————————————————————
Omit JSP page