20.SSM框架集~Spring MVC

20.SSM框架集~SSMS大总结之Spring MVC

本文是上一篇文章的后续,详情点击该链接~

初识SpringMVC

spring mvc依赖
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.2.2.RELEASEversion>
    dependency>
springmvc.xml配置
    
    <context:component-scan base-package="com.alvin.controller"/>
    
    <mvc:annotation-driven/>
web.xml


<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  <servlet>
    <servlet-name>mvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>mvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>
第一个SpringMVC程序
@Controller
public class MyController {
    @RequestMapping("/show")
    public String hello(){
        System.out.println("已运行SpringMVC");
        //跳转到index.jsp页面
        return "redirect:/index.jsp";
    }
}

将Spring,SpringMVC,Mybatis整合登录,并将内容传送到前台

所需依赖



<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.0modelVersion>

  <groupId>com.alvingroupId>
  <artifactId>springMvcInitartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <properties>
    <mysql-version>8.0.11mysql-version>
    <mybatis-version>3.5.2mybatis-version>
    <spring-version>5.2.2.RELEASEspring-version>
    <mybatis-spring-version>2.0.4mybatis-spring-version>
    <slf4j-version>1.7.25slf4j-version>
    <jstl-version>1.2jstl-version>
    <servlet-api-version>3.1.0servlet-api-version>
    <jsp-version>2.2jsp-version>
    <junit-version>4.12junit-version>
    <jackson-version>2.9.9jackson-version>
  properties>

  <dependencies>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>${mysql-version}version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis-version}version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring-version}version>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring-version}version>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>${spring-version}version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring-version}version>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>${mybatis-spring-version}version>
    dependency>

    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>${slf4j-version}version>
    dependency>

    
    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>${jstl-version}version>
    dependency>

    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson-version}version>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>${servlet-api-version}version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>${jsp-version}version>
      <scope>providedscope>
    dependency>

    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit-version}version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.aspectjgroupId>
      <artifactId>aspectjweaverartifactId>
      <version>1.9.4version>
    dependency>

  dependencies>

  <build>
    <plugins>
       
       <plugin>
          <groupId>org.apache.tomcat.mavengroupId>
          <artifactId>tomcat7-maven-pluginartifactId>
          <version>2.2version>
          <configuration>
          
          <port>8888port>
          
          <path>/path>
          configuration>
       plugin>
    plugins>
  build>
project>

配置文件

applicationContext-mybatis.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

        
        <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>

       
       <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="${m_driver}">property>
           <property name="url" value="${m_url}">property>
           <property name="username" value="${m_uname}">property>
           <property name="password" value="${m_pwd}">property>
       bean>

       
       <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean" >
           <property name="dataSource" ref="ds">property>
           <property name="typeAliasesPackage" value="com.alvin.pojo">property>
       bean>

      
      <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="sqlSessionFactoryBeanName" value="factory">property>
          <property name="basePackage" value="com.alvin.mapper">property>
      bean>
beans>
applicationContext-service.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

      
      <context:component-scan base-package="com.alvin.service.impl">context:component-scan>

    

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

      
     <context:component-scan base-package="com.alvin.controller">context:component-scan>

      
     <mvc:annotation-driven>mvc:annotation-driven>

    
    
    <mvc:resources mapping="/img/**" location="/img/">mvc:resources>

    <mvc:resources mapping="/css/**" location="/css/">mvc:resources>

    <mvc:resources mapping="/js/**" location="/js/">mvc:resources>

beans>

代码

Controller
@Controller
public class UserController {

    @Autowired
    private UserService  userService;

    @RequestMapping("/findAllUser")
    @ResponseBody
    public List<User>   findAllUser(Integer son,String password){
        User user = userService.Login(son,password);
        if(user != null){
            return  userService.findAll();
        }else{
            return null;
        }
    }
}
Service
@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper  userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public User Login(Integer son, String password) {
        return userMapper.Login(son,password);
    }
}
Mapper
public interface UserMapper {

    @Select("select *  from  student")
    List<User>  selectAll();

    @Select("select * from student where son = #{param1} and password = #{param2}")
    User Login(Integer son,String password);
}

web.xml



<web-app>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext-*.xmlparam-value>
  context-param>

  
  <filter>
    <filter-name>encfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>

  <filter-mapping>
    <filter-name>encfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>


  
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>0load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

你可能感兴趣的:(SSM框架集)