Spring整合springMVC mybatis springJPA

1、导入依赖

jpa主要依赖:


		
            org.springframework.data
            spring-data-jpa
            1.11.3.RELEASE
        
        
            org.hibernate
            hibernate-entitymanager
            4.3.8.Final
        

全依赖:


  4.0.0
  tour
  tour
  0.0.1-SNAPSHOT
  war
  
  
    
          
        4.3.8.RELEASE  
          
        3.2.6  
          
        1.7.7  
        1.2.17  
        UTF-8
    
    
  
  	
  	  
        junit  
        junit  
        4.11  
          
        test  
    
  
    
      org.apache.openejb
      javaee-api
      5.0-1
      provided
    
    
      javax.servlet
      jstl
      1.2
      provided
    
    
      javax.servlet.jsp
      jsp-api
      2.1
      provided
    
    
    
      
         org.springframework  
         spring-core  
         ${spring.version}  
     
       
          org.springframework  
          spring-web  
          ${spring.version}  
       
        
          org.springframework  
          spring-oxm  
          ${spring.version}  
        
        
          org.springframework  
          spring-tx  
          ${spring.version}  
        

        
          org.springframework  
          spring-jdbc  
          ${spring.version}  
      
        
            org.springframework  
            spring-webmvc  
            ${spring.version}  
          
          
            org.springframework  
            spring-aop  
            ${spring.version}  
          
  
          
            org.springframework  
            spring-context-support  
            ${spring.version}  
          
  
          
            org.springframework  
            spring-test  
            ${spring.version}  
        
        
          
          
            org.mybatis  
            mybatis  
            ${mybatis.version}  
        
        
          
          
            org.mybatis  
            mybatis-spring  
            1.2.2  
         
        
          
          
            javax  
            javaee-api  
            7.0  
        
        
          
          
            mysql  
            mysql-connector-java  
            5.1.30  
          
          
          
            commons-dbcp  
            commons-dbcp  
            1.2.2  
          
          
          
          
            log4j  
            log4j  
            ${log4j.version}  
          
          
          
          
          
            com.alibaba  
            fastjson  
            1.1.41  
          
  
  
          
            org.slf4j  
            slf4j-api  
            ${slf4j.version}  
          
  
          
            org.slf4j  
            slf4j-log4j12  
            ${slf4j.version}  
          
        
          
          
            org.codehaus.jackson  
            jackson-mapper-asl  
            1.9.13  
          
          
          
            commons-fileupload  
            commons-fileupload  
            1.3.1  
          
          
            commons-io  
            commons-io  
            2.4  
          
          
            commons-codec  
            commons-codec  
            1.9  
        
        
        
        
            org.apache.commons
            commons-lang3
            3.1
        
        
            commons-lang
            commons-lang
            2.4
        
        
        
        
        
            org.apache.shiro
            shiro-core
            1.2.2
        
        
            org.apache.shiro
            shiro-web
            1.2.2
        
        
		    org.apache.shiro
		    shiro-spring
		    1.2.2
		
        
            org.apache.shiro
            shiro-ehcache
            1.2.2
        
        
        
        
		    com.fasterxml.jackson.core
		    jackson-annotations
		    2.8.5
		
		  
            com.fasterxml.jackson.core  
            jackson-core  
            2.8.5  
          
          
            com.fasterxml.jackson.core  
            jackson-databind  
            2.8.5  
          
        
        
        
			com.github.pagehelper
			pagehelper
			4.1.4
		
		
		
		
		
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
        
            cglib
            cglib
            3.2.4
        
		
		
		
            org.springframework.data
            spring-data-jpa
            1.11.3.RELEASE
        
        
            org.hibernate
            hibernate-entitymanager
            4.3.8.Final
        
		
  
  
  	
        
            src/main/java
            
                **/*.xml
            
            false
        
        
            src/main/resources
            
                *.xml
                *.properties
            
            false
        
    
    
      
        maven-war-plugin
      
      
        maven-compiler-plugin
        
          1.7
          1.7
        
      
    
  

注意:如果加载不到class,主要要重新部署项目,

一般步骤为:maven-clean ->maven-install ->tomcat中部署项目删除再添加

若出现加载不出jap xml,则可能是导入的jpa版本和你的本身的spring版本不一致


2、JPA配置文件(spring-jpa.xml)





	
	
	
	
	
	
	
	
	
	
	
	

	
        
        
        
        
            
                com.zemel.pojo
            
        
        
        
            
                
                
                
                
                
                
                
                
            
        
        
            
                org.hibernate.cfg.ImprovedNamingStrategy
                
                update
                true
            
        
    

    

    
    

    
    
        
    

    
    

3、mybatis配置文件

  
  
      
    
    
    
      
      
          
      
  
      
          
          
          
          
          
          
          
          
          
          
          
          
          
          
      
  
      
      
          
          
          
        
		
		   
		     
		       
		         
		           dialect=mysql
		           reasonable=true
		           pageSizeZero=true
		         
		       
		     
		   
		
      
  
      
      
          
          
      
  
      
      
          
    
    
    
	
	

4、jdbc.properties配置

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=utf-8
username=root
password=123456
#定义初始连接数 
initialSize=0
#定义最大连接数  
maxActive=20
#定义最大空闲  
maxIdle=20
#定义最小空闲  
minIdle=1
#定义最长等待时间  
maxWait=60000

5、Spring-mvc配置文件

  
 
      
    
      
      
          
              
                text/html;charset=UTF-8  
              
          
      
      
      
          
              
                   
              
          
      
    
    
        
            
                
                    
                        application/json;charset=UTF-8
                    
                
            
        
    
    
      
      
          
          
          
      
      
      
        
          
            
          
            
          
            
       
    
	

6、log4j.properties配置

log4j.rootLogger=INFO,Console,File  
#定义日志输出目的地为控制台  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#可以灵活地指定日志输出格式,下面一行是指定具体的格式  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
  
#文件大小到达指定尺寸的时候产生一个新的文件  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#指定输出目录  
log4j.appender.File.File = logs/ssm.log  
#定义文件最大大小  
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

7.web.xml配置



  	
  
    
      
        contextConfigLocation  
        classpath:spring-mybatis.xml,
        			classpath:spring-shiro.xml,
        			classpath:spring-jpa.xml
          
      
      
      
        encodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            UTF-8  
          
      
      
        encodingFilter  
        /*  
    

	
	  
-->
org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 SpringMVC *.ht /pages/index.jsp

8、实体类

package com.zemel.pojo;

import java.util.Date;

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="article")
public class Article {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer id;

	@Column
    private String title;
	
	@Column
    private Integer type;

	@Column
    private Integer pubYear;

	@Column
    private String pubTime;

	@Column
    private Date createTime;

	@Column
    private String content;
    
	@Column
    private String coverImg;
    
	@Column
    private Date updateTime;
	
    // 查询用
    private Integer dtype;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getPubYear() {
        return pubYear;
    }

    public void setPubYear(Integer pubYear) {
        this.pubYear = pubYear;
    }

    public String getPubTime() {
        return pubTime;
    }

    public void setPubTime(String pubTime) {
        this.pubTime = pubTime == null ? null : pubTime.trim();
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }

	public String getCoverImg() {
		return coverImg;
	}

	public void setCoverImg(String coverImg) {
		this.coverImg = coverImg;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	public Integer getDtype() {
		return dtype;
	}

	public void setDtype(Integer dtype) {
		this.dtype = dtype;
	}

	@Override
	public String toString() {
		return this.getId()+","+this.getType();
	}
}

9、Jpa接口

package com.zemel.dao.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

import com.zemel.pojo.Article;

public interface ArticleRepository extends JpaRepository {
	
	
	
}

10、Jpa接口使用

package com.zemel.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zemel.dao.ArticleMapper;
import com.zemel.dao.jpa.ArticleRepository;
import com.zemel.pojo.Article;
import com.zemel.service.IArticleService;
import com.zemel.utils.DateUtil;

@Service
public class ArticleServiceImpl implements IArticleService {
	
	@Autowired
	private ArticleMapper articleDao;
	
	@Autowired
	private ArticleRepository articleRepository;

	public boolean add(Article article) {
		
		Date now = new Date();
		article.setCreateTime(now);
		article.setUpdateTime(now);
		// 日期截取
		String pubYear = DateUtil.formatDate(now, "yyyy");
		String pubTime = DateUtil.formatDate(now, "MM-dd HH:mm");
		article.setPubYear(Integer.parseInt(pubYear));
		article.setPubTime(pubTime);
		article = articleRepository.save(article);
		if(article.getId() > 0){
			return true;
		}
		return false;
	}

	

}

注意:如果启动出错,注意看错误。

        如有解决不了的错误,可以评论留言!




你可能感兴趣的:(SpringMVC,SpringBoot,Java,JPA)