spring+springmvc+mybatis框架搭建

Posted liandy001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring+springmvc+mybatis框架搭建相关的知识,希望对你有一定的参考价值。

一、开发前准备

  1)ecplise4.11.0 百度网盘:https://pan.baidu.com/s/1wO9_I52lp0mYNeNTdnj80w 提取码:booa

  2)jdk1.6.0_45  百度网盘:https://pan.baidu.com/s/1geF4nKYighL-COOBgUZouw  提取码:kv1b  

  3)apache-maven-3.2.5 百度网盘:https://pan.baidu.com/s/122Y_Sstgd-NXx9ljfQ-UDw 提取码:veei

  4)mysql 百度网盘:https://pan.baidu.com/s/1PUm1wl8wohBpF1uz8skrVg 提取码:cte4

  5)navicat 百度网盘:https://pan.baidu.com/s/1TX4BL7Mjy1oBQFMfBg1gqQ 提取码:3r4m

  6)windows64位

  

二、SSM项目结构

技术图片

 

 三、搭建SSM框架

  1.Create a Dynamic Web project

  技术图片

  技术图片

 

  2.Convert To Maven Project

  技术图片

  3.依赖maven管理jar包  

技术图片
<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>Ssm</groupId>
  <artifactId>Ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <properties>
      <spring.version>4.0.2.RELEASE</spring.version>
    <mybatis.version>3.2.8</mybatis.version>
    <slf4j.version>1.7.12</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
  </properties>
  <dependencies>      
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
            <scope>test</scope>
        </dependency>
        <!-- java ee包 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <!-- spring框架包 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</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-context-support</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</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-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-aspects</artifactId>
            <version>$spring.version</version>
        </dependency>
        <!-- spring框架包 end -->
        <!-- mybatis框架包 start -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>$mybatis.version</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- mybatis框架包 end -->
        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- jstl标签类 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <!-- log END -->
        <!-- Json -->
        <!-- 格式化对象,方便输出日志 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!-- 上传组件包 start -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- 上传组件包 end -->
 
        <!-- AL相关添加 -->
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
  </dependencies>
</project>
pom.xml

   4.model层创建域模型  

技术图片
/**  
 * @Title:  User.java   
 * @Package cn.com.kamfu.model   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: liandy    
 * @date:   2019年7月12日 上午2:47:34   
 * @version V1.0 
 */
package cn.com.kamfu.model;

/**   
 * @ClassName:  User   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: liandy 
 * @date:   2019年7月12日 上午2:47:34   
 *     
 */
public class User 
    private String username;
    private int age;
    public String getUsername() 
        return username;
    
    public void setUsername(String username) 
        this.username = username;
    
    public int getAge() 
        return age;
    
    public void setAge(int age) 
        this.age = age;
    
    
User

  5.service层创建服务  

技术图片
/**  
 * @Title:  UserService.java   
 * @Package cn.com.kamfu.service   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: liandy    
 * @date:   2019年7月12日 上午2:51:49   
 * @version V1.0 
 */
package cn.com.kamfu.service;

import java.util.List;

import cn.com.kamfu.model.User;

/**   
 * @ClassName:  UserService   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: liandy 
 * @date:   2019年7月12日 上午2:51:49   
 *     
 */

public interface UserService 
    List<User> findAllUser();
UserService
技术图片
/**  
 * @Title:  UserServiceImpl.java   
 * @Package cn.com.kamfu.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: liandy    
 * @date:   2019年7月12日 上午2:52:31   
 * @version V1.0 
 */
package cn.com.kamfu.service.impl;

/**   
 * @ClassName:  UserServiceImpl   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: liandy 
 * @date:   2019年7月12日 上午2:52:31   
 *     
 */

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.kamfu.mapper.UserMapper;
import cn.com.kamfu.model.User;
import cn.com.kamfu.service.UserService;


@Service
@Transactional
public class UserServiceImpl implements UserService
    
    @Resource
    public UserMapper userMapper;
    
    @Override
    public List<User> findAllUser() 
        // TODO Auto-generated method stub
        return userMapper.findAllUser();
    
UserServiceImpl

  6.controller层创建控制器

技术图片
/**  
 * @Title:  UserController.java   
 * @Package cn.com.kamfu.controller   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: liandy    
 * @date:   2019年7月12日 上午2:53:59   
 * @version V1.0 
 */
package cn.com.kamfu.controller;

import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.com.kamfu.model.User;
import cn.com.kamfu.service.UserService;



@Controller
@RequestMapping("/user")
public class UserController 
    
    @Autowired
    private UserService userService;
    protected Logger log = Logger.getLogger(UserController.class);
    
    //match automatically
    @RequestMapping("/findAllUser")
    public String findAllUser(HttpServletRequest request)
        List<User> listUser = userService.findAllUser();
        request.setAttribute("listUser",listUser);
        log.debug("服务器启动了,log4j开始工作了");
        log.error("服务器启动了,log4j开始工作了");
        return "/allUser";
    
    
    //boxing automatically
    @RequestMapping("/toUser")
    public String toUser(User user)
        System.out.println(user.toString());
        return user.toString();
    
    
    //pass the parameters to front-end
    @RequestMapping("/showUser")
    public String showUser(Map<String,Object> map)
        User p =new User();
        map.put("p", p);
        p.setAge(20);
        p.setUsername("jack");
        return "show";
    
    
    //pass the parameters to front-end using ajax
    @RequestMapping("/getUser")
    public void getPerson(String name,PrintWriter pw)
        pw.write("hello,"+name);        
    

    
    //redirect 
    @RequestMapping("/redirect")
    public String redirect()
        return "redirect:hello";
    
    
    
    @RequestMapping("/file")
    public String file()
        return "/file";
    
    //文件上传
    @RequestMapping(value="/upload",method=RequestMethod.POST)
    public String upload(HttpServletRequest req) throws Exception
        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
        MultipartFile file = mreq.getFile("file");
        String fileName = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");        
        FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
                "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf(‘.‘)));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
        
        return "success";
    
UserController

  7.mapper层依赖mybatis创建接口及配置文件

技术图片
/**  
 * @Title:  UserMapper.java   
 * @Package cn.com.kamfu.model   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: liandy    
 * @date:   2019年7月12日 上午2:48:14   
 * @version V1.0 
 */
package cn.com.kamfu.mapper;

/**   
 * @ClassName:  UserMapper   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: liandy 
 * @date:   2019年7月12日 上午2:48:14   
 *     
 */

import java.util.List;

import cn.com.kamfu.model.User;

public interface UserMapper 
    List<User> findAllUser();
UserMapper
技术图片
<?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="cn.com.kamfu.mapper.UserMapper">
    <!-- 解决表名与字段不匹配 -->
    <!-- type 类的全限定名, 或者一个类型别名
         id 当前命名空间中的一个唯一标识,用于标识一个result map
         result 注入到字段或 JavaBean 属性的普通结果
     -->
    <resultMap type="User" id="userResultMap">
        <!--  <id property="id" column="user_id"/>-->
        <!-- property 映射到列结果的字段或属性。
             column 从数据库中得到的列名,或者是列名的重命名标签。
         -->
        <result property="username" column="username"/>
        <result property="age" column="age"/>
    </resultMap>
    
    <!-- id 在命名空间中唯一的标识符,可以被用来引用这条语句
         resultMap 外部 resultMap 的命名引用
         resultType 从这条语句中返回的期望类型的类的完全限定名或别名。
     -->
    <select id="findAllUser" resultMap="userResultMap">
        select username,age from user
    </select>
</mapper>
userMapper.xml

  8.config层进行相关配置  

技术图片
#jdbc config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssm?autoReconnect=true&useUnicode=true&characterEncoding=utf8
jdbc.username = root
jdbc.password = 
jdbc.properties
技术图片
### \\u8BBE\\u7F6E###
log4j.rootLogger = debug,stdout,D,E
 
### \\u8F93\\u51FA\\u4FE1\\u606F\\u5230\\u63A7\\u5236\\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %dyyyy-MM-dd HH:mm:ss,SSS method:%l%n%m%n
 
### \\u8F93\\u51FADEBUG \\u7EA7\\u522B\\u4EE5\\u4E0A\\u7684\\u65E5\\u5FD7\\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-dyyyy-MM-dd HH:mm:ss  [ %t:%r ] - [ %p ]  %m%n
 
### \\u8F93\\u51FAERROR \\u7EA7\\u522B\\u4EE5\\u4E0A\\u7684\\u65E5\\u5FD7\\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-dyyyy-MM-dd HH:mm:ss  [ %t:%r ] - [ %p ]  %m%n
--------------------- 
\\u4F5C\\u8005\\uFF1A\\u300A\\u5C0F\\u5C0F\\u706B\\u8F66\\u300B 
\\u6765\\u6E90\\uFF1ACSDN 
\\u539F\\u6587\\uFF1Ahttps://blog.csdn.net/qq_29166327/article/details/80467593 
\\u7248\\u6743\\u58F0\\u660E\\uFF1A\\u672C\\u6587\\u4E3A\\u535A\\u4E3B\\u539F\\u521B\\u6587\\u7AE0\\uFF0C\\u8F6C\\u8F7D\\u8BF7\\u9644\\u4E0A\\u535A\\u6587\\u94FE\\u63A5\\uFF01
log4j.properties
技术图片
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- typeAliases 类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。 -->
    <typeAliases>
        <typeAlias alias="User" type="cn.com.kamfu.model.User"/>
    </typeAliases>
    <!-- mappers 告诉 MyBatis 到哪里去找映射文件 -->
    <mappers>
        <mapper resource="cn/com/kamfu/mapper/userMapper.xml" />
    </mappers>
</configuration>
mybatis-config.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  
  <!-- 引入配置文件 -->
  <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:config/jdbc.properties"/>
  </bean>
  
    <!-- 1. 数据源 : DriverManagerDataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="$jdbc.driver" />
        <property name="url" value="$jdbc.url" />
        <property name="username" value="$jdbc.username" />
        <property name="password" value="$jdbc.password" /> 
    </bean>  
    
    <!--2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:
        引用数据源MyBatis定义数据源,同意加载配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:config/mybatis-config.xml" /> 
    </bean>

    <!--3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
        basePackage:指定sql映射文件/接口所在的包(自动扫描)    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.com.kamfu.mapper"></property>
        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->  <!-- 会引起无法加载配置文件 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!--4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源    -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 

    <!-- 5. 使用声明式事务: transaction-manager:引用上面定义的事务管理器 -->
    <tx:annotation-driven transaction-manager="txManager" />

</beans>
spring-common.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <!-- 开启AOP -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!-- 注解扫描包 -->
    <context:component-scan base-package="cn.com.kamfu" />

    <!-- 开启注解 -->
    <mvc:annotation-driven />
    
    <!--  配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理 -->
    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/html/**" location="/html/" />
    
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp" />
        <property name="suffix" value=".jsp" />    
    </bean>
    
</beans>
spring-mvc.xml

  9.web.xml配置  

技术图片
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     
     <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <!-- 设置Spring容器加载所有的配置文件的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/spring-*.xml</param-value>
    </context-param>

    <context-param>  
        <param-name>webAppRootKey</param-name>    
        <param-value>webapp.root</param-value>    
    </context-param>  
    <!-- 设置log4j配置文件的路径 -->
      <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <!--  <param-value>classpath*:config/log4j.properties</param-value>  -->
        <param-value>classpath:config/log4j.properties</param-value>  
        <!--  <param-value>WEB-INF/log4j.properties</param-value> -->
    </context-param>  
    <!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 -->  
    <context-param>    
        <param-name>log4jRefreshInterval</param-name>    
        <param-value>3000</param-value>    
    </context-param>   
    <listener>  
           <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
 
    <!-- 配置SpringMVC核心控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:config/spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载 -->  
        <load-on-startup>1</load-on-startup>
     </servlet>
  
    <!--为DispatcherServlet建立映射 -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!-- 此处可以可以配置成*.do -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    
   <!-- 解决工程编码过滤器 -->
   <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
     
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
web.xml

  10.mysql中创建域模型表  

技术图片
/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50640
Source Host           : localhost:3306
Source Database       : ssm

Target Server Type    : MYSQL
Target Server Version : 50640
File Encoding         : 65001

Date: 2019-07-12 15:18:45
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `username` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (‘a‘, ‘1‘);
user.sql

 

四、项目源码下载

  百度网盘:https://pan.baidu.com/s/13A1AOgG-efh8_ARptZ1haQ

  提取码:g3lw

  

以上是关于spring+springmvc+mybatis框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

Spring+SpringMVC+Mybatis+Mysql整合实例

Spring+SpringMVC+Mybatis+Mysql整合实例

Spring+SpringMVC+Mybatis+Mysql整合实例

SpringMVC——Spring整合Mybatis和SpringMVC

SpringMVC学习--springmvc和mybatis整合

Spring+SpringMVC+MybatisSpring+SpringMVC+Mybatis实现前端到后台完整项目