Spring Integration SpringMVC + Mybatis Basic Framework Configuration File Detailed Explanation
- 2021-08-12 02:44:11
- OfStack
Preface
Create a new ordinary Maven project
Basic directory structure
-- src #
The -- main #
The The Off- java # java Code directory
The The Off- resources # Configuration file directory , Store below Spring Configuration file
The -- test # Unit test directory
-- web # web Directory
The Off- WEB-INF # web.xml Configuration file directory
1. Mybatis layer writing
1. In
resources
Create a new database configuration file under the directory
database.properties
jdbc.driver=com.mysql.jdbc.Driver
# If you are using MySQL8.0+ Then it needs to be increased 1 Configuration of time zones ; serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
2. In
resources
Create Mybatis configuration file under the directory
mybatis-config.xml
<?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>
<!-- Configure the data source , Hand over Spring-->
<!-- Configure log-->
<settings>
<!--STDOUT_LOGGING: Standard log factory implementation -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- Configuration alias -->
<typeAliases>
<package name="com.pro.pojo"/>
</typeAliases>
<!-- Binding Mapper-->
<mappers>
<mapper class="com.pro.dao.BooksMapper"/>
</mappers>
</configuration>
2. Spring layer writing
1. Spring integrates Mybatis
Configure Spring to integrate MyBatis, where the data source uses c3p0 connection pool;
Write the relevant configuration files of Spring integration Mybatis; In
resources
Directory
spring-dao.xml
Note: Two configuration files of Mybatis layer above should be introduced here, and the name of the configuration file should not be written incorrectly
<?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">
<!--1. Associated database configuration file -->
<context:property-placeholder location="classpath:database.properties"/>
<!--2. Connection pool
dbcp: Semi-automatic operation , Unable to connect automatically
c3p0: Automated operation ( Automatically load configuration files and set them into objects )
druid, hikari
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--c3p0 Private properties of connection pool , Maximum and Minimum Connection Pool Size -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- Not automatically after closing the connection commit-->
<property name="autoCommitOnClose" value="false"/>
<!-- Connection timeout -->
<property name="checkoutTimeout" value="10000"/>
<!-- Get the number of connection failure retries -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3. sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- Binding Mybatis Configuration file -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4. Configure Dao Scanning packet , Dynamic implementation Dao Interface is injected into the Spring In a container -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Injection sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- Configure the dao Bag -->
<property name="basePackage" value="com.pro.dao"/>
</bean>
</beans>
2. Spring integrates service
Inject the business layer classes into Spring, in the
resources
Directory
spring-service.xml
<?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">
<!--1. Scanning service Package under -->
<context:component-scan base-package="com.pro.service"/>
<!--2. Inject the business layer classes into the Spring Medium -->
<bean id="BooksServiceImpl" class="com.pro.service.BooksServiceImpl">
<property name="booksMapper" ref="booksMapper"/>
</bean>
<!--3. Configuring Declarative Transactions -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- Injection data source -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--4. Configure aop Implement transaction weaving -->
<!-- Configuring Transaction Notifications -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--1. Configure transactions for those methods -->
<!--2. Propagation characteristics of configuration transactions : propagation-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- Configure transaction cut-in -->
<aop:config>
<!--mapper All methods of all classes under the package -->
<aop:pointcut id="txPointCut" expression="execution(* com.pro.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
3. SpringMVC Layer Writing
1. Write web. xml
Modify
WEB-INF
Under
web.xml
Documents
Configuration files for Spring integration are introduced here
database.properties
0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<!--DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Loading Spring Configuration file -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- Startup level -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Garbled filtering -->
<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 Expired time -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
2. Write spring-mvc. xml
In
resources
Directory
spring-mvc.xml
<?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: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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1. Annotation driven -->
<mvc:annotation-driven/>
<!--2. Static resource filtering -->
<mvc:default-servlet-handler/>
<!--3. Scanning packet : controller-->
<context:component-scan base-package="com.pro.controller"/>
<!--4. View parser -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4. Spring Configuration Consolidation File, applicationContext. xml
In
resources
Directory
database.properties
0
The above three configuration files are introduced here
spring-dao.xml
,
spring-service.xml
,
spring-mvc.xml
Consolidate into a total 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
Dependency
<!-- Dependency -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- Database driven -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- Database connection pool -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<!-- 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>