spring 功能非常强大,已经是java web开发标配,但是配置较为复杂繁琐。直到spring boot的出现,彻底改变这种现状。基于spring boot可以用极简的配置,极少的代码,快速开发web应用。

 

一. 创建springboot工程

File --> New

springboot 使用_mysql

注:Idea comunity 没有 spring Initializr 组件,需使用Idea Ultimate 版本。

编辑包名版本号等信息:

springboot 使用_springboot_02

Next,选择web模板:

springboot 使用_spring_03

下一步点击完成后,IDEA 需要一段时间从maven仓库下载依赖库。

生成工程目录如下:

springboot 使用_spring_04

 

二. 创建controller

创建HelloController控制器,添加@RestController  @RequestMapping 注解,代码十分简洁。

package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}

启动DemoApplication,请求:localhost:8080/hello 一直显示404错误。

springboot 使用_mysql_05

解决方案:出现这个错误一般是Controller没有注册,spring入口类 DemoApplication 默认只加载同目录的类,添加注解@ComponentScan 指定com.example.controller 包名。

重新运行:

springboot 使用_mysql_06

springboot 使用_mysql_07

 

三. mybatis 集成 

3.1 添加mysql 驱动

pom.xml

<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

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

3.2 配置mysql连接

application.yml

spring:
datasource:
url: jdbc:mysql://localhost:3306/test?charaterEncoding=UTF-8
username: root
password: test
driver-class-name: com.mysql.jdbc.Driver

jpa:
hibernate:
ddl-auto: update

3.3 添加Tbservices实体类和Tbservices映射类

springboot 使用_java_08

TbServiceMapper 映射类:

package com.example.mapper;

import com.example.bean.Tbservices;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TbservicesMapper {

// 优雅的mybatis
@Select("select * from tb_services")
List<Tbservices> findAll();
}

Tbservices 控制器:

package com.example.controller;

import com.example.bean.Tbservices;
import com.example.mapper.TbservicesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class HelloController {
@Autowired
TbservicesMapper tMapper;

@RequestMapping(value = "/show", method = RequestMethod.GET)
public String show() {
StringBuffer sb = new StringBuffer();

// 查询db
List<Tbservices> services = tMapper.findAll();

for (Tbservices srv : services) {
sb.append("id: ").append(srv.getId()).append("\t");
sb.append("key: ").append(srv.getKey()).append("\t");
sb.append("name: ").append(srv.getName()).append("\t");
sb.append("pid: ").append("<br/>");
}

return sb.toString();
}
}

运行springboot服务,控制台报错:

Field tMapper in com.example.controller.HelloController required a bean of type 'com.example.mapper.TbservicesMapper' that could not be found.

解决方案:spring找不到Mapper,需要添加@MapperScan 注解指定映射器目录:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.example.controller", "com.example.bean"})
@MapperScan("com.example.mapper")
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

重新启动springboot,显示正常:

springboot 使用_spring_09