Integration of SSM framework

Posted by snrecords on Wed, 05 Feb 2020 16:23:23 +0100

1. Create database first

2. Create project

3. Import required coordinates

4. Configure mybatis (using annotation method), and write SqlMapConfig.xml, the content is as follows (Dao layer configuration), and test

<?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">
  <!--mybatis Master profile-->
<configuration>
    <!--To configure properties-->
    <properties resource="jdbcConfig.properties">
        <!--<property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/user"/>
        <property name="username" value="root"/>
        <property name="password" value="wzp961208"/>-->

    </properties>
    <!--Use typeAliases Configure aliases-->
    <typeAliases>
        <typeAlias type="com.ping.domain.Users" alias="user"></typeAlias>
       <!-- Used to specify packages to configure aliases.Class name is alias,Case insensitive-->
        <package name="com.ping.domain"></package>
    </typeAliases>
    <!--Configuration environment-->
    <environments default="mysql">
       <!-- To configure mysql Environmental Science-->
        <environment id="mysql">
            <!--Configure transaction type-->
            <transactionManager type="JDBC"></transactionManager>
           <!-- Configure connection pool-->
            <dataSource type="POOLED">
                <!--Basic information of configuration connection database-->
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
   <!-- Mapping profiles, each independent dao Profile for-->
    <mappers>
       <!-- <mapper resource="com/ping/Dao/IUsersDao.xml"></mapper>-->
        <package name="com.ping.Dao"></package>
    </mappers>
</configuration>

5. Business layer configuration (edible Spring configuration) where ApplicationContext.xml is established, as follows

<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:component-scan base-package="com.ping">
        <!--Configure annotations that do not need to be scanned-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 
</beans>

6. Configuration of Spring MVC in the presentation layer. First, configure the front-end controller (web.xml) and view parser (Spring MVC.xml)

web.xml configuration:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>characterEncodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Spring MVC.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.ping">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <bean id ="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- Set static resources not to filter -->
    <mvc:resources location="/css/" mapping="/css/**" />
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />

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

</beans>

7. First, integrate spring and spring MVC, and configure the listener in web.xml

<!--  spring And spring MVC Integration-->
  <!--Listener, load only WEB-INF lower applicationContextx.xml file-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--Configuration path-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

8. Then, configure spring and mybatis in applicationContext.xml

The contents are as follows:

<!--spring integration mybatis-->
    <!--Configure connection pool-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///ssm"></property>
        <property name="user" value="root"></property>
        <property name="password" value="wzp961208"></property>
    </bean>
    <!--To configure SqlSessionFactoty-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--Configure the package of the interface-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ping.Dao"></property>
    </bean>
    <!--To configure spring Declarative transaction management-->
    <bean id ="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find" read-only="true"/>
            <tx:method name="*"   isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    <aop:config >
        <aop:advisor advice-ref="txadvice" pointcut="execution(* com.ping.Service.Impl.AccountServiceImp.*(..))"></aop:advisor>
    </aop:config>

9. Delete SqlMapConfig.xml

Published 127 original articles, won praise 4, visited 9610
Private letter follow

Topics: Spring xml JDBC Mybatis