Spingboot+Mybatis+Oracle項目配置


配置過程參考:

項目創建: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>
View Code

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 }
View Code

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 }
View Code

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
View Code

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 }
View Code

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 }
View Code

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 }
View Code

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>
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM