1. Create a new MAVEN project and import the required dependent and static resource files
<dependencies> <!--junit rely on--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!--mysql rely on--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <!--mybatis rely on--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- spring rely on--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.9</version> </dependency> <!-- servlet-api--> <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> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- AOP Cut in transaction--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies> <!-- Static resource file--> <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>
2. The database needed to connect
3. Configure the mybatis-config.xml file. Since Spring can integrate Mybatis, some aliases and settings can be placed in the configuration file in Mybatis. If possible, all of them can be placed in the configuration file of Spring
<?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.wang.pojo"/> </typeAliases> </configuration>
4. Write the spring configuration file of applicationContext.xml. For easier identification, there are spring-dao.xml,spring-service.xml and spring-mvc.xml files below
<?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>
5. Build mapper(dao), pojo, service and controller (servlet) packages to write entity classes
6. Write the configuration file of spring Dao, corresponding to the mapper package
The main function of spring Dao configuration file is to connect to the database and configure some configuration files of mybatis, and the SQL statements used in the xml configuration file of the interface under mapper can be connected to the database!
<?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,Import data source--> <context:property-placeholder location="classpath:database.properties"/> <!-- 2,Connection data pool dataSource 1,c3p0 automation Cannot connect automatically 2,dbcp Semi automatic can be connected automatically. Use this below --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <!--When using connection pool username This property cannot be associated with value The variables inside have the same name, otherwise they will conflict--> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> </bean> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">--> <!-- <property name="driverClass" value="${driver}"/>--> <!-- <property name="jdbcUrl" value="${url}"/>--> <!-- <property name="user" value="${username}"/>--> <!-- <property name="password" value="${password}"/>--> <!-- </bean>--> <!-- 3,SqlSessionFactory Mybatis Configuration in--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/wang/mapper/*.xml"/> </bean> <!-- 4,to configure Mapper Interface scanning package, dynamic configuration DAO Interface injection Spring In container--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.wang.mapper"/> </bean> </beans>
Configuration in database.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf-8 user=root pwd=123456
There is a mistake here
Do not set the two names to the same, otherwise they will conflict due to naming rules
7. You can write interfaces and xml files for the mapper layer and query some data. I wrote some CRUD methods here
7.1. BookMapper interface
package com.wang.mapper; import com.wang.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BooksMapper { //increase int addBook(Books book); //Delete int deleteBookById(@Param("BookId") int id); //change int updateBook(Books book); //Check one Books queryBookById(@Param("BookId") int id); //Check all List<Books> queryBook(); List<Books> queryBookByName(String BookName); }
7.2 mapper interface XML configuration file BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wang.mapper.BooksMapper"> <insert id="addBook" parameterType="books"> insert into books (BookName,BookPrice,BookInfo) values (#{BookName},#{BookPrice},#{BookInfo}); </insert> <delete id="deleteBookById" parameterType="_int"> delete from books where BookId=#{BookId}; </delete> <update id="updateBook" parameterType="books"> update books set BookName = #{BookName},BookPrice=#{BookPrice},BookInfo=#{BookInfo} where BookID=#{BookID}; </update> <select id="queryBookById" parameterType="_int" resultType="books"> select * from books where BookId=#{BookId}; </select> <select id="queryBook" resultType="Books"> select * from books; </select> <select id="queryBookByName" resultType="Books"> select * from books where BookName like concat('%',#{BookName},'%') </select> </mapper>
8. Service is the business layer, which mainly obtains some Mapper data. Therefore, there should also be a service interface corresponding to Mapper's interface and an implementation class.
8.1BookService interface
package com.wang.service; import com.wang.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookService { //increase int addBook(Books book); //Delete int deleteBookById(@Param("BookId") int id); //change int updateBook(Books book); //Check one Books queryBookById(@Param("BookId") int id); //Check all List<Books> queryBook(); List<Books> queryBookByName(String BookName); }
8.2BookServiceImpl implementation class. Mapper should be used here, so a BookMapper should be declared and injected with the set method.
package com.wang.service; import com.wang.mapper.BooksMapper; import com.wang.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService{ private BooksMapper booksMapper; public void setBooksMapper(BooksMapper booksMapper) { this.booksMapper = booksMapper; } public int addBook(Books book) { return booksMapper.addBook(book); } public int deleteBookById(int id) { return booksMapper.deleteBookById(id); } public int updateBook(Books book) { return booksMapper.updateBook(book); } public Books queryBookById(int id) { return booksMapper.queryBookById(id); } public List<Books> queryBook() { return booksMapper.queryBook(); } public List<Books> queryBookByName(String BookName) { return booksMapper.queryBookByName(BookName); } }
9. Configure the Spring-service.xml configuration file. The main operation of this configuration file is to inject the implementation of the service. Transactions and AOP entry are also done in this 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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1,scanning service The following package--> <context:component-scan base-package="com.wang.service"/> <!--2,Configure all business classes to spring,It can be implemented by configuration or annotation@Autowired--> <bean id="bookServiceImpl" class="com.wang.service.BookServiceImpl"> <property name="booksMapper" ref="booksMapper"/> </bean> <!--3,Declarative transaction--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"> <!-- Injection data source--> <property name="dataSource" ref="dataSource"/> </bean> <!-- 4,AOP Cut in transaction--> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <!--All methods are transactional--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--aop cut-in--> <aop:config> <!--All classes and methods below--> <aop:pointcut id="txPointCut" expression="execution(* com.wang.service.*.*(..))"/> <!--notice:Cut the transaction class into mapper All classes and methods below--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> </beans>
11. Import WEB project and configure WEB file
<?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"> <servlet> <servlet-name>springmvc</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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>Encoding</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>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
You also need to configure the package to the web project
10. Configure the spring MVC configuration file. Spring MVC corresponds to the controller layer and interacts with the front end
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Scan package--> <context:component-scan base-package="com.wang.cotroller"/> <!-- Annotation driven--> <mvc:annotation-driven> <!-- JSON Garbled code--> <!-- <mvc:message-converters>--> <!-- <bean class="org.springframework.http.converter.StringHttpMessageConverter">--> <!-- <constructor-arg value="UTF-8"/>--> <!-- </bean>--> <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> <!-- <property name="objectMapper">--> <!-- <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">--> <!-- <property name="failOnEmptyBeans" value="false"/>--> <!-- </bean>--> <!-- </property>--> <!-- </bean>--> <!-- </mvc:message-converters>--> </mvc:annotation-driven> <!-- Static resource filtering--> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
At this time, the underlying construction is almost the same. The following are some basic jsp projects that need front-end interaction
addbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>Add books</small> </h1> <form action="/book/addBook" method="get"> <div class="form-group"> <label>Book name</label> <input type="text" class="form-control" name="BookName" required> </div> <div class="form-group"> <label>Book price</label> <input type="text" class="form-control" name="BookPrice" required> </div> <div class="form-group"> <label>Book information</label> <input type="text" class="form-control" name="BookInfo" required> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </div> </body> </html>
showbook.jsp
1,newly build MAVEN Project, and import the required dependent and static resource files <dependencies> <!--junit rely on--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!--mysql rely on--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <!--mybatis rely on--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- spring rely on--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.9</version> </dependency> <!-- servlet-api--> <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> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- AOP Cut in transaction--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies> <!-- Static resource file--> <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> 2,The database needed to connect 3,to configure mybatis-config.xml File, due to Spring Can integrate Mybatis,stay Mybatis You can put some aliases and settings in the configuration file. If you can, you can put all of them Spring In the configuration file <?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.wang.pojo"/> </typeAliases> </configuration> 4,write in applicationContext.xml of spring Configuration file. For easier identification, there are spring-dao.xml,spring-service.xml and spring-mvc.xml 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="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans> 5,structure mapper(dao),pojo,service,controller(servlet)Package to write entity classes 6,write spring-dao Configuration file, corresponding to mapper package spring-dao The main function of the configuration file is to connect and configure the database mybatis Some configuration files for,And will mapper Of the following interfaces xml Configuration file used SQL Statement can be docked with the database! <?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,Import data source--> <context:property-placeholder location="classpath:database.properties"/> <!-- 2,Connection data pool dataSource 1,c3p0 automation Cannot connect automatically 2,dbcp Semi automatic can be connected automatically. Use this below --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <!--When using connection pool username This property cannot be associated with value The variables inside have the same name, otherwise they will conflict--> <property name="username" value="${user}"/> <property name="password" value="${pwd}"/> </bean> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">--> <!-- <property name="driverClass" value="${driver}"/>--> <!-- <property name="jdbcUrl" value="${url}"/>--> <!-- <property name="user" value="${username}"/>--> <!-- <property name="password" value="${password}"/>--> <!-- </bean>--> <!-- 3,SqlSessionFactory Mybatis Configuration in--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/wang/mapper/*.xml"/> </bean> <!-- 4,to configure Mapper Interface scanning package, dynamic configuration DAO Interface injection Spring In container--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.wang.mapper"/> </bean> </beans> database.properties Configuration inside driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=utf-8 user=root pwd=123456 There is a mistake here Do not set the two names to the same, otherwise they will conflict due to naming rules 7,Yes mapper Layer write interface and xml File, you can query some data. I wrote some here CRUD Method of 7.1,BookMapper Interface package com.wang.mapper; import com.wang.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BooksMapper { //increase int addBook(Books book); //Delete int deleteBookById(@Param("BookId") int id); //change int updateBook(Books book); //Check one Books queryBookById(@Param("BookId") int id); //Check all List<Books> queryBook(); List<Books> queryBookByName(String BookName); } 7.2Mapper Interfaced xml configuration file BookMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wang.mapper.BooksMapper"> <insert id="addBook" parameterType="books"> insert into books (BookName,BookPrice,BookInfo) values (#{BookName},#{BookPrice},#{BookInfo}); </insert> <delete id="deleteBookById" parameterType="_int"> delete from books where BookId=#{BookId}; </delete> <update id="updateBook" parameterType="books"> update books set BookName = #{BookName},BookPrice=#{BookPrice},BookInfo=#{BookInfo} where BookID=#{BookID}; </update> <select id="queryBookById" parameterType="_int" resultType="books"> select * from books where BookId=#{BookId}; </select> <select id="queryBook" resultType="Books"> select * from books; </select> <select id="queryBookByName" resultType="Books"> select * from books where BookName like concat('%',#{BookName},'%') </select> </mapper> 8,Service It is the business layer, which is mainly to obtain Mapper Some data, so there should also be one Service Interface Mapper And has implementation classes. 8.1BookService Interface package com.wang.service; import com.wang.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BookService { //increase int addBook(Books book); //Delete int deleteBookById(@Param("BookId") int id); //change int updateBook(Books book); //Check one Books queryBookById(@Param("BookId") int id); //Check all List<Books> queryBook(); List<Books> queryBookByName(String BookName); } 8.2BookServiceImpl Implementation class, which is used here Mapper,So declare a BookMapper,And use set Method injection. package com.wang.service; import com.wang.mapper.BooksMapper; import com.wang.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService{ private BooksMapper booksMapper; public void setBooksMapper(BooksMapper booksMapper) { this.booksMapper = booksMapper; } public int addBook(Books book) { return booksMapper.addBook(book); } public int deleteBookById(int id) { return booksMapper.deleteBookById(id); } public int updateBook(Books book) { return booksMapper.updateBook(book); } public Books queryBookById(int id) { return booksMapper.queryBookById(id); } public List<Books> queryBook() { return booksMapper.queryBook(); } public List<Books> queryBookByName(String BookName) { return booksMapper.queryBookByName(BookName); } } 9,to configure Spring-service.xml Configuration file. The main operation of this configuration file is to service Implementation injection, transaction and AOP The cut in is also done in this 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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 1,scanning service The following package--> <context:component-scan base-package="com.wang.service"/> <!--2,Configure all business classes to spring,It can be implemented by configuration or annotation@Autowired--> <bean id="bookServiceImpl" class="com.wang.service.BookServiceImpl"> <property name="booksMapper" ref="booksMapper"/> </bean> <!--3,Declarative transaction--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"> <!-- Injection data source--> <property name="dataSource" ref="dataSource"/> </bean> <!-- 4,AOP Cut in transaction--> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <tx:attributes> <!--All methods are transactional--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--aop cut-in--> <aop:config> <!--All classes and methods below--> <aop:pointcut id="txPointCut" expression="execution(* com.wang.service.*.*(..))"/> <!--notice:Cut the transaction class into mapper All classes and methods below--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> </beans> 11,Import WEB Project, configuration WEB file <?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"> <servlet> <servlet-name>springmvc</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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>Encoding</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>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> You also need to configure the package to web project 10,to configure Spring-MVC Profile, Spring-MVC Corresponding to controller Layer, which interacts with the front end <?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--Scan package--> <context:component-scan base-package="com.wang.cotroller"/> <!-- Annotation driven--> <mvc:annotation-driven> <!-- JSON Garbled code--> <!-- <mvc:message-converters>--> <!-- <bean class="org.springframework.http.converter.StringHttpMessageConverter">--> <!-- <constructor-arg value="UTF-8"/>--> <!-- </bean>--> <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> <!-- <property name="objectMapper">--> <!-- <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">--> <!-- <property name="failOnEmptyBeans" value="false"/>--> <!-- </bean>--> <!-- </property>--> <!-- </bean>--> <!-- </mvc:message-converters>--> </mvc:annotation-driven> <!-- Static resource filtering--> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> At this time, the underlying construction is almost the same. The following is the front-end interaction. I have some basic information below jsp project addbook.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>Add books</small> </h1> <form action="/book/addBook" method="get"> <div class="form-group"> <label>Book name</label> <input type="text" class="form-control" name="BookName" required> </div> <div class="form-group"> <label>Book price</label> <input type="text" class="form-control" name="BookPrice" required> </div> <div class="form-group"> <label>Book information</label> <input type="text" class="form-control" name="BookInfo" required> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </div> </body> </html> showbook.jsp <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Book display</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>Book list - displays all books</small> </h1> <a href="/book/toAddBook">Add books</a> <form action="/book/queryBookByName"> <input type="text" property="Query books" name="BookName" required/> <input type="submit" value="query"/> <scan style="color: red">${error}</scan> </form> <a href="/book/show">Show all books</a> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-bordered"> <thead> <tr> <th scope="col">Book number</th> <th scope="col">Book name</th> <th scope="col">Book price</th> <th scope="col">Book information</th> <th scope="col">operation</th> </tr> </thead> <tbody> <c:forEach var="book" items="${list}"> <tr> <td>${book.bookID}</td> <td>${book.bookName}</td> <td>${book.bookPrice}</td> <td>${book.bookInfo}</td> <td> <a href="/book/toUpdatePage?id=${book.bookID}">modify</a>  |  <a href="/book/deleteBook/${book.bookID}">delete</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </body> </html>
updatebook.jsp
<%-- Created by IntelliJ IDEA. User: DELL Date: 2021/9/7 Time: 17:15 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> <title>modify</title> </head> <body> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>Revise books</small> </h1> <form action="/book/UpdateBook" method="get"> <input type="hidden" name="BookID" value="${books.bookID}"/> <div class="form-group"> <label>Book name</label> <input type="text" class="form-control" name="BookName" value="${books.bookName}" required> </div> <div class="form-group"> <label>Book price</label> <input type="text" class="form-control" name="BookPrice" value="${books.bookPrice}" required> </div> <div class="form-group"> <label>Book information</label> <input type="text" class="form-control" name="BookInfo" value="${books.bookInfo}" required> </div> <button type="submit" class="btn btn-default">modify</button> </form> </div>A </div> </div> </body> </html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>home page</title> <style> h3{ width: 300px; height: 38px; margin: 200px auto; text-align: center; background: deepskyblue; line-height: 58px; border-radius: 5px; } a{ text-decoration: none; font: 28px black; } </style> </head> <body> <h3><a href="${pageContext.request.contextPath}/book/show">Jump to book page</a></h3> </body> </html>