springboot 使用

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

 

一. 创建springboot工程

File --> New

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

编辑包名版本号等信息:

Next,选择web模板:

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

生成工程目录如下:

 

二. 创建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错误。

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

重新运行:

 

三. 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映射类

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,显示正常:

 

参考链接:

https://www.cnblogs.com/wmyskxz/p/9010832.html

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值