配置过程参考:
项目创建:http://how2j.cn/k/springboot/springboot-eclipse/1640.html
集成Mybatis使用Oracle:https://www.cnblogs.com/pangkang/p/8296666.html
1、创建MAVEN项目
1-1、菜单 -> File -> New -> Other -> Maven -> Maven -> Maven Project -> New Maven Project
groupId: com.公司名
artifactId: 项目名
name: 项目名
description: 项目名
1-2、配置pom.xml(这里附上本次项目完整的pom.xml)
注1:https://mvnrepository.com/ 下可查找需要的依赖
注2:仓库未提供ojdbc下载,自行下载ojdbc6.jar(https://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html)。
进入ojdbc6.jar所在目录,cmd执行mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=下载的版本号 -Dpackaging=jar -Dfile=ojdbc6.jar
(参考http://www.cnblogs.com/pangkang/p/8250014.html)
注3:经指教下面这一段可自动下载ojdbc:<dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version></dependency>
注4:若不使用数据库连接,要去掉Mybatis的依赖

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.hello</groupId> 8 <artifactId>springboot</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <name>springboot</name> 11 <description>springboot</description> 12 <packaging>war</packaging> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.9.RELEASE</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-tomcat</artifactId> 28 29 </dependency> 30 <dependency> 31 <groupId>junit</groupId> 32 <artifactId>junit</artifactId> 33 <scope>test</scope> 34 </dependency> 35 36 <!-- servlet依赖. --> 37 <dependency> 38 <groupId>javax.servlet</groupId> 39 <artifactId>javax.servlet-api</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>javax.servlet</groupId> 43 <artifactId>jstl</artifactId> 44 </dependency> 45 46 <!-- tomcat的支持. --> 47 <dependency> 48 <groupId>org.apache.tomcat.embed</groupId> 49 <artifactId>tomcat-embed-jasper</artifactId> 50 </dependency> 51 52 <!-- 支持热部署 --> 53 <dependency> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-devtools</artifactId> 56 <optional>true</optional> <!-- 这个需要为 true 热部署才有效 --> 57 </dependency> 58 59 <!-- Swagger2依赖 --> 60 <dependency> 61 <groupId>io.springfox</groupId> 62 <artifactId>springfox-swagger2</artifactId> 63 <version>2.6.1</version> 64 </dependency> 65 <dependency> 66 <groupId>io.springfox</groupId> 67 <artifactId>springfox-swagger-ui</artifactId> 68 <version>2.6.1</version> 69 </dependency> 70 71 <!-- Mybatis --> 72 <dependency> 73 <groupId>org.mybatis.spring.boot</groupId> 74 <artifactId>mybatis-spring-boot-starter</artifactId> 75 <version>1.1.1</version> 76 </dependency> 77 78 <!-- oracle --> 79 <dependency> 80 <groupId>com.oracle</groupId> 81 <artifactId>ojdbc6</artifactId> 82 <version>11.2.0.4.0</version> 83 </dependency> 84 </dependencies> 85 86 <properties> 87 <java.version>1.8</java.version> 88 </properties> 89 90 <build> 91 <plugins> 92 <plugin> 93 <groupId>org.springframework.boot</groupId> 94 <artifactId>spring-boot-maven-plugin</artifactId> 95 </plugin> 96 </plugins> 97 </build> 98 99 </project>
1-3、创建包并创建Application.java主方法
运行这个方法就会启动tomcat,默认端口是8080

1 package com.hello.springboot; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 import springfox.documentation.swagger2.annotations.EnableSwagger2; 8 9 @SpringBootApplication // 表示这是一个SpringBoot应用 10 @EnableSwagger2 // 配置扫描mapper 11 @MapperScan("com.hello.springboot.Dao") // 配置扫描mapper 修改项目后注意检查扫描地址 12 public class Application { 13 14 public static void main(String[] args) { 15 SpringApplication.run(Application.class, args); 16 } 17 18 }
1-4、创建控制器包并创建控制器类

1 package com.hello.springboot.controller; 2 3 import java.util.ArrayList; 4 5 import javax.annotation.Resource; 6 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.hello.springboot.Entyties.HelloEntity; 12 import com.hello.springboot.Service.ControllerService; 13 14 @RestController 15 public class HelloController { 16 @Resource 17 private ControllerService service; 18 19 @RequestMapping(value = "/hello", method = RequestMethod.GET) 20 // @ResponseBody:以JSON格式返回到前端 21 @ResponseBody 22 public ArrayList<HelloEntity> GetHelloEntities() { 23 return service.GetHelloEntities();// 访问数据库查询内容到前端 24 } 25 }
2、集成使用Mybatis、Oracle
2-1、在src/main/resources 目录下增加 application.properties文件。用于视图重定向jsp文件的位置,配置数据库地址及账号密码,指定sql语句位置

1 spring.mvc.view.prefix=/WEB-INF/jsp/ 2 spring.mvc.view.suffix=.jsp 3 4 spring.datasource.url = jdbc:oracle:thin:@HOST:POST:数据库名 5 spring.datasource.username = admin 6 spring.datasource.password = 123456 7 8 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver 9 10 mybatis.mapperLocations=classpath:mapper/*.xml
2-2、创建Service包并创建Service类

1 package com.hello.springboot.Service; 2 3 import java.util.ArrayList; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.hello.springboot.Dao.HelloDao; 9 import com.hello.springboot.Entyties.HelloEntity; 10 11 @Service 12 public class ControllerService { 13 @Autowired 14 private HelloDao dao; 15 16 public ArrayList<HelloEntity> GetHelloEntities() { 17 return dao.GetHello(); 18 } 19 }
2-3、创建Dao包并创建Dao类

1 package com.hello.springboot.Dao; 2 3 import java.util.ArrayList; 4 5 import com.hello.springboot.Entyties.HelloEntity; 6 7 public interface HelloDao { 8 public ArrayList<HelloEntity> GetHello(); 9 }
2-4、创建Entyties包并创建Entyties类

1 package com.hello.springboot.Entyties; 2 3 import java.io.Serializable; 4 5 public class HelloEntity implements Serializable { 6 private static final long serialVersionUID = -6556793741331167103L; 7 private String str; 8 9 public String getstr() { 10 return str; 11 } 12 13 public void setstr(String str) { 14 this.str = str; 15 } 16 17 }
2-5、在src/main/resources下创建mapper,mapper下创建sql.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.hello.spring.Dao.HelloDao"> 4 5 <!-- 修改项目后,注意检查namespace、resultType --> 6 7 <select id="GetHello" resultType="com.hello.spring.Entyties.HelloEntity"> 8 SELECT 'Hello Spingboot' str FROM dual 9 </select> 10 </mapper>