Create a Maven project

Import dependencies

< properties > < spring. Version > 5.2.12. RELEASE < / spring. Version > < slf4j. Version > 1.6.6 < / slf4j version > < log4j version > 1.2.12 < / log4j version > <. Mysql version > 5.1.6 < / mysql version > < mybatis. Version > 3.5.0 < / mybatis version > </properties> <dependencies> <! -- spring --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> < version > 1.6.8 < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope> Provided </scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId> JSTL </groupId> <artifactId> JSTL </artifactId> <version>1.2</version> </dependency> <! -- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <! -- log end --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> < artifactId > mybatis - spring < / artifactId > < version > 2.0.6 < / version > < / dependency > < the dependency > < the groupId > com. McHange < / groupId > < artifactId > c3p0 < / artifactId > < version > 0.9.5.2 < / version > < scope > compile < / scope > </dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <! -- lomlok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit. Jupiter </groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies>Copy the code

Create the directory structure and corresponding files as shown in the figure

4. Display the contents of each configuration file

4.1 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" 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/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"> <context:component-scan base-package="com.ljy"/> <! -- Enable annotation scan, Manage Services and DAOs --> <context:component-scan base-package="com.ljy.mapper"> </context:component-scan> <context:component-scan base-package="com.ljy.service"> </context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"/> <! - configure the connection pool - > < bean id = "dataSource" class = "boPooledDataSource com.mchange.v2.c3p0.Com" > < property name = "driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}"  /> <property name="password" value="${jdbc.password}" /> </bean> <! - the management to the IOC SqlSessionFactory - > < bean id = "SqlSessionFactory" class = "org. Mybatis. Spring. SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <! - scan mapper interfaces - > < bean id = "mapperScanner" class = "org. Mybatis. Spring. Mapper. MapperScannerConfigurer" > < property name="basePackage" value="com.ljy.mapper"/> </bean> <! Configure Spring's declarative transaction management --> <! - configuration transaction manager - > < bean id = "transactionManager" class = ". Org. Springframework. JDBC datasource. DataSourceTransactionManager "> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>Copy the code

JDBC 4.2. The properties

jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/lvjianyou? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=rootCopy the code

4.3 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.ljy.pojo"/> </typeAliases> <mappers> <package name="com.ljy.mapper"/> </mappers> </configuration>Copy the code

4.4 for springmvc. XML

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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 "> <! <context:component-scan base-package="com.ljy.controller"> </context:component-scan> <! - the parser configuration view - > < bean id = "viewResolver" class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > <! <property name="prefix" value="/WEB-INF/" /> <! - the file suffix - > < property name = "suffix" value = "JSP" / > < / bean > <! < MVC :resources location="/ CSS /" mapping="/ CSS /**" /> < MVC :resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/static/" mapping="/static/**" /> <! -- Enable support for SpringMVC annotations --> < MVC :annotation-driven /> </beans>Copy the code

4.5 web. XML

<? The XML version = "1.0" encoding = "utf-8"? > <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" Version = "3.1" > <! <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <! Listener - configuration - > < listener > < listener - class > org. Springframework. Web. Context. ContextLoaderListener < / listener - class > </listener> <servlet> <servlet-name>dispatcherServlet</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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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> </web-app>Copy the code

Instructions before test

Configuration files and dependencies, we have now completed, the next step is certainly to write the code, and then we SSM three layers run together, see if the environment has been successfully set up, so we can create a database table, and then corresponding write controller, service, mapper layer, and then test oh!

Let’s start with the database table

create table user( id int(5) primary key, name varchar(20), age int(2), description varchar(100) ) default charset=utf8; Insert into user values(1,' 1 ',21,' 1 '); Insert into user values(1, 2, 1); Insert into user values(3,' ultraman ',56,' not bad '); Insert into user values(4,' dika ',42,' not bad '); Insert into user values(5,' Jackie ',66,' nice ');Copy the code

Entity class User

@Data
public class User implements Serializable {
  private int id;
  private String name;
  private int age;
  private String description;

}
Copy the code

mapper

@Repository
public interface UserMapper {

  @Select("select * from user")
  List<User> findAll();
}
Copy the code

controller

@RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/findAll") public String getAll(){ List<User> all = userMapper.findAll(); return all.get(0).getName(); }}Copy the code

service? Dependency injection means that the Spring environment is fine!

Try a JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <html> <body> <h2>Hello World! </h2> <a href="/test/test1"> Click on me to test! </a> <a href="/user/findAll"> </a> </body> </html>Copy the code

Configure our Tom cat

Ok, now run directly!! One unit

Vi. Operation results

Click in, query success!

At this point, call it a day

7. To summarize

7.1 Version Problems

If mybatis does not match the Spring version, or because other JAR versions do not match, it is possible to generate various errors, this should be paid attention to.

7.2 Mapper File and XML Cannot be bound Successfully

This is because Maven does not compile XML files by default. Add the following configuration to POM.xml to enable Maven to compile XML files

<build>
<resources>
  <resource>
    <directory>src/main/java</directory>    
    <includes>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
</resources>
</build>
Copy the code

7.3 JDBc.properties data cannot be read in ApplicationContext. XML

Before:

<context:property-placeholder location="classpath:/jdbc.properties"/>
Copy the code

After:

<context:property-placeholder location="classpath:/jdbc.properties" ignore-unresolvable="true"/>
Copy the code

7.4 If an error occurs during the Operation

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!

Change the dirver in jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
Copy the code