[note] spring MVC + mybatis build Web development environment

Keywords: PHP Mybatis Spring Maven xml

After using the habit of jFinal, spring forgot...

Take another note here

The project structure is as follows

First of all, I used maven of IDEA to process mybatis used by the database framework here. The database is mariadb, and I used FASTJSON when I used json to transfer data to the front desk.

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>demo</groupId>
    <artifactId>demo6</artifactId>
    <version>1.0-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>

    <build>
        <defaultGoal>compile</defaultGoal>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
    </build>

    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.36</version>
        </dependency>
    </dependencies>
</project>

Then the name of the spring configuration file applicationContext.xml can be changed randomly as long as it is consistent with the web.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="cn.erika.*"/>
    <mvc:annotation-driven/>

    <bean id="dataConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:datasource.properties"/>
    </bean>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}">
        </property>
        <property name="url" value="${url}">
        </property>
        <property name="username" value="${username}">
        </property>
        <property name="password" value="${password}">
        </property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- My mybatis Just configure it mapper It is recommended to write the location of the file separately if other configurations are needed. mybatis The configuration file of is referenced here -->
        <property name="mapperLocations" value="classpath:cn/erika/mapper/*.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="close">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>
</beans>

Obviously, you can write the configuration file of the data source by yourself. The configuration of the properties I used, that is, the k-v format, is omitted here.

Next, configure web.xml to add the following content, which is the same as the general selvet.

<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:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The environment is finished here, and then the test data.

The code of User.java will not be let go, boring code

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.erika.mapper.UserMapper">
    <resultMap id="UserEntry" type="cn.erika.model.User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="password" column="password"/>
        <result property="type" column="type"/>
        <result property="lastLogin" column="last_login"/>
        <result property="desc" column="desc"/>
    </resultMap>

    <select id="getAll" resultMap="UserEntry">
        SELECT * FROM tb_user
    </select>
</mapper>

UserEntry.java is nothing but to reduce the amount of code

UserService.java

package cn.erika.service;

import cn.erika.mapper.UserMapper;
import cn.erika.model.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserService {
    private UserMapper mapper;

    @Autowired
    public UserService(@Qualifier("sqlSession") SqlSession session) {
        this.mapper = session.getMapper(UserMapper.class);
    }

    public List<User> getAll() {
        return mapper.getAll();
    }
}

DemoController.java

package cn.erika.controller;

import cn.erika.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class DemoController{

    @Autowired
    private UserService service;

    @RequestMapping("getAll")
    @ResponseBody
    public String getAll(){
        String json = JSON.toJSONString(service.getAll());
        return json;
    }
}

Later, I couldn't get up when I was running, and then I found that it was a deployment problem.

Throw all the LIBS in and it's all right... I was so depressed that Baidu didn't find out the reason for half a day.

Posted by PHP_PhREEEk on Thu, 17 Oct 2019 14:36:31 -0700