SSM Integration (spring+springMVC+mybatis)

SSM Integration What is SSM That is, technology that integrates the three frameworks of Spring, Spring MVC and Mybati...
SSM Integration

SSM Integration

What is SSM

That is, technology that integrates the three frameworks of Spring, Spring MVC and Mybatis.

Integrative thinking

  1. Build an integrated environment
  2. Set up the spring configuration first
  3. Reuse spring to integrate the spring MVC framework
  4. Finally, use Spring to integrate the Mybatis framework

Integration steps

  1. Create a Maven project and import the required coordinate dependencies (version and connection pools vary from person to person)

    <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <!-- Database Configuration --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!-- Spring To configure--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>$</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!-- jsp --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!-- log Journal --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.30</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <!-- Connection Pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <dependency> <!-- test --> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <!-- Servlet --> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies>
  2. Write a configuration file for spring

    <?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd" > <!--Turn on scan, only want to process Service and dao,controller Unwanted Spring Frame Processing--> <context:component-scan base-package="cn.okt"> <!--Configure which do not scan--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
  3. Write java code

    1. Business Tier

      public interface AccountService { // Query all account information List<Account> findAll(); // Save account information void saveAccount(Account account); }
      @Service("accountService") public class AccountServiceImpl implements AccountService{ public List<Account> findAll() { System.out.println("Business Tier: Query all accounts..."); return null; } public void saveAccount(Account account) { System.out.println("Business Tier: Save Accounts..."); } }
    2. Persistence Layer

      /** * Account dao interface */ public interface AccountDao { // Query all accounts public List<Account> findAll(); // Save account information public void saveAccount(Account account); }
    3. Entity Class

      public class Account implements Serializable { private Integer id; private String name; private Double money; 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 Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
  4. Write test classes to test if the spring environment is successfully built

    public class TestSpring { @Test public void run1(){ ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:spring Configuration File Name"); AccountService service=(AccountService) ac.getBean("accountService"); service.findAll(); service.saveAccount(new Account()); } }
  5. Spring integrates springMVC, writes web.xml, configures front-end controllers, Chinese scrambling, Spring listeners

    <!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> <!--To configure Spring Monitor, load only by default WEB-INF Directory applicationContext.xml configuration file--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Set the path to the configuration file--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring Configuration Name</param-value> </context-param> <!--Configure Front End Controller--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--Load springmvc.xml configuration file--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc Configuration Name</param-value> </init-param> <!--Start the server and create the servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--Filter to solve Chinese random code--> <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>
  6. Write SpringMVC configuration file

    <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 http://www.springframework.org/schema/beans/spring-beans-4.2.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-4.2.xsd"> <!--Turn on Annotation Scan--> <context:component-scan base-package="cn.okt"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--View Parser Object--> <bean id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--Front End Controller, which static resources are not intercepted <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> --> <!--open SpringMVC Frame Annotation Support--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
  7. Write Control Layer Code

    /** * Account web */ @Controller @RequestMapping("/account") public class AccountController { @Autowired private AccountService accountService; @RequestMapping("/findAll") public String findAll(Model model){ System.out.println("Presentation layer: Query all accounts..."); // Method to invoke service List<Account> list = accountService.findAll(); model.addAttribute("list",list); return "list"; } }
  8. Write front-end pages and open tomcat tests

    <index page> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>test</h3> <a href="account/findAll">test</a> <form action="account/save" method="post"> Full name:<input type="text" name="name"><br> Amount of money:<input type="text" name="money"><br> <input type="submit" value="Preservation"> </form> </body> </html>
    <list page> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Title</title> </head> <body> <h3>Successfully queried</h3> </body> </html>
  9. Spring integrates Mybatis and modifies persistence and business tier code

    @Service("accountService") public class AccountServiceImpl implements AccountService{ @Autowired private AccountDao accountDao; public List<Account> findAll() { System.out.println("Business Tier: Query all accounts..."); return accountDao.findAll(); } public void saveAccount(Account account) { System.out.println("Business Tier: Save Accounts..."); accountDao.saveAccount(account); } }
    /** * Account dao interface */ @Repository public interface AccountDao { // Query all accounts @Select("select * from account") public List<Account> findAll(); // Save account information @Insert("insert into account (name,money) values (#,#)") public void saveAccount(Account account); }
  10. Configuring mybatis in the spring configuration file

    <?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd" > <!--Turn on scan, only want to process Service and dao,controller Unwanted Spring Frame Processing--> <context:component-scan base-package="cn.okt"> <!--Configure which do not scan--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--spring integration mybatis--> <!--Configure Connection Pool--> <bean id="dataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///ssm?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="19970930"></property> </bean> <!--To configure SqlSessionFactory--> <bean id="sessionFactory"> <property name="dataSource" ref="dataSource"></property> </bean> <!--To configure AccountDao Location of interface--> <bean id="mapperScanner"> <property name="basePackage" value="cn.okt.dao"></property> </bean> </beans>
  11. Modify the front-end page list.jsp and open Tomcat for testing

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Title</title> </head> <body> <h3>Successfully queried</h3> <c:forEach items="$" var="account"> $ </c:forEach> </body> </html>
  12. Spring Configuration Transaction, added as follows in the spring configuration file

    <!--Configure Transaction Notifications--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--Method Name find*Are read-only and do not commit transactions--> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" isolation="DEFAULT"></tx:method> </tx:attributes> </tx:advice> <!--To configure AOP Enhanced, business tier is typically chosen to control transactions--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.okt.service.impl.*ServiceImpl.*(..))"></aop:advisor> </aop:config>
  13. Add save methods at the control level

    @RequestMapping("/save") public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("Performance layer: increase account..."); // Method to invoke service accountService.saveAccount(account); //redirect response.sendRedirect(request.getContextPath()+"/account/findAll"); }
  14. Modify the front-end page and start Tomcat testing

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>test</h3> <a href="account/findAll">test</a> <form action="account/save" method="post"> Full name:<input type="text" name="name"><br> Amount of money:<input type="text" name="money"><br> <input type="submit" value="Preservation"> </form> </body> </html>

summary

Ultimately, we want to integrate the other two frameworks with Spring as the core. If the above tests pass, you will have successfully integrated the SSM environment, and then you can develop the project.I've had a crazy problem here, writing the listening class wrong in web.xml, which prevented me from loading my spring's configuration file when I opened Tomcat, so I couldn't create all the bean objects. So once an error occurs, it's important to have a good look at whether there are mismatched classes in the configuration file and whether the annotations are missing. This is a typical error, after all, people are incomplete.There is always a time when you are absent-minded, just pay more attention.

obeeey Fifteen original articles were published, 7 were praised, and 1889 were visited Private letter follow

10 February 2020, 22:29 | Views: 9419

Add new comment

For adding a comment, please log in
or create account

0 comments