spring+springMVC+hibernate+maven简单demo

前言

最近因为个人需求,需要一份SSH+maven的简单模版,然后开始各种百度谷歌github找,发现找到的很多的网上的demo要么使用的版本太低(spring3+hibernate3之类),要么就是太复杂不太适合我的需求(仅仅想要一个CURD的项目就足够)
于是最后还是决定自己写一个。

github:https://github.com/GoldButcher/SSH

项目路径:

spring+springMVC+hibernate+maven简单demo_第1张图片
image.png

既然是maven工程,先贴pom.xml文件 比较长希望大家略过,复制粘贴用就好了。


  4.0.0
  com.ssh
  springmvc
  war
  1.0-SNAPSHOT
  springmvc Maven Webapp
  
    4.1.5.RELEASE
    4.3.8.Final
    5.1.10
    4.11
    3.1.0
    2.1
    1.2
    1.8
  

  
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.4.4
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.4.4
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.4.4
    

    
    
      org.springframework
      spring-core
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
      org.springframework
      spring-orm
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
      test
    

    
    
      org.hibernate
      hibernate-core
      ${hibernate.version}
    

    
      org.hibernate
      hibernate-c3p0
      ${hibernate.version}
    

    
    
      mysql
      mysql-connector-java
      ${mysql.version}
    

    
    
      javax.servlet
      javax.servlet-api
      ${servlet-api-version}
    

    
      javax.servlet.jsp
      jsp-api
      ${jsp-version}
      provided
    

    
    
      jstl
      jstl
      ${jstl-version}
    

    
    
      junit
      junit
      ${junit-version}
      test
    

  

  
    Spring4Hiberante4Integration
    
      
        maven-compiler-plugin
        2.3.2
        
          ${java.version}
          ${java.version}
        
      
    
  


实体类

实体类就用了一个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 {
    T getByKey(PK key);
    T getByProperties(String propertiesName,Object properties);
    PK sava(T entity);
    void update(T entity);
    void delete(T entity);
    List 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 implements BaseDao{

    private final Class persistentClass;

    protected BaseDaoImpl(){
        this.persistentClass = (Class) ((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 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{

}

因为基础的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 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 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框架的朋友一点小帮助,就可以啦!!!

你可能感兴趣的:(spring+springMVC+hibernate+maven简单demo)