❝
This article has been included at github.com/likekk/stud… Welcome to star, 😊😊😊 learn together and make progress together. If there are any mistakes in the article, please point them out. The route of front-end learning and resource sharing will be planned on GitHub in the later stage.
❞
Writing in the front
Each article hope you have a harvest, each article hope you can calm down to browse, reading. Each article is the author’s carefully polished work.
If you think Yang JIAN this small white still have some things, Yang JIAN hope is looking at the article you can help light that point praise button, for Yang jian this warm male, “really really very important”, this will be my continued writing power.
The body of the
After learning MyBatis,Spring,SpringMVC, what we need to do is to connect these three. Spring implements business object management,SpringMVC is responsible for request forwarding and view management, and MyBatis serves as data object persistence engine. The advantages of this combination are: lightweight, high degree of freedom, and better fit between Spring and Spring MVC. Complete the INTEGRATION of SSM framework through a library management example.
The project build
The parent module
1. We open idea, select Create New Project, and then select Maven project. We don’t select Maven skeleton for the time being, and select Maven skeleton for our view layer (Web-UI layer)
2. Enter the organization name and module name
- Groupld: “Organization name”
- ArtifactId: “Module name”
- Version: Version number
In the Maven world, each project has its own unique organization name, module name, and version, which are the coordinates of the Maven project. A Maven project can be packaged as a JAR, war, POM, and so on, but they all have these three coordinates. The JAR package we imported during the project was uniquely identified by the above coordinate values. Therefore, when we create a Maven project, we are required to specify the specific organization name, module name, version, and packaging method.
Submodules (Common Layer)
1. Create the submodule Common layer and click Next
Submodule (Dao layer)
Since the previous steps are the same, I will not repeat the above steps and directly complete the last step
Submodule (Service layer)
Entity Layer Submodule
The entity layer and the Mmon layer are the same operation steps, we will not take screenshots here, the operation steps are the same
Submodule (Web-UI layer)
1. The sub-module Web-UI layer needs to be mentioned. Since our layer is the view layer, we need to select maven skeleton.
2. Then click Next, enter the module name and finish. Here we need to select our local central repository. Since then our multi-module project has been built
3. The complete project structure is as follows:
4. Here we found that the webApp folder was added to the view layer. In the future development process, our views will be placed in the WEB_INF directory
- Common module: a generic module that does not depend on any other modules, mainly utils, and can be used in multiple different projects
- Entitie module: POJO, VO, DTO
- Dao module: Data persistence, access database, here using Mybatis
- Service module: a business module that processes business logic
- Webui module: a B/S presentation layer used to display data in the browser and provide user interfaces
Module is dependent on
Parent module dependency
1. Next, we go to the parent module’s POM. XML file for unified module management. In order to facilitate version management and reduce the complexity of adding dependencies, we only manage the dependencies in the parent module pom. XML file without any dependencies.
The parent module pom.xml file
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
The < modelVersion > 4.0.0 < / modelVersion > <groupId>com.ssm.booksystem</groupId> <artifactId>BookSystem</artifactId> <packaging>pom</packaging> < version > 1.0 - the SNAPSHOT < / version > <modules> <module>Book-Common</module> <module>Book-Dao</module> <module>Book-Service</module> <module>Book-WEBUI</module> <module>Book-Entity</module> </modules> <properties> <! -- Module internal dependencies -->< the Book - Common version > 1.0 - the SNAPSHOT < / Book - Common. Version >< the Book - Dao. Version > 1.0 - the SNAPSHOT < / Book - Dao. Version >< the Book - Service version > 1.0 - the SNAPSHOT < / Book - Service. Version >< the Book - the Entity version > 1.0 - the SNAPSHOT < / Book - the Entity. The version >< the Book - WEBUI. Version > 1.0 - the SNAPSHOT < / Book - WEBUI. Version ><! --Common module management -->< Jackson version > 2.7.4 < / Jackson version >< Commons - lang3 version > 3.4 < / Commons - lang3. Version ><! -- General section -->< log4j version > 2.6.1 < / log4j version >< junit version > 4.12 < / junit version >< spring - the test version > 4.3.18. RELEASE < / spring - test. Version ><! --Spring-->< spring version > 4.3.0. RELEASE < / spring. Version >< aspectjweaver version > 1.8.9 < / aspectjweaver version >< additional version > 3.2.4 < / additional version ><! --mybatis-->< mybatis - spring. Version > 1.3.0 < / mybatis - spring. Version >< mysql connector - Java version > 5.1.38 < / mysql connector - Java version >< mybatis version > 3.4.1 track < / mybatis version >< c3p0 version > 0.9.1.2 < / c3p0 version ><! --spring mvc-->< JSTL version > 1.2 < / JSTL. Version >< servlet - API version > 4.0.1 < / servlet - API. Version >< JSP - API version > 2.1 < / JSP - API. Version >< hibernate version > 5.2.2. The Final < / hibernate. Version >< Commons - IO. Version > 2.4 < / Commons - IO. Version >< Commons fileupload - version > 1.3.1 < / Commons fileupload - version >< freemarker version > 2.3.23 < / freemarker version > </properties> <! -- Unified management of module dependencies --> <dependencyManagement> <dependencies> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Common</artifactId> <version>${Book-Common.version}</version> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Dao</artifactId> <version>${Book-Dao.version}</version> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Service</artifactId> <version>${Book-Service.version}</version> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Entity</artifactId> <version>${Book-Entity.version}</version> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-WEBUI</artifactId> <version>${Book-WEBUI.version}</version> </dependency> <! - common module -- -- ><! --jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <! --apache-commons--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <! --log4j log package --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> </dependency> <! -- JUnit Unit Test Tool --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-test.version}</version> </dependency> <! Spring Framework Core Library --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <! AspectJ AOP weaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <! -- Mybatis -- Spring adapter --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <! Spring Java database access package, which in this case is used to provide the data source <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <! Mysql database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <! Mybatis ORM framework --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <! -- C3P0 connection pool --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <! -- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <! -- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <! -- Servlet Core package --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> </dependency> <! --JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> </dependency> <! --JSR303 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.version}</version> </dependency> <! -- File upload --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <! -- FreeMarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> </dependencies> </dependencyManagement> </project> Copy the code
Submodule Comon layer module dependency
Pom.xml file in the common module
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>BookSystem</artifactId> <groupId>com.ssm.booksystem</groupId> < version > 1.0 - the SNAPSHOT < / version > </parent> The < modelVersion > 4.0.0 < / modelVersion > <artifactId>Book-Common</artifactId> <dependencies> <! --jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <! --apache-commons--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project> Copy the code
Submodule Dao layer module dependency
XML file in Dao module, we need to rely on the common layer and entity layer in this layer, because in this layer we are dealing with the database, one table for each POJO
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>BookSystem</artifactId> <groupId>com.ssm.booksystem</groupId> < version > 1.0 - the SNAPSHOT < / version > </parent> The < modelVersion > 4.0.0 < / modelVersion > <artifactId>Book-Dao</artifactId> <dependencies> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Common</artifactId> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Entity</artifactId> </dependency> <! --log4j log package --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </dependency> <! -- JUnit Unit Test Tool --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <! Spring Framework Core Library --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <! AspectJ AOP weaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> </dependency> <! -- Mybatis -- Spring adapter --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <! Spring Java database access package, which in this case is used to provide the data source <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <! Mysql database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <! Mybatis ORM framework --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <! -- C3P0 connection pool --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> </dependency> </dependencies> </project> Copy the code
Submodule Service layer module dependency
The pom.xml file in the Service module, where we need to rely on the DAO layer
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>BookSystem</artifactId> <groupId>com.ssm.booksystem</groupId> < version > 1.0 - the SNAPSHOT < / version > </parent> The < modelVersion > 4.0.0 < / modelVersion > <artifactId>Book-Service</artifactId> <dependencies> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Dao</artifactId> </dependency> </dependencies> </project> Copy the code
Submodule Entity layer module dependency
The Entity layer does not need to rely on any module for the time being, the Pom.xml file in the Entity module
<? xml version="1.0" encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>BookSystem</artifactId> <groupId>com.ssm.booksystem</groupId> < version > 1.0 - the SNAPSHOT < / version > </parent> The < modelVersion > 4.0.0 < / modelVersion > <artifactId>Book-Entity</artifactId> </project> Copy the code
Submodule Web-UI layer module dependencies
The pom.xml file in the Service module, where we need to rely on the Service layer
<? xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>BookSystem</artifactId> <groupId>com.ssm.booksystem</groupId> < version > 1.0 - the SNAPSHOT < / version > </parent> The < modelVersion > 4.0.0 < / modelVersion > <artifactId>Book-WEBUI</artifactId> <packaging>war</packaging> <name>Book-WEBUI Maven Webapp</name> <! -- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> Piler < maven.com. Source > 1.7 < / maven.com piler. Source >< maven.com piler target > 1.7 < / maven.com piler. Target > </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> < version > 4.11 < / version > <scope>test</scope> </dependency> <dependency> <groupId>com.ssm.booksystem</groupId> <artifactId>Book-Service</artifactId> </dependency> <! -- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <! -- Servlet Core package --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <! -- File upload --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <finalName>Book-WEBUI</finalName> <pluginManagement><! -- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> The < version > 3.1.0 < / version > </plugin> <! -- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> The < version > 3.0.2 < / version > </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> The < version > 3.8.0 < / version > </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> The < version > 2.22.1 < / version > </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> The < version > 3.2.2 < / version > </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> The < version > 2.5.2 < / version > </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> The < version > 2.8.2 < / version > </plugin> </plugins> </pluginManagement> </build> </project> Copy the code
The final dependency effect
Database connection
1. This is navicat’s interface. I won’t go into too much detail about the connection configuration.
2. Right-click localHost_3306 and select Create database, enter the name of the data, select the second to last character set (utF-8) and confirm.
3. Database scripts
create table book(
bid int auto_increment primary key not null COMMENT'Book Number'. bname varchar(50) not null COMMENT'Book Name'. bauthor VARCHAR(50) COMMENT'Book author'
)
INSERT into book(bname,bauthor)VALUES ('Douro Continent'.'The Tang Family three Shao'), ('Three days to see'.Helen Keller), ('Fight through the sky'.'Silkworm potato'), (Snow Eagle Lord.'I eat tomatoes') SELECT * from book Copy the code
Configuration file
1. First we remove the SRC directory of the parent module. In fact, it doesn’t matter whether we remove it or not.
2. We need to add the corresponding package name and config file submodule common layer to each submodule
“R.java“
package com.booksystem.common;
import java.util.HashMap;
import java.util.Map;
/ * ** Returns data encapsulation* /public class R extends HashMap<String, Object> { private static final long serialVersionUID = 1L; public R() { put("code", 1); put("msg"."success"); } / / error public static R error() { return error(500, "Unknown exception, please contact administrator."); } public static R error(String msg) { return error(500, msg); } public static R error(int code, String msg) { R r = new R(); r.put("code", code); r.put("msg", msg); return r; } / / success public static R ok(String msg) { R r = new R(); r.put("msg", msg); return r; } public static R ok(Map<String, Object> map) { R r = new R(); r.putAll(map); return r; } public static R ok() { return new R(); } public static R ok(Object data) { return new R().put("data",data); } @Override public R put(String key, Object value) { super.put(key, value); return this; } } Copy the code
Submodule DAO layer
BookMapper.xml
<? xml version="1.0" encoding="UTF-8"? ><! DOCTYPE mapper PUBLIC"- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<! The namespace should be the package name of the corresponding interface + the interface name.<mapper namespace="com.booksystem.dao.BookDao">
<! -- Query all books --> <select id="getAllBooks" resultMap="bookMap"> select bid,bname,bauthor from book </select> <! SQL > configure the mapping between table and entity <resultMap id="bookMap" type="book"> <id column="bid" property="bid"/> <result property="bname" column="bname"/> <result column="bauthor" property="bauthor"/> </resultMap> </mapper> Copy the code
applicationContext.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:p="http://www.springframework.org/schema/p"
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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <! --1 Import properties file, use placeholder in configuration --> <context:property-placeholder location="classpath*:db.properties" /> <! C3P0 --> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <! Driver class name --> <property name="driverClass" value="${mysql.driver}" /> <! -- url --> <property name="jdbcUrl" value="${mysql.url}" /> <! -- User name --> <property name="user" value="${mysql.uid}" /> <! - password - > <property name="password" value="${mysql.password}" /> <! The number of simultaneous connections c3P0 gets when the pool runs out --> <property name="acquireIncrement" value="${mysql.acquireIncrement}"></property> <! Initial connection pool size --> <property name="initialPoolSize" value="${mysql.initialPoolSize}"></property> <! -- Minimum number of connections in the pool --> <property name="minPoolSize" value="${mysql.minPoolSize}"></property> <! -- Maximum number of connections in the pool --> <property name="maxPoolSize" value="${mysql.maxPoolSize}"></property> </bean> <! SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <! Mybatis core configuration file path --> <property name="configLocation" value="classpath:mybatis.xml"></property> <! -- Data source --> <property name="dataSource" ref="datasource"/> <! SQL map file path [mapper path] --> <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"></property> </bean> <! --4 Automatic object relational mapping --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <! -- Specifies the session factory. This property can be omitted if only one is defined in the current context. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <! -- Specify the base package of the interface to be automatically scanned, implement interface --> <property name="basePackage" value="com.booksystem.dao"/> </bean> <! --5 Declarative transaction Management --><! -- Define transaction manager, which spring manages transactions --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <! Support annotation-driven transaction management, specify transaction manager --> <tx:annotation-driven transaction-manager="transactionManager" /> <! --6 Containers automatically scan IOC components --> <context:component-scan base-package="com.booksystem.dao"/> <! Aspectj supports automatic proxy implementation of AOP functionality <aop:aspectj-autoproxy/> </beans> Copy the code
db.properties
##mysql connection string
# driver
mysql.driver=com.mysql.jdbc.Driver
# connect string
mysql.url=jdbc:mysql://localhost:3306/booksystem? useUnicode=true&characterEncoding=UTF-8
# username mysql.uid=root # your password mysql.password=123456 mysql.acquireIncrement=5 mysql.initialPoolSize=10 mysql.minPoolSize=5 mysql.maxPoolSize=20 Copy the code
mybatis.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>
<properties resource="db.properties"></properties>
<settings>
<! -- Specify mybatis to use logging component --> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <! -- Set alias --> <typeAliases> <package name="com.booksystem.entity"/> </typeAliases> </configuration> Copy the code
BookDao,java
package com.booksystem.dao;
import com.booksystem.entity.Book;
import java.util.List;
public interface BookDao { // Query all book information public List<Book> getAllBooks(); } Copy the code
Entity layer book.java submodule
package com.booksystem.entity;
/ ** Book entity class* * /public class Book {
private long bid; // Book numberprivate String bname; // Book nameprivate String bauthor; // Book author public long getBid() { return bid; } public void setBid(long bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getBauthor() { return bauthor; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } // no argument constructor public Book() {}// parameter constructor public Book(long bid, String bname, String bauthor) { this.bid = bid; this.bname = bname; this.bauthor = bauthor; } @Override public String toString() { return "Book{" + "bid=" + bid + ", bname='" + bname + '\''+ ", bauthor='" + bauthor + '\'' + '} '; } } Copy the code
We need to test how much we’ve written to reduce errors. Right now we’re just doing an exercise, and the code is relatively small, but when it comes to real projects it will be a lot more. To get into the habit, I usually test when I write to the Dao layer.
BookTest.java
package com.booksystem.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; // Specify the bean injection configuration file@ContextConfiguration("/applicationContext.xml") // Use standard junit@RunWith(SpringJUnit4ClassRunner.class) Transactional // Transaction management@Rollback(true) // Whether to roll backpublic class BookDaoTest { @Autowired private BookDao bookDao; @Test public void getAllBooks() { System.out.println(bookDao.getAllBooks()); } } Copy the code
Submodule Service layer
BookService
package com.booksystem.service;
import com.booksystem.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List; public interface BookService { // Query all book information public List<Book> getAllBooks(); } Copy the code
BookImple
package com.booksystem.imple;
import com.booksystem.dao.BookDao;
import com.booksystem.entity.Book;
import com.booksystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookImple implements BookService { @Autowired public BookDao bookDao; public List<Book> getAllBooks() { return bookDao.getAllBooks(); } } Copy the code
We found that there was no file to store the source code in this layer, so we need to add it by ourselves, and select File and Project Structure
The generated directory
BookController.java
package com.book.controller;
import com.booksystem.common.R;
import com.booksystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/book") public class BookController { @Autowired public BookService bookService; @GetMapping("/getAllBook") @ResponseBody public R getAllBook() { return R.ok(bookService.getAllBooks()); } } Copy the code
springmvc-servlet.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: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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <! -- Automatic package scanning, implementing annotation-enabled IOC <context:component-scan base-package="com.booksystem" /> <! Spring MVC doesn't handle static resources --> <mvc:default-servlet-handler /> <! Support MVC annotation-driven --> <mvc:annotation-driven enable-matrix-variables="true" /> <! -- Configure policy mapping media type --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="removeSemicolonContent" value="false" /> </bean> <! Internal View parser, JSP and JSTL template <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <! -- Specify the view rendering class --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <! -- Prefix automatically added to path --> <property name="prefix" value="/WEB-INF/views/" /> <! -- Suffix automatically added to path --> <property name="suffix" value=".html" /> <! Set the content type of all views. If the view itself is set to the content type, the view class can be ignored. <property name="contentType" value="text/html; charset=UTF-8" /> <! -- Priority, the smaller the more advanced --> <property name="order" value="1" /> </bean> <! -- File upload resolver --><! Spring MVC does not recognize multipart file contents by default. <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> <mvc:cors> <mvc:mapping path="/ * *" allowed-origins="*" allowed-methods="POST,GET, OPTIONS,DELETE,PUT" allowed-headers="Content-Type,ContentType,Access-Control-Allow-Headers, Authorization, X-Requested-With" allow-credentials="true"/> </mvc:cors> </beans> Copy the code
web.xml
<? xml version="1.0" encoding="UTF-8"? ><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<welcome-file-list> <! Welcome page --> <welcome-file>index.html</welcome-file> </welcome-file-list> <listener> <description>Spring container load listener </description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> </description> Sets the configuration file location for Spring loading. The default location is in web-INF /lib. </description> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <! --Spring MVC front-end Servlet, central controller --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <! --Spring MVC configuration file path --> <param-value>classpath*:springmvc-servlet.xml</param-value> </init-param> <! -- Start priority, the smaller the earlier the load --> <load-on-startup>1</load-on-startup> <! File upload configuration --> <multipart-config> <! -- Maximum file upload limit 5MB --> <max-file-size>5242880</max-file-size> <! -- Request Max 20MB --> <max-request-size>20971520</max-request-size> <! -- When the file size exceeds the threshold, it will be written to disk --> <file-size-threshold>0</file-size-threshold> </multipart-config> </servlet> <! -- Path map for Servlet access, all access must go through pre-control for scheduling <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <! -- Encoding filter --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <! -- Path mapping --> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> Copy the code
3. Configure tomcat
4. Project deployment
5. Start the project
index.html
<! DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title> Library Management system </title> <style> h1{ text-align: center; } #myTab{ width: 800px; margin: 0 auto; } </style> </head> <body> <h1> Library Management System </h1><table id="myTab" cellpadding="0" cellspacing="0" border="1"> <tr> Number < td > < / td >The name of the < td > < / td >Author < td > < / td > </tr> </table> <script src="Js/jquery - 1.11.3. Min. Js." "></script> <script> $.ajax({ url:'/book/getAllBook'. type:'get'. dataType:'json'. }).done(function (data) { if(data.code===1){ $("#myTab tr:not(:eq(0))").remove(); for(var i=0; i<data.data.length; i++){ var tr="<tr>"; tr+="<td>"+(i+1)+"</td>"; tr+="<td>"+data.data[i].bname+"</td>"; tr+="<td>"+data.data[i].bauthor+"</td>"; tr+="</tr>" $("#myTab").append(tr); } } }) </script> </body> </html> Copy the code
At the end
If you find this article useful, could you please help to turn on the like button? .
For Yang Jian, this warm man: “really really very useful”, your support will be my motivation to continue to write the article forward, we will see the next article.
““Original” | er lang shen Yang Jian“
❝
Erlang God Yang Jian, a programmer in the front of the Internet to scrape a living, focus on the front development, good at technology sharing. If you need to reprint, please contact the author or retain the original link, wechat public search Erlang God Yang Jian or scan the two-dimensional code below more convenient.
Come together to witness the growth of Erlang God Yang Jian! More good articles, technology to share in the public account below. Welcome to follow.
❞