Springboot build web project and integrate mybatis

1. Create a parent project to manage the versions of dependencies common to all modules

<?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>org.zh.springboot</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>cache</module>
        <module>doc</module>
    </modules>
    <properties>
        <spring.boot.version>2.0.9.RELEASE</spring.boot.version>
        <lomck.version>1.16.16</lomck.version>
        <logger.version>1.3.8.RELEASE</logger.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lomck.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j</artifactId>
                <version>${logger.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2. Create a cache web module for developing web applications using springboot and introduce related dependencies

<?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>parent</artifactId>
        <groupId>org.zh.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cache</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.zh.springboot</groupId>
            <artifactId>doc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
    </dependencies>

</project>

3. Create a startup class and add @SpringBootApplication annotation to the startup class:

@SpringBootConfiguration继承自@Configuration,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。
@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。在下面博客会具体分析这个注解,快速入门的demo实际没有用到该注解。
@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring容器管理。
@SpringBootApplication
public class Application {
    public static  void main(String[] args){
        SpringApplication.run(Application.class);
    }
}

4. Create a controller for testing

package org.zh.springboot.cache.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zh.springboot.cache.entity.Product;
import org.zh.springboot.cache.entity.ResultDto;
import org.zh.springboot.cache.service.ProductService;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductCotroller {
    @Autowired
    private ProductService productService;
    private static Logger logger = LoggerFactory.getLogger(ProductCotroller.class);

    @RequestMapping("/queryAll")
    public ResultDto<List<Product>> queryAllProduct(){
        logger.info("ProductCotroller queryAllProduct start");
        ResultDto<List<Product>> result = new ResultDto();
        try{
            List<Product> productList = productService.queryAllProduct();
            result.setSuccess(true);
            result.setData(productList);
            result.setResponseTime(new Date());
        } catch (Exception e){
           result.setSuccess(false);
           result.setResponseTime(new Date());
           result.setMsg("查询商品失败");
        }
        logger.info("ProductCotroller queryAllProduct end");
        return result;
    }

}

4. Test the function of springboot's mvc: the results are as follows:

5. Integrate mybatis

 1. Add the following dependencies to the cache module

  <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

2. Create a mapper interface

package org.zh.springboot.cache.mapper;

import org.springframework.stereotype.Repository;
import org.zh.springboot.cache.entity.Product;

import java.util.List;

@Repository
public interface ProductMapper{
    List<Product> queryAll();
}

3. Create mapper xml definition

<?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="org.zh.springboot.cache.mapper.ProductMapper">

    <resultMap type="org.zh.springboot.cache.entity.Product" id="productMapper">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="desc" property="desc" />
        <result column="price" property="price" />
        <result column="type" property="type" />
        <result column="img" property="img" />
        <result column="status" property="status" />
    </resultMap>

    <select id="queryAll" resultMap="productMapper">
        select * from t_product
    </select>

</mapper>

4. Configure database connection and mybatis related configuration

#数据库链接的设置
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

#mybatis 的设置
#mybatis mapper xml 文件位置
mybatis.mapper-locations=classpath:mapper/*.xml

5. Customize the scan path of the mapper and add to the startup class:

@MapperScan(value="org.zh.springboot.cache.mapper")

6. Start the startup class

7. The test results are as follows:

 

 

Guess you like

Origin blog.csdn.net/zh127368zh/article/details/90603157