I’ll write down all the steps. A bad pen is better than a good memory. It doesn’t feel anything just by looking at it. Let’s do it manually. cheer up

1. Create projects

Projects are created in maven’s most basic way

Modify to a Web project [pom.xml]

packaging

<! <packaging> War </packaging>Copy the code

Project code

<! - set the encoding - > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < / properties >Copy the code

Dependent libraries

It depends on the situation

<! --> <dependencies> <! -- Servlet + JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope> Provided </scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp- API </artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <! -- Log --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> The < version > 1.2.3 < / version > < / dependency > <! -- Spring + SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 5.2.8. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Aspectj < / groupId > < artifactId > aspectjweaver < / artifactId > < version > 1.9.6 < / version > < / dependency > < the dependency > <groupId>org.aspectj</groupId> <artifactId> aspectJrt </artifactId> <version>1.9.6</version> </dependency> <dependency> < the groupId > com. Fasterxml. Jackson. Core < / groupId > < artifactId > Jackson - databind < / artifactId > < version > 2.11.0 < / version > </dependency> <! -- MyBatis --> <dependency> <groupId>org. MyBatis </artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> < version > at 2.0.5 < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < < artifactId > spring - JDBC/artifactId > < version > 5.2.8. RELEASE < / version > < / dependency > < the dependency > < the groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.49 < / version > < / dependency > <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.22</version> </dependency> <! -- dependency> <groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>Copy the code

Project structure core configuration file [web.xml]

War is already a Web project because of the packaging method configured in POM.xml

Press the shortcut key to enter the project configuration

The initial interface

Add web.xml (change path oh)

src/main/webapp

Create a directory

The effect

2. Start the web

Create index. The JSP

Create index.jsp under webApp

Configuration tomact

Configure the Application Context

Enable bug debug

3. Create a service

Create a domain

Skill class

private Integer id;
private Date createdTime;
private String name;
private Integer level;
Copy the code

Create a dao layer

Create interface interface because use mybatis

boolean save(Skill skill);
boolean update(Skill skill);
boolean remove(Integer id);
@Select("SELECT * FROM skill")
List<Skill> list();
@Select("SELECT * FROM skill WHERE id = #{id}")
Skill get(Integer id);
Copy the code

Create the mappers folder under Resources to create the file skill.xml

<? The 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" > < mapper namespace="com.mj.dao.SkillDao"> <insert id="save" parameterType="com.mj.domain.Skill"> INSERT INTO skill(name, level) VALUES (#{name}, #{level}) </insert> <update id="update" parameterType="com.mj.domain.Skill"> UPDATE skill SET name = #{name}, level = #{level} WHERE id = #{id} </update> <delete id="remove" parameterType="int"> DELETE FROM skill WHERE id = #{id} </delete> </mapper>Copy the code

Create the applicationContext. XML

The mybatis SqlSessionFatoryBean

  • Introduces a configuration file for database drivers
# jdbc
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xr
jdbc.username=root
jdbc.password=root
Copy the code
  • The applicationcontext.xml added
<! - data source (Druid) -- - > < context: the property - placeholder location = "classpath: main. The properties" / > < bean id = "dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mappers/*.xml"/> </bean> <bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.mj.dao"/> </bean>Copy the code

Test Tests a wave

4. Return json

Start with the service and Controller layers

Remember to inject it

Create a dispatcherServlet. The XML

Remember to scan the custom package in applicationContext.xml to add

<context:component-scan base-package="com.mj.service"/>
Copy the code

The dispatcherServlet. XML is added

<context:component-scan base-package="com.mj.controller"/>
Copy the code

Web.xml configures DispatcherServlet (parent-child container)

<servlet> <servlet-name>DispatcherServlet</servlet-name> <! / web-inf /${servlet-name}-servlet.xml--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! > <init-param> <param-name>contextConfigLocation</param-name> <! - children - > < param - value > classpath: dispatcherServlet. XML value > < param - < / init - param > <! Create a servlet as soon as the project is deployed to the Web. --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <! - intercept all requests - > < url - the pattern > / < / url - the pattern > < / servlet - mapping > <! - the parent container - > < listener > < listener - class > org. Springframework. Web. Context. ContextLoaderListener < / listener - class > < / listener >  <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>Copy the code

The ContextLoaderListener is used to automatically assemble configuration information for Applicationcontenxt.xml when the Web container is started. Because it implements the ServletContextLister interface, when the web.xml listener is configured, the methods it implements are executed by default when the container is started.

Web. XML configures garbled POST requests

<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>
</filter>

<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Copy the code

Dispatcherservlet. XML Configures garbled response data

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
        </bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
Copy the code

MyBatis supplement

If the Mapper file is placed in the same directory as dao and the file name is the same as DAO

  • You can find the Mapper file through MapperScannerConfigurer
  • You no longer need to configure the mapperLocations property of SqlSessionFactoryBean

Solution: Keep the names the same

Create 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> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>Copy the code

Configuration configLocation

<! -- <property name="mapperLocations" value="classpath:mappers/*.xml"/>--> <property name="configLocation" value="classpath:mybatis-config.xml"/>Copy the code

Maven Supplement (with Mabatis supplement)

By default, Maven does not package configuration files in the source folder so it does

  • Problem with Dao layer XML in the same folder as Dao layer
<build> <! --> <resources> <! <resource> <directory> SRC /main/resources</directory> </resource> <! <resource> <directory> SRC /main/ Java </directory> <includes> <include>**/*. Properties </include> <include>**/*.xml</include> </includes> </resource> </resources> </build>Copy the code

To solve the problem

Attributes in XML report red

The general druid properties also have problems with the solution is

This does not solve the problem, I will create a new project can be and prompt is not the network problem