SSM small book city integration, novice framework integration exercises

All files are on Github (github.com/laowenruo/S…

This project is mainly used for beginners just getting started with Spring, Mybatis,SpringMVC framework, need to practice the integration, familiar with the framework, or you can learn or learn Springboot and other content (if this project is helpful to you, Please watch, star and fork to encourage the author. Thank you.

Database environment

  • Create a database table that holds the book data
  • The file is database.sql

Basic Environment construction

  • Create a New Maven project! Add Web support
  • Import the relevant POM dependencies!
  • File for pom. XML
  • File for Maven resource filtering Settings, static resource export problem
<build>
   <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
   </resources>
</build>

Copy the code
  • Set up the basic structure and configuration framework!

Mybatis layer to write

  • Database configuration file database.properties

    jdbc.driver=com.mysql.jdbc.Driver # &serverTimezone=Asia/Shanghai jdbc.url=jdbc:mysql://localhost:3306/ssmbuild? useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456Copy the code
  • Compile MyBatis core configuration file MyBatis -config.xml

    <? The 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" > <configuration> <typeAliases> <package name="com.kuang.pojo"/> </typeAliases> <mappers> <mapper resource="com/kuang/dao/BookMapper.xml"/> </mappers> </configuration>Copy the code
  • Write the entity class com.pojo.Books for the database, using the Lombok plug-in!

  • Write Dao layer Mapper interface!

  • Compile mapper.xml file corresponding to the interface. Need to import MyBatis package;

  • Write interfaces and implementation classes for the Service layer

Spring layer

  • Configure Spring to integrate MyBatis. Here, the data source uses C3P0 connection pool.

  • We go to write Spring integration of Mybatis related configuration files; spring-dao.xml

  • <? The 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <! Mybatis --> <! - 1. Relational database file -- > < context: the property - placeholder location = "classpath: database. The properties" / > <! -- 2. Database connection pool --> <! -- DBCP semi-automatic operation cannot automatically connect to c3P0 automatic operation (automatically load configuration file and set to object) --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <! <property name="driverClass" value="${jdbc.driver}"/> <property name=" jdbc.driver "value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <! Property name="minPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <property name="autoCommitOnClose" value="false"/> <! <property name="checkoutTimeout" value="10000"/> <! <property name=" attempts "value="2"/> </bean> <! - 3. Configure SqlSessionFactory object - > < bean id = "SqlSessionFactory" class = "org. Mybatis. Spring. SqlSessionFactoryBean" > <! < dataSource ="dataSource"/> <! <property name="configLocation" value=" clasSPath :mybatis-config.xml"/> </bean> <! 4. Configure the scan Dao interface package and dynamically implement the Dao interface injection into the Spring container. - explanation: https://www.cnblogs.com/jpfss/p/7799806.html--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <! - injection sqlSessionFactory - > < property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" / > <! <property name="basePackage" value="com.kuang. Dao "/> </bean> </beans>Copy the code
  • Spring integrates the service layer, spring-service. XML file written

    <? The 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" 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"> <! <context:component-scan base-package="com.service" /> <! - BookServiceImpl injection to the IOC container - > < bean id = "BookServiceImpl" class = "com. Service. BookServiceImpl" > < property name="bookMapper" ref="bookMapper"/> </bean> <! - configuration transaction manager - > < bean id = "transactionManager" class = ". Org. Springframework. JDBC datasource. DataSourceTransactionManager "> <! < dataSource ="dataSource" /> </bean> </beans>Copy the code

For SpringMVC layer

  • Web.xml to write

    <? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" Version = "4.0" > <! --DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <! Note: we are loading the total configuration file here, which was corrupted before! --> <param-value>classpath:applicationContext.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> <! --encodingFilter--> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <! </session-timeout> </session-timeout> </session-config> </web-app>Copy the code
  • Spring – the MVC. Write XML

    <? The 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:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <! -- Configure SpringMVC --> <! -- 1. Enable SpringMVC annotation driver --> < MVC :annotation-driven /> <! MVC :default-servlet-handler/> <! - 3. Configure the JSP shows ViewResolver view parser - > < bean class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <! <context:component-scan base-package="com.kuang. Controller "/> </beans>Copy the code

And finally, the big integration

  • applicationContext.xml

    <? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" 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"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans>Copy the code

Now I’m free to write the Controller and view layer, which I won’t do here

  • This project is mainly based on the crazy god said SpringMVC integration book city project written, the additional is to try the next PageHelper plug-in, to achieve paging