Java学习之SpringBoot+SSM+Oracle

整合SSM框架+Oracle数据库

运行环境:Java8、Maven

技术:SpringBoot、SSM、Thymeleaf、Oracle

1、项目结构

 2、项目代码

启动类:MyoracleApplication

 1 package com.ssm.myoracle;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 @MapperScan("com.ssm.myoracle.mapper")
 9 public class MyoracleApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(MyoracleApplication.class, args);
13     }
14 
15 }
View Code

Bean:People

 1 package com.ssm.myoracle.bean;
 2 
 3 
 4 /**
 5  * @Classname People
 6  * @Description TODO
 7  * @Date 2021/8/27 9:13
 8  * @Created by JH
 9  */
10 public class People {
11     private String id;
12     private String userName;
13     private String passWord;
14 
15     public People() {
16     }
17 
18     public People(String id, String userName, String passWord) {
19         this.id = id;
20         this.userName = userName;
21         this.passWord = passWord;
22     }
23 
24     public String getId() {
25         return id;
26     }
27 
28     public void setId(String id) {
29         this.id = id;
30     }
31 
32     public String getUserName() {
33         return userName;
34     }
35 
36     public void setUserName(String userName) {
37         this.userName = userName;
38     }
39 
40     public String getPassWord() {
41         return passWord;
42     }
43 
44     public void setPassWord(String passWord) {
45         this.passWord = passWord;
46     }
47 
48     @Override
49     public String toString() {
50         return "People{" +
51                 "id='" + id + '\'' +
52                 ", userName='" + userName + '\'' +
53                 ", passWord='" + passWord + '\'' +
54                 '}';
55     }
56 }
View Code

Controller:BaseController(用于第一次进入页面)、FormController

 1 package com.ssm.myoracle.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 /**
 7  * @Classname BaseController
 8  * @Description TODO
 9  * @Date 2021/8/26 18:59
10  * @Created by bernard
11  */
12 @Controller
13 public class BaseController {
14     @RequestMapping("/")
15     public String index() {
16         return "insert";
17     }
18 }
View Code
 1 package com.ssm.myoracle.controller;
 2 
 3 import com.ssm.myoracle.bean.People;
 4 import com.ssm.myoracle.service.FormService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import java.util.List;
12 import java.util.Map;
13 
14 /**
15  * @Classname FormController
16  * @Description TODO
17  * @Date 2021/8/26 11:02
18  * @Created by bernard
19  */
20 @Controller
21 @RequestMapping("/oracle")
22 public class FormController {
23     @Autowired
24     private FormService formService;
25 
26     @RequestMapping("/insert")
27     public String insertDate(HttpServletRequest request) {
28         String id = request.getParameter("id");
29         String userName = request.getParameter("userName");
30         String passWord = request.getParameter("passWord");
31         int flag = formService.insertDate(new People(id, userName, passWord));
32         System.out.println(flag);
33         return "find";
34     }
35 
36     @RequestMapping("/find")
37     public String findDate(Model model) {
38         List<Map<String, Object>> list = formService.findData();
39         System.out.println(list);
40         model.addAttribute("list", list);
41         return "show";
42     }
43 }
View Code

Service

 1 package com.ssm.myoracle.service;
 2 
 3 import com.ssm.myoracle.bean.People;
 4 
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 /**
 9  * @Classname FormService
10  * @Description TODO
11  * @Date 2021/8/26 11:02
12  * @Created by bernard
13  */
14 public interface FormService {
15     int insertDate(People people);
16 
17     List<Map<String, Object>> findData();
18 }
View Code
 1 package com.ssm.myoracle.service.impl;
 2 
 3 import com.ssm.myoracle.bean.People;
 4 import com.ssm.myoracle.mapper.FormMapper;
 5 import com.ssm.myoracle.service.FormService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 import java.util.Map;
11 
12 /**
13  * @Classname FormServiceImpl
14  * @Description TODO
15  * @Date 2021/8/26 11:03
16  * @Created by bernard
17  */
18 @Service
19 public class FormServiceImpl implements FormService {
20     @Autowired
21     private FormMapper formMapper;
22 
23     @Override
24     public int insertDate(People people) {
25         int flag = formMapper.insertDate(people);
26         return flag;
27     }
28 
29     @Override
30     public List<Map<String, Object>> findData() {
31         List<Map<String, Object>> list = formMapper.findData();
32         return list;
33     }
34 }
View Code

Mapper

 1 package com.ssm.myoracle.mapper;
 2 
 3 import com.ssm.myoracle.bean.People;
 4 import org.apache.ibatis.annotations.Mapper;
 5 
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 /**
10  * @Classname FormMapper
11  * @Description TODO
12  * @Date 2021/8/26 10:58
13  * @Created by bernard
14  */
15 @Mapper
16 public interface FormMapper {
17     int insertDate(People people);
18 
19     List<Map<String, Object>> findData();
20 }
View Code

配置文件:application.yml(如果是远程则把“localhost”换成IP)

 1 #端口号
 2 server:
 3   port: 10010
 4 #数据库
 5 spring:
 6   datasource:
 7     platform: oracle
 8     driver-class-name: oracle.jdbc.driver.OracleDriver
 9     url: jdbc:oracle:thin:@locahost:1521:orcl
10     username: root
11     password: root123
12 #thymeleaf
13   thymeleaf:
14     enabled: true  #开启thymeleaf视图解析
15     encoding: utf-8  #编码
16     prefix: classpath:/templates/  #前缀
17     cache: false  #是否使用缓存
18     mode: HTML  #严格的HTML语法模式
19     suffix: .html  #后缀名
20 #sqlmapper映射文件位置
21 mybatis:
22   mapper-locations: classpath:mapper/*Mapper.xml      #所有的mapper映射文件
23   type-aliases-package: com.ssm.myoracle.bean    #定义所有操作类的别名所在包
View Code

Mapper.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.ssm.myoracle.mapper.FormMapper">
 4 
 5     <!--  Sql不要加“;”  -->
 6     <select id="findData" resultType="java.util.HashMap">
 7     select * from tbl_form_people
 8     </select>
 9 
10     <insert id="insertDate" parameterType="com.ssm.myoracle.bean.People">
11         insert into
12         tbl_form_people
13         values (
14         #{id},
15         #{userName},
16         #{passWord}
17         )
18     </insert>
19 </mapper>
View Code

3、静态页面

find.html

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>查询</title>
 6 </head>
 7 <body>
 8 <h1>数据添加成功</h1>
 9 <form action="/oracle/find">
10     <button type="submit"> 点击查询数据</button>
11 </form>
12 </body>
13 </html>
View Code

insert.html

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>新增</title>
 6 </head>
 7 <body>
 8 <form action="/oracle/insert">
 9     ID: <input type="text" name="id" value=""/><br/>
10     账号: <input type="text" name="userName" value=""/><br/>
11     密码: <input type="text" name="passWord" value=""/><br/>
12     <button type="submit">提交</button>
13 </form>
14 </body>
15 </html>
View Code

show.html

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>展示</title>
 6 </head>
 7 <body>
 8 <h1>展示数据</h1>
 9 <table th:each="people:${list}">
10     <tr>
11         <td th:text="${people.id}"></td>
12         <td th:text="${people.userName}"></td>
13         <td th:text="${people.passWord}"></td>
14     </tr>
15 </table>
16 </body>
17 </html>
View Code

4、依赖文件:pom.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5     <parent>
  6         <groupId>org.springframework.boot</groupId>
  7         <artifactId>spring-boot-starter-parent</artifactId>
  8         <version>2.5.4</version>
  9         <relativePath/> <!-- lookup parent from repository -->
 10     </parent>
 11     <groupId>com.ssm</groupId>
 12     <artifactId>myoracle</artifactId>
 13     <version>0.0.1-SNAPSHOT</version>
 14     <name>myoracle</name>
 15     <description>Demo project for Spring Boot</description>
 16     <properties>
 17         <java.version>1.8</java.version>
 18     </properties>
 19     <dependencies>
 20         <!--  支持web  -->
 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-devtools</artifactId>
 28             <scope>runtime</scope>
 29             <optional>true</optional>
 30         </dependency>
 31         <!--  Oracle数据库连接  -->
 32         <dependency>
 33             <groupId>com.oracle.database.jdbc</groupId>
 34             <artifactId>ojdbc8</artifactId>
 35             <scope>runtime</scope>
 36         </dependency>
 37         <!--  oracle字符类型转换  -->
 38         <dependency>
 39             <groupId>cn.easyproject</groupId>
 40             <artifactId>orai18n</artifactId>
 41             <version>12.1.0.2.0</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>mysql</groupId>
 45             <artifactId>mysql-connector-java</artifactId>
 46             <scope>runtime</scope>
 47         </dependency>
 48         <!--  thymeleaf  -->
 49         <dependency>
 50             <groupId>org.springframework.boot</groupId>
 51             <artifactId>spring-boot-starter-thymeleaf</artifactId>
 52             <version>2.5.4</version>
 53         </dependency>
 54         <!--  工具类  -->
 55         <dependency>
 56             <groupId>org.projectlombok</groupId>
 57             <artifactId>lombok</artifactId>
 58             <optional>true</optional>
 59         </dependency>
 60         <dependency>
 61             <groupId>org.springframework.boot</groupId>
 62             <artifactId>spring-boot-starter-test</artifactId>
 63             <scope>test</scope>
 64         </dependency>
 65         <!--  mybatis  -->
 66         <dependency>
 67             <groupId>org.mybatis.spring.boot</groupId>
 68             <artifactId>mybatis-spring-boot-starter</artifactId>
 69             <version>1.3.2</version>
 70         </dependency>
 71         <dependency>
 72             <groupId>org.mybatis</groupId>
 73             <artifactId>mybatis-spring</artifactId>
 74             <version>2.0.6</version>
 75         </dependency>
 76         <dependency>
 77             <groupId>org.mybatis</groupId>
 78             <artifactId>mybatis</artifactId>
 79             <version>3.5.7</version>
 80         </dependency>
 81     </dependencies>
 82 
 83     <build>
 84         <plugins>
 85             <plugin>
 86                 <groupId>org.springframework.boot</groupId>
 87                 <artifactId>spring-boot-maven-plugin</artifactId>
 88                 <configuration>
 89                     <excludes>
 90                         <exclude>
 91                             <groupId>org.projectlombok</groupId>
 92                             <artifactId>lombok</artifactId>
 93                         </exclude>
 94                     </excludes>
 95                 </configuration>
 96             </plugin>
 97         </plugins>
 98     </build>
 99 
100 </project>
View Code

5、成果展示

1、访问路径(配置文件中将端口号改为了 10010)

http://localhost:10010/

经过BaseController进入了insert.html

 2、添加数据后点击提交,经过FormController进入find.html

 

 3、点击查询数据,经过FormController跳到show.html

补充:

1、mapper中的Sql不要加“;”,不然可能会报错;

2、依赖中引入”orai18n“,防止报编码的错误:

不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK

        <dependency>
            <groupId>cn.easyproject</groupId>
            <artifactId>orai18n</artifactId>
            <version>12.1.0.2.0</version>
        </dependency>

3、字段名在数据库中默认转成了大写,所以页面中调用也要大写;

 

 

posted @ 2021-08-27 10:43  请别耽误我写BUG  阅读(137)  评论(0编辑  收藏  举报