SpringBoot实战

在官网https://start.spring.io/生成spring boot的模板

在idea导入下载的springboot模版
在这里插入图片描述

在这里插入图片描述

实战

  1. 创建Controller
    在这里插入图片描述
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {

    @RequestMapping(value = "/index")
    @ResponseBody
    public Map<String,String> Index() {
        Map map = new HashMap<String, String>();
        map.put("北京市","北方城市");
        map.put("深圳市","北方城市");
        return map;
    }
}
  1. 添加注解@ComponentScan

在这里插入图片描述

package com.example.emergency;

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

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.controller"})

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

}

  1. pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>emergency-system-api</name>
	<description>emergency system api</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
<!--			<exclusions>-->
<!--				<exclusion>-->
<!--					<groupId>org.junit.vintage</groupId>-->
<!--					<artifactId>junit-vintage-engine</artifactId>-->
<!--				</exclusion>-->
<!--			</exclusions>-->
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<pluginRepositories>
		<pluginRepository>
			<id>aliyun-repos</id>
			<name>Aliyun Repository</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>
结果

通过运行 localhost:8080/index
在这里插入图片描述

记录一些报错
  • Error:(3, 29) java: 程序包org.junit.jupiter.api不存在
    删除pom.xml中的
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
tests.java下
//import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.Test;

接入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>
			<scope>runtime</scope>
		</dependency>
创建数据库

在这里插入图片描述

创建Ontology.java
public class Ontology {
    private String id;
    private String name;
    private String onto_version;
    private String onto_desc;
    private String type;
    private String create_by;
    private Date create_date;
    private String update_by;
    private Date update_date;
    ...相应的getter()setter()...
}
创建OntologyMapper.java
package com.example.dao;

import com.example.pojo.Ontology;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OntologyMapper {
    @Select("SELECT * FROM ON_ONTOLOGY WHERE ID=#{id}")
    List<Ontology> getOntology(String id);
}

创建OntologyService.java
package com.example.service;

import com.example.pojo.Ontology;

import java.util.List;

public interface OntologyService {
    List<Ontology> getOntology(String id);
}
创建OntologyServiceImpl.java
package com.example.service;

import com.example.pojo.Ontology;
import com.example.dao.OntologyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OntologyServiceImpl implements OntologyService{
    @Autowired
    OntologyMapper ontologyMapper;

    @Override
    public List<Ontology> getOntology(String id) {
        return ontologyMapper.getOntology(id);
    }
}
controller添加API方法
package com.example.controller;

import com.example.pojo.Ontology;
import com.example.service.OntologyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {
    @Autowired
    OntologyService ontologyService;
    @GetMapping("/show")
    public List<Ontology> getOntology(String id) {
        return ontologyService.getOntology(id);
    }


    @RequestMapping(value = "/index")
    @ResponseBody
    public Map<String,String> Index() {
        Map map = new HashMap<String, String>();
        map.put("北京市","北方城市");
        map.put("深圳市","北方城市");
        return map;
    }
}

修改EmergencySystemApplication.java
package com.example.emergency;

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

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.controller","com.example.service"})
@MapperScan(basePackages = {"com.example.dao"})

public class EmergencySystemApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(EmergencySystemApiApplication.class, args);
	}
}
数据库配置

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
运行结果

访问http://localhost:8080/show?id=1
在这里插入图片描述

多表查询

@Mapper
public interface ProcessMapper {
    @Select("SELECT * FROM ON_PROCESS")
    @Results({
            @Result(property = "ontology",column = "onto_id", one=@One(select="com.example.dao.OntologyMapper.getOntologyById"))
    })
    List<Process> getProcess();
}

property表示process类中的ontology字段,column表示用process中的onto_id做getOntologyById中的参数id

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值