美文网首页java大集合Java学习笔记
Spring+SpringMVC+Mybatis整合

Spring+SpringMVC+Mybatis整合

作者: 海天一树X | 来源:发表于2017-08-13 17:17 被阅读63次

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


    1.png

    二、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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>Forest</groupId>
      <artifactId>Forest</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>Forest</name>
      <description/>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- spring版本号 -->
            <spring.version>3.2.9.RELEASE</spring.version>
            <!-- mybatis版本号 -->
            <mybatis.version>3.2.8</mybatis.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.web</groupId>
                <artifactId>javax.servlet.jsp.jstl</artifactId>
                <version>1.2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency> 
            
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>3.2.9.RELEASE</version>
            </dependency>
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-webmvc</artifactId>  
                <version>3.2.9.RELEASE</version>  
            </dependency>  
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
                
            <!--mybatis依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
    
            <!-- mybatis/spring包 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>
    
            <!-- mysql驱动包 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.34</version>
            </dependency>
            
        </dependencies>
      
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <version>3.1</version>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    三、web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>Forest</display-name>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mybatis-config.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
      </servlet-mapping>
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>/WEB-INF/views/getArticles.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    四、jdbc.properties

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

    五、spring-mybatis.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="  
               http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
               http://www.springframework.org/schema/aop  
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/context  
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <!-- 引入数据库的配置文件,使用配置文件的话,部署时在测试版和正式版之间修改参数(比如数据库的ip或密码)比如较方便 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties" />
        </bean>
           
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- 不使用jdbc.properties来配置 -->
    <!--        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/forest" />
            <property name="username" value="root" />
            <property name="password" value="123456" /> -->
            
            <!-- 使用jdbc.properties来配置 -->
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${url}" />
            <property name="username" value="${user}" />
            <property name="password" value="${password}" />
        </bean>
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 这里的ref要与上段中的bean id保持一致 -->
            <property name="dataSource" ref="dataSource"/>
        </bean>
            
        <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.z.mapper" />
        </bean>
    
    </beans>
    

    六、springmvc-config.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     
        <context:component-scan base-package="com.z" />
     
    <!--     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
         
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>
     
    </beans>
    

    七、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<Article> 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<Article> getArticles() {
            return dao.getArticles();
        }
    }
    

    十、ArticleMapper.java

    package com.z.mapper;
    
    
    import java.util.List;
    
    import com.z.model.Article;
    public interface ArticleMapper {
        public List<Article> getArticles();
    }
    

    十一、ArticleMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.z.mapper.ArticleMapper" >
      
      <resultMap id="ArticleMap" type="com.z.model.Article" >
        <id column="id" property="id" />
        <result column="title" property="title" />
        <result column="content" property="content" />
        <result column="url" property="url" />
      </resultMap>
      
      <select id="getArticles" resultMap="ArticleMap">
        select title,url from article;
      </select>
      
    </mapper>
    

    十二、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"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>翡翠森林</title>
      </head>
        <link rel="stylesheet" href="css/style.css" type="text/css" />
      
      <body>
        <span> 翡翠森林   </span>
        <ul class='nav-list'>
            <li>
                <a href='getArticles'>文章列表</a>
            </li>
            <li>
                <a href='contactUs'>联系方式</a>
            </li>
        </ul>
    
        <br/>
        <br/>
        <hr/>
        <br/>
        
    
      </body>
    </html>
    

    十四、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"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>   
         <%@ include file="header.jsp" %>
    
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        
      </head>
      
      <body>
         <c:forEach items="${articles}" var="article">
            <p>
                <a href="${article.url}">${article.title}</a>
            </p>
         </c:forEach>       
      </body>
    </html>
    

    十五、运行结果

    14.png



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


    wechat.jpg

    相关文章

      网友评论

        本文标题:Spring+SpringMVC+Mybatis整合

        本文链接:https://www.haomeiwen.com/subject/szjerxtx.html