SSM框架整合(IntellIj IDEA+Maven+Spring+SpringMVC+MyBatis)之Spring Framework

1.配置SpringMVC

1.1编写Spring配置文件

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    <context:component-scan base-package="com.jyh.controller" >context:component-scan>

    
    <mvc:annotation-driven />

    
    
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    bean>

beans>

1.2编写web.xml配置文件


<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" version="3.1">
  <display-name>BlogManagedisplay-name>

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

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

  
  <filter>
    <filter-name>CharacterEncodingFilterfilter-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>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc-servlet.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
web-app>

1.3编写Controller、jsp & 运行测试

UserController

/**
 * Created by OverrideRe on 2017/7/6.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "/getUser")
    public String getUser(Model model) throws Exception {
        User user = new User();
        user.setUserName("OverrideRe");
        user.setCity("上海");
        model.addAttribute("user", user);
        return "userInfo";
    }
}

WEB-INF/jsp/userInfo.jsp

<%--
  Created by IntelliJ IDEA.
  User: OverrideRe
  Date: 2017/7/6
  Time: 17:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
    ${user.userName}你好
body>
html>

运行结果

SSM框架整合(IntellIj IDEA+Maven+Spring+SpringMVC+MyBatis)之Spring Framework_第1张图片

2.整合SSM

2.1将MyBatis整合进Spring

在applicationContext.xml文件中整合MyBatis配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" 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/aop
                      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                      http://www.springframework.org/schema/tx
                      http://www.springframework.org/schema/tx/spring-tx-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/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    
    <context:component-scan base-package="com.jyh.controller,com.jyh.service.impl" >context:component-scan>


    


    
    <mvc:annotation-driven />

    
    
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    bean>


    


    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        
        <property name="basePackage" value="com.jyh.dao" />
    bean>


    


    
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.propertiesvalue>
        property>
    bean>

    <bean id="dataSource" destroy-method="close"
          class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml">property>
    bean>

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

    
    <tx:advice transaction-manager="transactionManager" id="tx">
        <tx:attributes>
            <tx:method name="save*" read-only="false"/>
            <tx:method name="update*" read-only="false"/>
            <tx:method name="delete*" read-only="false"/>
            
            <tx:method name="*" read-only="true"/>
        tx:attributes>
    tx:advice>

    
    <aop:config>
        <aop:pointcut
                expression="execution(* com.jyh.dao.*.*(..))" id="perform"/>
        <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
    aop:config>
beans>

2.2编写service、controller

service

/**
 * Created by OverrideRe on 2017/7/6.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public User findUserById(String userId){
        return userMapper.selectByPrimaryKey(userId);
    }
}

controller

/**
 * Created by OverrideRe on 2017/7/6.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource(name = "userService")
    private UserService userService;

    @RequestMapping(value = "/getUser")
    public String getUser(Model model) throws Exception {
        User user = userService.findUserById("111");
        model.addAttribute("user", user);
        return "userInfo";
    }
}

2.3运行结果 & 完整模块

运行结果

SSM框架整合(IntellIj IDEA+Maven+Spring+SpringMVC+MyBatis)之Spring Framework_第2张图片

完整模块

SSM框架整合(IntellIj IDEA+Maven+Spring+SpringMVC+MyBatis)之Spring Framework_第3张图片

github地址

你可能感兴趣的:(spring,java)