1.创建一个parent 工程,用于管理各模块共同的依赖的版本
<?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.创建一个cache 的web 模块,用于使用springboot 开发web 应用,并引入相关依赖
<?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.创建启动类,并给启动类添加@SpringBootApplication注解:
@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.创建一个controller 用于测试
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.测试 springboot 的mvc 的功能:结果如下:
5.整合mybatis
1.在cache 模块中加入如下依赖
<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.创建mapper接口
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.创建mapper 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">
<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.配置数据库连接和mybatis 相关配置
#数据库链接的设置
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.自定义mapper 的扫描路径,在启动类上加入:
@MapperScan(value="org.zh.springboot.cache.mapper")
6.启动启动类
7.测试结果如下: