一、准备环境:

maven:apache-maven-3.2.3

jdk:jdk1.8.0_25

tomcat:tomcat-9.0

二、配置Maven、jdk

1、Window——>Preferences——>Maven——>设置自己的Settings

2、Window——>Preferences——>Java——>Installed JREs——>Add

配置好maven之后,就可以在本地设置好的地方看到对应的Maven文件了,还有包含resp文件夹

三、新建Maven项目:

1、右击——>New(或者是File——>New)——>other——>Maven——>Maven Project——>Next

2、这时候,我们就成功创建出一个Maven项目了,项目结构如下图:

3、右击项目,选择Properties进行一些配置:

尽量使用Tomcat9  因为最新版,不解释。

到这里,我们的Maven项目就建好了,接下来,我们来整合搭建SSM(spring MVC + Spring + Mybatis)

首先我们来配置一些整合框架所必须要的jar包,也就是来配置我们的pom文件,然后直接alt+f5,保存后,就会自动下载,存放在之前配置好的本地maven仓库中。

pox.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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ssm</groupId>
    <artifactId>Maven_Project</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Maven_Project Maven Webapp</name>
    <url>http://maven.apache.org</url>    

    <!-- 用来设置版本号 -->
    <properties>
        <srping.version>4.0.2.RELEASE</srping.version>
        <mybatis.version>3.2.8</mybatis.version>
        <slf4j.version>1.7.12</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>
    <!-- 用到的jar包 -->
    <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>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${srping.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>
        <!-- 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 -->
    </dependencies>    

    <build>
      <finalName>Maven_Project</finalName>
    </build>
</project>

  2、在src/main/resources下添加配置文件:applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    

    <!-- 使用注解式注入 -->
    <context:annotation-config />    

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.ssm" />    

    <!-- 导入DAO配置 -->
    <import resource="spring-dao.xml"/>    

    <!-- 导入数据库配置 -->
    <import resource="spring-db.xml"/>    

    <!-- 导入数据库配置 -->
    <import resource="spring-tx.xml"/>    

</beans>

  配置数据库连接池:jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/maven_ssm
username=root
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

  配置日志:log4j.properties

#定义LOG输出级别
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

  配置我们的ioc注入:spring-dao.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。
         可指定多个包,包与包之间用逗号或分号分隔-->
        <property name="basePackage" value="com.ssm.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>                           

</beans>

  spring-db.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>    

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>    

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
    </bean>    

</beans>

  配置spring的事务管理器:spring-tx.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>    

</beans>

  接下来,我们来整合spring mvc:spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    

    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>    

     <!-- 添加注解驱动 -->
    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>    

    <!-- 设置使用注解的类所在的包 -->
    <context:component-scan base-package="com.ssm.controller" />    

    <!-- 完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
            </list>
        </property>
    </bean>    

    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>    

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>     

</beans>

  修改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"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>    

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>    

    <!-- 加载spring bean -->
    <context-param>
         <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>    

    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>    

    <!-- Spring MVC servlet -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>    

</web-app>

  

到这里,我们的ssm框架就完成了,下面,我们来个实现:

在src/main/java下分别添加我们的dao包,service包,还有controller包.包里面添加相应的类,效果图如下:

UserController.java:

package com.ssm.controller;    

import java.util.List;    

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;    

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;    

import com.ssm.dto.User;
import com.ssm.service.IUserService;    

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;    

    @RequestMapping("/userList")
    public String userList(HttpServletRequest request,Model model){
        List<User> uList = userService.getAllUser();
        model.addAttribute("uList", uList);
        return "userList";
    }    

    @RequestMapping("/showUser")
    public String showUser(HttpServletRequest request,Model model){
        int userId = Integer.parseInt(request.getParameter("id"));
        User user = userService.getUserById(userId);
        model.addAttribute("user", user);
        return "showUser";
    }    

    @RequestMapping("/addUserUI")
    public String addUserUI(){
        return "addUser";
    }    

    @RequestMapping("/addUser")
    public String addUser(HttpServletRequest request,Model model){
        User user = new User();
        user.setName(String.valueOf(request.getParameter("name")));
        user.setPassword(String.valueOf(request.getParameter("password")));
        user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));
        userService.addUser(user);
        return "redirect:/user/userList";
    }
}

  IUserDao.java:

package com.ssm.dao;    

import java.util.List;
import java.util.Map;    

import com.ssm.dto.User;    

public interface IUserDao {    

    public User queryByPrimaryKey(Integer id);    

    public List<User> queryUserByBatch(Map<String,Object> params);    

    public void insertUser(User user);    

    public void insertUserByBatch(List<User> list);    

    public void deleteByPrimaryKey(Integer id);    

    public void delteUserByBatch(Map<String,Object> params);    

    public void updateByPrimaryKey(Integer id);    

    public List<User> getAllUser();    

}

  User.java:

package com.ssm.dto;    

public class User {
    private Integer id;    

    private String name;    

    private String password;    

    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 String getPassword() {
        return password;
    }    

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
}

  UserMapper.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.ssm.dao.IUserDao" >
    <resultMap id="BaseResultMap" type="com.ssm.dto.User">
        <result column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
    </resultMap>    

    <sql id="Base_Column_List">
        id, name, password, age
    </sql>    

    <!-- 查询用户-->
    <select id="queryByPrimaryKey" resultMap="BaseResultMap"
        parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List" />
        from user
        where id = #{id,jdbcType=INTEGER}
    </select>    

    <!-- 查询用户-->
    <select id="getAllUser" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from user
    </select>    

    <!-- 批量查询用户-->
    <select id="queryUserByBatch" resultMap="BaseResultMap"
        parameterType="java.util.Map">
        select
        <include refid="Base_Column_List" />
        from user
        where id in
        <foreach collection="idList" item="userId" index="index" open="(" separator="," close=")">
             #{userId,jdbcType=DECIMAL}
        </foreach>
    </select>    

    <!-- 插入用户 -->
    <insert id="insertUser" parameterType="com.ssm.dto.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="name != null" >
            name,
          </if>
          <if test="password != null" >
            password,
          </if>
          <if test="age != null" >
            age,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null" >
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            #{password,jdbcType=VARCHAR},
          </if>
          <if test="age != null" >
            #{age,jdbcType=INTEGER},
          </if>
        </trim>
    </insert>    

    <!-- 批量插入用户 -->
    <insert id="insertUserByBatch" parameterType="java.util.List" >
    insert into USER
       (ID,
        NAME,
        PASSWORD,
        AGE)
    select A.* from
         (
         <foreach collection="list" item="user" index="index" separator="union">
             select
             #{user.id,jdbcType=INTEGER},
             #{user.name,jdbcType=VARCHAR},
             #{user.password,jdbcType=VARCHAR},
             #{user.age,jdbcType=INTEGER}
              from dual
         </foreach>
         ) A
    </insert>    

    <!-- 删除用户 -->
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from user
        where id = #{id,jdbcType=INTEGER}
    </delete>    

    <!-- 批量删除用户 -->
    <delete id="deleteUserByBatch" parameterType="java.util.Map" >
     delete from user
     where id IN
         <foreach item="ids" collection="iList" open="(" separator="," close=")">
             #{ids,jdbcType=DECIMAL}
         </foreach>
    </delete>    

    <!-- 更新用户 -->
    <update id="updateByPrimaryKey" parameterType="com.ssm.dto.User" >
        update user
        <set >
          <if test="name != null" >
            name = #{name,jdbcType=VARCHAR},
          </if>
          <if test="password != null" >
            password = #{password,jdbcType=VARCHAR},
          </if>
          <if test="age != null" >
            age = #{age,jdbcType=INTEGER},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>    

</mapper>

  IUserService.java:

package com.ssm.service;    

import java.util.List;    

import com.ssm.dto.User;    

public interface IUserService {    

    public User getUserById(int userId);    

    public void insertUser(User user);    

    public void addUser(User user);    

    public List<User> getAllUser();
}

  UserServiceImpl.java:

package com.ssm.service.Impl;    

import java.util.List;    

import javax.annotation.Resource;    

import org.springframework.stereotype.Service;    

import com.ssm.dao.IUserDao;
import com.ssm.dto.User;
import com.ssm.service.IUserService;    

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserDao userDao;    

    public User getUserById(int userId) {
        return userDao.queryByPrimaryKey(userId);
    }    

    public void insertUser(User user) {
        userDao.insertUser(user);
    }    

    public void addUser(User user) {
        userDao.insertUser(user);
    }    

    @Override
    public List<User> getAllUser() {
        return userDao.getAllUser();
    }    

}

  接下来就是我们的静态页面了jsp文件了,在WEB-INF文件夹下新建jsp文件夹,添加userinfo.jsp

userinfo.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath=request.getSSScheme()+"://request.getServerName()"+":"+request.getServerPort+Path+
  "/"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    

    <title>My JSP 'userinfo.jsp' starting page</title>    

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->    

  </head>    

  <body>
    <form id="addUser" action="user/addUser" method="post">
        userName: <input id="name" name="name" /><br/>
        password: <input id="password" name="password" /><br/>
        <input type="submit" value="添加新用户"/>
    </form>
  </body>
</html>

  

哦,忘记配置tomcat,配置一下tomcat,然后部署发布项目,进行测试

到此,框架成。

另外,网上只找到了一个可以成功搭建的,这个测试成功,其他的好多半成品,要不就是老版本,无比的坑。

稍后更新会把源代码发上来。

Eclipse使用Maven创建Web项目+整合SSM框架的更多相关文章

  1. 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架

    自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...

  2. eclipse 用maven创建web项目

    Eclipse 用maven构建web项目 (2013-01-27 11:05:31) 转载▼ 标签: it eclipse maven spring web 杂谈   一.背景介绍 对于初学者,用m ...

  3. eclipse中用maven创建web项目

    上一节中完成了本地的maven环境搭建,在eclipse中怎么创建一个maven项目呢 一.eclipse中配置maven环境 eclipse版本4.3 1.打开菜单Help->Eclipse ...

  4. eclipse中Maven创建WEB项目

    刚刚学到Maven的时候总是容易忽视到一些创建Maven项目是的步骤, 这里记录笔者熟悉一种,直接创建Maven Project 下面开始吧--- 选择web-app,没得说,然后那些groupID ...

  5. Eclipse使用Maven创建Web项目

    一.Maven插件下载.jdk下载 1.maven下载地址: http://maven.apache.org/download.cgi 2.jdk下载地址: http://www.oracle.com ...

  6. JAVA Eclipse使用Maven构建web项目详解(SSM框架)

    tips: 启动项目后,welcome-file的链接即为测试用例 部署maven web项目 Eclipse使用Maven构建web项目详解 pom.xml添加webapp依赖: <depen ...

  7. (转)Maven学习总结(七)——eclipse中使用Maven创建Web项目

    孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(七)——eclipse中使用Maven创建Web项目 一.创建Web项目 1.1 选择建立Maven Project 选择File -&g ...

  8. SSM 框架-04-使用maven创建web项目

    SSM 框架-04-使用maven创建web项目 本篇介绍使用MAVEN来管理jar包,就不用一个一个去添加和下载jar包了,直接在maven配置文件中配置就可以了,maven可以帮助我们自动下载.本 ...

  9. 简述泛型、用Maven创建Web项目以及在Web项目上整合SpringMVC

    表设计 Timestamp列是否取消"根据当前时间戳自动更新" 是否null及默认值选择合理不合理 外键命名规范及更新和删除时的动作是否合理   泛型 类型参数 --允许在外部指定 ...

随机推荐

  1. 查看struct或class的内存布局

    适用于VC编译器(Visual Studio) 附加选项: /d1 reportSingleClassLayout[foo] 例如CItem(注意后面没有空格) /d1 reportSingleCla ...

  2. Docker 安装部署

    Docker学习笔记 一.Ubuntu Docker 安装 (1).获取最新版本Docker安装包 lyn@lyn:/data/docker$ sudo wget -qO- https://get.d ...

  3. svn的差异查看器和合并工具换成BCompare.exe

    svn的差异查看器和合并工具换成BCompare.exe

  4. 第二章 DateTime工具类

    项目中经常需要将DateTime转化成各种格式的String类型,或将各种类型的String转为DateTime类型. 本文提供一个DateTime与String的转换工具类: import org. ...

  5. 第一章 初识MVC4

    1.MVC模式 Mvc将应用程序分离为三个部分: Model:是一组类,用来描述被处理的数据,同时也定义这些数据如何被变更和操作的业务规则.与数据访问层非常类似. View:是一种动态生成HTML的模 ...

  6. Master-Worker模式

    并行程序设计模式--Master-Worker模式 简介 Master-Worker模式是常用的并行设计模式.它的核心思想是,系统有两个进程协议工作:Master进程和Worker进程.Master进 ...

  7. 搭建高性能计算环境(八)、应用软件的安装之gromacs

    1,下载安装新版本的gcc(高版本的gcc只是推荐使用的,系统自带的gcc4.4照样能正常编译.运行) wget http://ftp.tsukuba.wide.ad.jp/software/gcc/ ...

  8. 6.24 AppCan移动开发者大会:议程重大更新,报名即将关闭

    大会倒计时2天,议程重大更新,报名通道即将关闭! 创业6年,由AppCan主办的第一届移动开发者大会将在本周五盛大召开.超过100万开发者线上参与.现场1500人规模.50家移动互联企业深度参与.30 ...

  9. android ViewFlipper的使用

    有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果.该类有如下几个和动画 ...

  10. spring集成mongodb封装的简单的CRUD

    1.什么是mongodb         MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB是一个介 ...