前言
最近因为个人需求,需要一份SSH+maven的简单模版,然后开始各种百度谷歌github找,发现找到的很多的网上的demo要么使用的版本太低(spring3+hibernate3之类),要么就是太复杂不太适合我的需求(仅仅想要一个CURD的项目就足够)
于是最后还是决定自己写一个。
github:https://github.com/GoldButcher/SSH
项目路径:
既然是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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ssh</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<properties>
<spring.version>4.1.5.RELEASE</spring.version>
<hibernate.version>4.3.8.Final</hibernate.version>
<mysql.version>5.1.10</mysql.version>
<junit-version>4.11</junit-version>
<servlet-api-version>3.1.0</servlet-api-version>
<jsp-version>2.1</jsp-version>
<jstl-version>1.2</jstl-version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- Hibernate 4 dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--MYSQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Servlet and JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-version}</version>
<scope>provided</scope>
</dependency>
<!-- JSTL dependency -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Spring4Hiberante4Integration</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
实体类
实体类就用了一个user实体,贴代码:
package com.ssh.model;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by Administrator on 2016/7/28.
*/
@Entity
@Table(name = "users")
public class UserVO implements Serializable{
private static final long serialVersionUID = -7988799579036225137L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@Column
private Integer age;
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;
}
@Override
public String toString() {
return "users{" +
"id=" + id +
", username='" + name + '\'' +
", age=" + age +
'}';
}
}
dao层
dao层的设计符合现在的面向接口配合泛型的开发方式
BaseDao.class:
package com.ssh.dao;
import java.io.Serializable;
import java.util.List;
/**
* Created by admin on 2017/6/10.
*/public interface BaseDao<PK extends Serializable,T> {
T getByKey(PK key);
T getByProperties(String propertiesName,Object properties);
PK sava(T entity);
void update(T entity);
void delete(T entity);
List<T> getAll();
Long getTotalCount();
}
BaseDaoImpl.class:
package com.ssh.dao.impl;
import com.ssh.dao.BaseDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
/**
* Created by admin 2017/6/10.
*/
public class BaseDaoImpl<PK extends Serializable,T> implements BaseDao<PK,T>{
private final Class<T> persistentClass;
protected BaseDaoImpl(){
this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
@Resource
protected SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
}
@Override
public T getByProperties(String propertiesName, Object properties) {
String hql = "From "+persistentClass.getName()+" as modal WHERE modal."+ propertiesName + "= ?";
Query query = getSession().createQuery(hql);
query.setParameter(0,properties);
return (T) query.uniqueResult();
}
@Override
public void update(T entity) {
getSession().update(entity);
}
public PK sava(T entity) {
return (PK) getSession().save(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
@Override
@SuppressWarnings("unchecked")
public List<T> getAll() {
String hql = "From "+persistentClass.getName();
Query query = getSession().createQuery(hql);
return query.list();
}
@Override
public Long getTotalCount() {
String hql = "select count(*) from "+persistentClass.getName();
Query query = getSession().createQuery(hql);
return (Long) query.uniqueResult();
}
}
IUserDao.class:
package com.ssh.dao;
import com.ssh.model.UserVO;
/**
* Created by admin on 2016/7/28.
*/
public interface IUserDAO extends BaseDao<Integer,UserVO>{
}
因为基础的CURD我都在BaseDao里面定义过了。所以这边IUserDao里面就不需要再写额外方法了
UserDaoImpl.class:
package com.ssh.dao.impl;
import com.ssh.dao.IUserDAO;
import com.ssh.model.UserVO;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 2016/7/28.
*/
@Repository
public class UserDAOImpl extends BaseDaoImpl<Integer,UserVO> implements IUserDAO {
}
service层
代码就不贴了,希望大家自己下载琢磨,不过有一点要提的是
先贴上UserServiceImpl的代码:
package com.ssh.service.impl;
import com.ssh.dao.IUserDAO;
import com.ssh.model.UserVO;
import com.ssh.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* Created by Administrator on 2016/7/28.
*/
@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl<Integer,UserVO> implements IUserService {
@Resource
private IUserDAO userDAO;
@Resource
public void setUserDAO(IUserDAO userDAO) {
super.setBaseDao(userDAO);
}
@Override
public UserVO addUser(String name, Integer age) {
UserVO userVO = new UserVO();
userVO.setName(name);
userVO.setAge(age);
Integer id = userDAO.sava(userVO);
return userDAO.getByKey(id);
}
}
这里的userDao是要显示注入的,否则就会报ClassNotFound的错误
controller层
UserController.class:
package com.ssh.controller;
import com.ssh.model.UserVO;
import com.ssh.service.IUserService;
import org.springframework.stereotype.Controller;
import org.jboss.logging.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class UserController {
public UserController() {
System.out.println("UserController()");
}
@Resource
private IUserService userService;
@RequestMapping(value = "/user")
@ResponseBody
public UserVO getUser(Integer id){
return userService.getByKey(id);
}
@RequestMapping(value = "/addUser")
@ResponseBody
public UserVO addUser(String name,Integer age){
return userService.addUser(name,age);
}
}
这是一份很简单的demo,仔细看一下发现还是有点小缺陷的,还得去修改一下。嗯就是这样,如果这个demo能给学习SSH+maven框架的朋友一点小帮助,就可以啦!!!