1 在https://start.spring.io/网站上创建Spring Boot项目
引入Spring Web、MyBatis、Oracle Driver依赖
或者在STS中创建Spring Start Project项目(和网站创建类似)
2 在STS中导入创建好的Spring Boot项目
3 配置src/main/resource下的application.properties属性文件
#tomcat
server.port=8080
server.servlet.context-path=/springboot02
#dataSource
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.password=123456
spring.datasource.username=scott
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#mybatis
mybatis.type-aliases-package=com.yf.springboot02.domain
mybatis.mapper-locations=classpath:mybatis/mapper/*Mapper.xml
设置Tomcat,端口好为8080,当前项目的虚拟路径为springboot02
设置数据源,url、password、username、driver-class-name
设置mybatis,为domain实体类包配置类别名,设置mapper映射文件路径
4 创建service包、mapper包、domain包、controller包
注意这些包是com.yf.springboot02的子包
Dept.java
package com.yf.springboot02.domain;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept() {
super();
}
public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
DeptMapper.java
注意Mapper接口需要加上@Mapper注解
package com.yf.springboot02.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.yf.springboot02.domain.Dept;
@Mapper
public interface DeptMapper {
public List<Dept> queryAllDept();
}
DeptService.java
package com.yf.springboot02.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yf.springboot02.domain.Dept;
import com.yf.springboot02.mapper.DeptMapper;
@Service
public class DeptService {
@Autowired
private DeptMapper deptMapper;
public List<Dept> listDepts(){
return deptMapper.queryAllDept();
}
}
DeptController.java
package com.yf.springboot02.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yf.springboot02.domain.Dept;
import com.yf.springboot02.service.DeptService;
@Controller
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("queryAllDept")
@ResponseBody
public List<Dept> queryAllDept() {
System.out.println("queryAllDept");
return deptService.listDepts();
}
}
DeptMapper.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="com.yf.springboot02.mapper.DeptMapper">
<select id="queryAllDept" resultType="dept">
select deptno,dname,loc from dept
</select>
</mapper>
5 启动Spring Boot项目
@SpringBootApplication
public class Springboot02Application {
public static void main(String[] args) {
SpringApplication.run(Springboot02Application.class, args);
}
}
浏览器访问localhost:8080/springboot02/queryAllDept
结果返回JSON
6 补充
测试项目时发现,使用com.oracle.ojdbc的时后
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
需要加上以下依赖否则会报异常
<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/orai18n -->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>orai18n</artifactId>
</dependency>