在官网https://start.spring.io/生成spring boot的模板
在idea导入下载的springboot模版
实战
- 创建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;
}
}
- 添加注解@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);
}
}
- 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