如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目

最近想自己搭建一下ssm项目,百度了一下,发现网上的好乱,很多都不知道是什么?自己边百度边学习,自己搭建了一下,当然科学在于可重复,我再次搭一下,写了博客做笔记
项目是基于spring4.3和mybatis3.2实现的,jdk用是1.8,不过可以改成自己版本

新建maven项目

1.file->new->other,然后搜索maven
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第1张图片
2.
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第2张图片
3.
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第3张图片
4.
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第4张图片
5.完成
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第5张图片

完成maven项目

1.右键点击项目进去properties里移除
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第6张图片
2.然后新建资源文件
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第7张图片
3.新建两个文件,一个是src/main/java,一个是src/main/test
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第8张图片
4.新建完成效果
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第9张图片
5.进去properties配置输入class位置
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第10张图片
6.选择如下
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第11张图片

配置pom文件

<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.0modelVersion>
  <groupId>com.sisegroupId>
  <artifactId>SSMTestartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>SSMTest Maven Webappname>  

    
  <properties>  
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
    <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>  

    
        <proxool.version>0.9.1proxool.version>
        <commons-pool.version>1.6commons-pool.version>
        <commons-dbcp.version>1.4commons-dbcp.version>
        <druid.version>0.2.9druid.version>

        <tomcat.version>2.2tomcat.version>
        <webserver.port>8084webserver.port>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

        
        <aip.version>4.4.1 aip.version>
  properties>  

    
  <dependencies>  

      
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-coreartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-aopartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-ormartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-webartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-webmvcartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-txartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-testartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-jdbcartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-mockartifactId>  
      <version>2.0.8version>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-contextartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-expressionartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  
    <dependency>  
      <groupId>org.springframeworkgroupId>  
      <artifactId>spring-context-supportartifactId>  
      <version>4.3.5.RELEASEversion>  
    dependency>  

      
    <dependency>  
      <groupId>org.mybatisgroupId>  
      <artifactId>mybatis-springartifactId>  
      <version>1.3.1version>  
    dependency>  

      
    <dependency>  
      <groupId>org.mybatisgroupId>  
      <artifactId>mybatisartifactId>  
      <version>3.2.8version>  
    dependency>  
      
    <dependency>  
      <groupId>com.github.pagehelpergroupId>  
      <artifactId>pagehelperartifactId>  
      <version>3.7.3version>  
    dependency>  
    <dependency>  
      <groupId>com.github.jsqlparsergroupId>  
      <artifactId>jsqlparserartifactId>  
      <version>0.9.1version>  
    dependency>  

      
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency> 
    <dependency>  
      <groupId>javax.servletgroupId>  
      <artifactId>jstlartifactId>  
      <version>1.2version>  
    dependency>  
    <dependency>  
      <groupId>taglibsgroupId>  
      <artifactId>standardartifactId>  
      <version>1.1.2version>  
    dependency>  
    <dependency>  
      <groupId>org.aspectjgroupId>  
      <artifactId>aspectjrtartifactId>  
      <version>1.8.10version>  
    dependency>  
    <dependency>  
      <groupId>org.aspectjgroupId>  
      <artifactId>aspectjweaverartifactId>  
      <version>1.8.10version>  
    dependency>  
    <dependency>  
      <groupId>commons-langgroupId>  
      <artifactId>commons-langartifactId>  
      <version>2.6version>  
    dependency>  
    <dependency>  
      <groupId>dom4jgroupId>  
      <artifactId>dom4jartifactId>  
      <version>1.6.1version>  
    dependency>  

      
    <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.20version>
    dependency>  


      
    
   
        <dependency>
            <groupId>com.cloudhopper.proxoolgroupId>
            <artifactId>proxoolartifactId>
            <version>${proxool.version}version>
            <exclusions>
                <exclusion>
                    <artifactId>geronimo-spec-javamailartifactId>
                    <groupId>geronimo-specgroupId>
                exclusion>
                <exclusion>
                    <artifactId>avalon-framework-apiartifactId>
                    <groupId>avalon-frameworkgroupId>
                exclusion>
                <exclusion>
                    <artifactId>proxool-cglibartifactId>
                    <groupId>com.cloudhopper.proxoolgroupId>
                exclusion>
            exclusions>
        dependency>
        
        
        <dependency>
            <groupId>commons-poolgroupId>
            <artifactId>commons-poolartifactId>
            <version>${commons-pool.version}version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>${commons-dbcp.version}version>
            <scope>compilescope>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>${druid.version}version>
            <scope>runtimescope>
        dependency>
      
    <dependency>  
      <groupId>com.alibabagroupId>  
      <artifactId>fastjsonartifactId>  
      <version>1.2.23version>  
    dependency>  
    <dependency>  
      <groupId>xml-apisgroupId>  
      <artifactId>xml-apisartifactId>  
      <version>2.0.2version>  
    dependency>  
    <dependency>  
      <groupId>commons-netgroupId>  
      <artifactId>commons-netartifactId>  
      <version>3.5version>  
    dependency>  

      
    <dependency>  
      <groupId>org.slf4jgroupId>  
      <artifactId>slf4j-apiartifactId>  
      <version>1.7.22version>  
    dependency>  
    <dependency>  
      <groupId>org.slf4jgroupId>  
      <artifactId>slf4j-log4j12artifactId>  
      <version>1.7.22version>  
    dependency>  
    <dependency>  
      <groupId>log4jgroupId>  
      <artifactId>log4jartifactId>  
      <version>1.2.17version>  
    dependency>  

      
    <dependency>  
      <groupId>junitgroupId>  
      <artifactId>junitartifactId>  
      <version>4.12version>  
      <scope>testscope>  
    dependency>  
    
    <dependency>
    <groupId>com.baidu.aipgroupId>
    <artifactId>java-sdkartifactId>
    <version>${aip.version}version>
dependency>


  dependencies>  

    
  <build>  
    <plugins>  
      <plugin>  
        <groupId>org.apache.maven.pluginsgroupId>  
        <artifactId>maven-war-pluginartifactId>  
        <version>2.4version>  
        <configuration>  
          <warName>SSMwarName>  
            
          <webResources>  
            <resource>  
              <directory>src/main/resources/directory>  
              <targetPath>WEB-INF/classes/targetPath>  
              <filtering>truefiltering>  
            resource>  
            <resource>  
              <directory>src/main/javadirectory>  
              <includes>  
                <include>**/*.xmlinclude>  
              includes>  
              <targetPath>WEB-INF/classestargetPath>  
            resource>  
          webResources>  
        configuration>  
      plugin>  
  <plugin>  
      <groupId>org.mybatis.generatorgroupId>  
      <artifactId>mybatis-generator-maven-pluginartifactId>  
      <version>1.3.2version>  
      <configuration>  
        <verbose>trueverbose>  
        <overwrite>trueoverwrite>  
      configuration>  
    plugin>
      <plugin>  
        <groupId>org.apache.maven.pluginsgroupId>  
        <artifactId>maven-resources-pluginartifactId>  
        <version>2.4.3version>  
        <configuration>  
          <encoding>UTF-8encoding>  
        configuration>  
      plugin>  
      <plugin>  
        <groupId>org.apache.maven.pluginsgroupId>  
        <artifactId>maven-compiler-pluginartifactId>  
      plugin>  
      <plugin>  
        <groupId>org.apache.felixgroupId>  
        <artifactId>maven-bundle-pluginartifactId>  
        <inherited>trueinherited>  
        <extensions>trueextensions>  
      plugin>  
      <plugin>  
        <groupId>org.apache.maven.pluginsgroupId>  
        <artifactId>maven-surefire-pluginartifactId>  
        <configuration>  
          <testFailureIgnore>truetestFailureIgnore>  
        configuration>  
      plugin>  
      <plugin>  
        <groupId>org.apache.maven.pluginsgroupId>  
        <artifactId>maven-site-pluginartifactId>  
          
        <version>3.3version>  
        <configuration>  
          <locales>zh_CNlocales>  
        configuration>  
      plugin>  
   
        <plugin>
            <groupId>org.apache.tomcat.mavengroupId>
            <artifactId>tomcat7-maven-pluginartifactId>
            <version>${tomcat.version}version> 
            <configuration>
                <port>${webserver.port}port>
                <uriEncoding>${project.build.sourceEncoding}uriEncoding>
            configuration>
        plugin>
    plugins>  
  build>  

project>

配置log4j.properties文件

log4j.rootLogger=debug,A1,R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Target=System.out
log4j.appender.common.DatePattern = '.'yyyy-MM-dd
log4j.appender.common.Threshold = INFO
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c:%L] %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=../logs/SSM
log4j.appender.R.DatePattern ='-'yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.Append = true
log4j.appender.R.ImmediateFlush = true
log4j.appender.R.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c:%L] %m%n

配置数据库文件

即db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/test
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=3
jdbc.maxIdle=1
validationQuery.sql=SELECT 1 FROM DUAL

配置mybatis文件

即mybatis-config.xml

  
  
<configuration>  
      
    <settings>  
    
        <setting name="logImpl" value="STDOUT_LOGGING" />  
          
        <setting name="lazyLoadingEnabled" value="true" />  
        <setting name="aggressiveLazyLoading" value="false" />  
        <setting name="mapUnderscoreToCamelCase" value="true" />  
    settings>  
      
    
configuration>

配置mybatis与spring整合文件

即applicationContext-mybatis.xml

  
<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" xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/tx    
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  
    <context:property-placeholder location="classpath:db.properties" /> 
       
     
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
    init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}">property>
        <property name="url" value="${jdbc.url}">property>
        <property name="username" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
     
        <property name="initialSize" value="0" />
        
        <property name="maxActive" value="20" />
    
        <property name="maxIdle" value="20" />

        
        <property name="minIdle" value="5" />
    
        <property name="maxWait" value="60000" />
         
        <property name="validationQuery" value="${validationQuery.sql}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

    
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        
        <property name="removeAbandoned" value="true" />
    
        <property name="removeAbandonedTimeout" value="3600" />
        
        <property name="logAbandoned" value="true" />

        
        <property name="filters" value="stat" />

   bean> 

   

      
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource">property>  
        
         <property name="typeAliasesPackage" value="com.sise.model" />
        <property name="mapperLocations" value="classpath:com/sise/mapper/*Mapper.xml" />
        <property name="configLocation" value="classpath:mybatis-config.xml">property>  
    bean> 

   

      
   

      
    <bean id="txManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource">property>  
    bean>  

      
        
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>  

beans>

配置springMVC文件

即spring-mvc.xml

  
<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" xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/tx    
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
        default-autowire="byName" default-lazy-init="false" >  
      
    <mvc:annotation-driven/>  
      
    <context:component-scan base-package="com.sise.controller"/>  
    <context:component-scan base-package="com.sise.dao"/>  
    <context:component-scan base-package="com.sise.service"/>  

      
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/views/">property>  
        <property name="suffix" value=".jsp">property>  
    bean>  
      
     
    <mvc:resources mapping="/vendor/**" location="/vendor/" />
    <mvc:resources mapping="/dist/**" location="/dist/" />
beans>

配置web.xml文件



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
            classpath*:applicationContext-mybatis.xml
        param-value>
    context-param>
    <filter>
        <filter-name>encodingFilterfilter-name>
        <description>字符集过滤器description>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
            <description>字符集编码description>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
    listener>
    
    <servlet>
        <servlet-name>springMvcservlet-name>
        <description>spring mvc servletdescription>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:spring-mvc.xmlparam-value>
            <description>spring mvc 配置文件description>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    
    <servlet>
        <servlet-name>DruidStatViewservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>

    <servlet-mapping>
        <servlet-name>DruidStatViewservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>
web-app>

配置完成截图:
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第12张图片
报错解决:
如果有其他错误可以参考我另一篇博客:https://blog.csdn.net/u014204541/article/details/80915507
更新maven项目
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第13张图片
更新完:还得设置一下jdk版本,一般默认更新后版本会跳到1.5去了:
这里可以配置不跳:参考配置博客:
https://blog.csdn.net/u014204541/article/details/80194567
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第14张图片
完成效果:
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第15张图片

代码测试

新建表

CREATE TABLE `user` (
  `user_id` varchar(10) NOT NULL DEFAULT '',
  `user_name` varchar(50) DEFAULT NULL,
  `user_age` varchar(50) DEFAULT NULL,
  `department_id` int(10) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

新建bean

package com.sise.model;

import java.io.Serializable;

public class User implements Serializable{

    private static final long serialVersionUID = 5507838140752571489L;

    private String userId;
    private String userName;
    private String userAge;
    private int departmentId;

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserAge() {
        return userAge;
    }
    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
    public int getDepartmentId() {
        return departmentId;
    }
    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    @Override
    public String toString() {
        return "User [userId=" + userId + ", userName=" + userName + ", userAge=" + userAge + ", departmentId="
                + departmentId + "]";
    }
}

新建baseDao

package com.sise.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.DaoSupport;
import org.springframework.util.Assert;

public class BaseDao extends DaoSupport{
    protected final Log log = LogFactory.getLog(getClass());
    private SqlSessionFactory sqlSessionFactory;
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    protected void checkDaoConfig() throws IllegalArgumentException {
        Assert.notNull(this.sqlSessionFactory, "sqlSessionFactory must be not null");

    }

    public SqlSessionFactory getSqlSessionFactory() {
        return this.sqlSessionFactory;
    }

    public void setSqlSessionFactory(SqlSessionFactory paramSqlSessionFactory) {
        this.sqlSessionFactory = paramSqlSessionFactory;
        this.sqlSessionTemplate = new SqlSessionTemplate(paramSqlSessionFactory);
    }

    public SqlSessionTemplate getSqlSessionTemplate() {
        return this.sqlSessionTemplate;
    }

}

新建TestDao

package com.sise.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.sise.model.User;
@Repository
public class TestDao extends BaseDao{

    public List getUserList(){
        return getSqlSessionTemplate().selectList("com.sise.model.User.getUserList", "");
    }
}

新建service接口类TestService

package com.sise.service;

import java.util.List;

import com.sise.model.User;

public interface TestService {

    public List getUserList();

}

新建service实现类TestServiceImpl

package com.sise.service.impl;

import java.util.List;

import javax.annotation.Resource;

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

import com.sise.dao.TestDao;
import com.sise.model.User;
import com.sise.service.TestService;

@Service
public class TestServiceImpl implements TestService{

    @Resource
    private TestDao testDao;

    @Override
    public List getUserList() {
        // TODO Auto-generated method stub
        return testDao.getUserList();
    }

}

新建userMapper





<mapper namespace="com.sise.model.User">
    
    

    <select id="findUserById" parameterType="String" resultType="com.sise.model.User">
        SELECT * FROM USER WHERE userId=#{userId}
    select>

    <select id="finUserByNameAndPsw" parameterType="java.util.Map" resultType="com.sise.model.User">
        select * from user where department_id=#{departmentId} and passwd=#{passwd}
    select>

    <select id="getUserList" resultType="com.sise.model.User">
        select * from user;
    select>

    <delete id="deleteUser" parameterType="String">
        delete from user where userId=#{userid}
    delete>

    <select id="getLong" resultType="Integer">
        select count(*) from user
    select>


mapper>

新建TestController

package com.sise.controller;

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

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

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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sise.model.User;
import com.sise.service.TestService;


@Controller
@RequestMapping("/testController")
public class TestController {

    @Resource
    private TestService testService;

    @RequestMapping(params = "router")
    @ResponseBody
    public ModelAndView router(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("dddddddddd");
        List list= testService.getUserList();
        for (int i = 0; i < list.size(); i++) {
            User user= (User) list.get(i);
            System.out.println(user.toString());
        }
        return new ModelAndView("test");
    }

}

新建jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>
head>
<body>
接口访问成功
body>
html>

所有新建文件的目录结构如下:
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第16张图片
运行项目查看效果:
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第17张图片
配置运行参数
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第18张图片
浏览器访问:http://localhost:8084/SSM/
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第19张图片
访问接口: http://localhost:8084/SSM/testController.do?router
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第20张图片
日志打印,我的数据库就有两条记录,这里打印语句就是在TestController的:
如何搭建Spring+SpringMVC+Mybatis+Maven+注解开发项目_第21张图片
好了,大功告成了
下面是项目完整源码:
https://download.csdn.net/download/u014204541/10535560

你可能感兴趣的:(java,SSM,springmvc,mybatis,框架)