Spring+SpringMVC+Mybatis整合

一、新建一个Maven工程,目录如下


Spring+SpringMVC+Mybatis整合_第1张图片
1.png

二、pom.xml


  4.0.0
  Forest
  Forest
  0.0.1-SNAPSHOT
  war
  Forest
  
    
        UTF-8
        
        3.2.9.RELEASE
        
        3.2.8
    
    
        
            javax
            javaee-api
            7.0
            provided
        
        
            org.glassfish.web
            javax.servlet.jsp.jstl
            1.2.2
        
        
            javax.servlet.jsp
            jsp-api
            2.1
            provided
         
        
        
            org.springframework
            spring-core
            4.0.0.RELEASE
        
        
            org.springframework
            spring-context
            4.0.0.RELEASE
        
        
            org.springframework
            spring-web
            3.2.9.RELEASE
        
          
            org.springframework  
            spring-webmvc  
            3.2.9.RELEASE  
          

        
            org.springframework
            spring-jdbc
            ${spring.version}
        
            
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
        
            org.mybatis
            mybatis-spring
            1.2.2
        

        
        
            mysql
            mysql-connector-java
            5.1.34
        
        
    
  
  
    
      
        maven-compiler-plugin
        2.3.2
        
          1.7
          1.7
        
      
      
        maven-war-plugin
        2.2
        
          3.1
          false
        
      
    
  

三、web.xml



  Forest
  
    contextConfigLocation
    classpath:mybatis-config.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    default
    *.css
  
  
    default
    *.jpg
  
  
    spring
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc-config.xml
    
    1
  
  
    spring
    /
  
  
    /WEB-INF/views/getArticles.jsp
  

四、jdbc.properties

user=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/forest

五、spring-mybatis.xml



    
    
    
        
    
       
    
    
        

        
        
        
        
        
        
    
    
    
        
        
    
        
    
    
        
    


六、springmvc-config.xml


 
    
 

     
    
        
        
    
 

七、ArticleController.java

package com.z.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.z.service.ArticleService;

@Controller
public class ArticleController {
    @Autowired
    ArticleService service;
    
    @RequestMapping(value = "/getArticles", method = RequestMethod.GET)
    public String getArticles(Model model) {
        model.addAttribute("articles", service.getArticles());
        return "getArticles";
    }
}

八、ArticleService.java

package com.z.service;

import java.util.List;

import com.z.model.Article;

public interface ArticleService {
    public List
getArticles(); }

九、ArticleServiceImpl.java

package com.z.service;

import java.util.List;

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

import com.z.mapper.ArticleMapper;
import com.z.model.Article;

@Service
public class AriticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper dao;
    public List
getArticles() { return dao.getArticles(); } }

十、ArticleMapper.java

package com.z.mapper;


import java.util.List;

import com.z.model.Article;
public interface ArticleMapper {
    public List
getArticles(); }

十一、ArticleMapper.xml




  
  
    
    
    
    
  
  
  
  

十二、Article.java

package com.z.model;

public class Article {
    private String id;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
/*  private String content;
    
    private String getContent() {
        return content;
    }
    
    private void setContent(String content) {
        this.content = content;
    }*/
    
    private String title;
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    private String url;
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
}

十三、header.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



  
    翡翠森林
  
    
  
  
     翡翠森林   
    

    



十四、getArticles.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>



     
     <%@ include file="header.jsp" %>

    
    
  
  
  
     
        

${article.title}

十五、运行结果

Spring+SpringMVC+Mybatis整合_第2张图片
14.png




更多内容请关注微信公众号


Spring+SpringMVC+Mybatis整合_第3张图片
wechat.jpg

你可能感兴趣的:(Spring+SpringMVC+Mybatis整合)