在引入oracle 依赖包ojdbc
com.oracle
ojdbc6
11.2.0.1.0
其他依赖 即包含 spring boot 、mybatis、freemarker 即可
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
org.springframework.boot
spring-boot-devtools
runtime
true
在引入ojbc包报错,依赖包中显示红色波浪线,由于本实例已经处理,红色波浪线不再有
解决办法,在原来下载的maven仓库中删除oracle 依赖文件 如下图
这里maven 仓库 是工程中设置的,如果没有特别设置 ,默认为
C:\Users\username.m2\repository;
否则就是在下面目录中
单独下载oracle 依赖包,如果本地安装了oracle数据库,可以到
oracle_home/jdbc/lib 中找到
然后 打开 dos 并切切换到maven 配置路径下 执行
mvn install:install-file -DgroupId=com.Oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=D:\apache-maven-3.5.3-bin\apache-maven-3.5.3\mvnrepository\com\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar
只需将之前mysql的配置中 url、驱动、用户、密码 修改即可
spring:
# freemarker 配置
freemarker:
template-loader-path: classpath:/templates/view/
cache: false
charset: utf-8
check-template-location: true
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
request-context-attribute: request
suffix: .ftl
# oracle 数据源
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: scott
password: 123456
driverClassName: oracle.jdbc.driver.OracleDriver
mybatis:
mapper-locations: classpath:mapper/*.xml
文件路径放在 templates\view 下,在 templates下新建路径 view ,然后新建ftl文件 showAllEmps.ftl
hello world! this is spring boot + mybatis + freemarker + oracle
<#list emps as emp>
${emp.empno} ${emp.ename} ${(emp.job)!} ${(emp.sal)!}
#list>
create table EMP
(
empno NUMBER(4) not null,
ename VARCHAR2(10) not null,
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
)
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'CURREY', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975.00, null, 20);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250.00, 1400.00, 30);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850.00, null, 30);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, to_date('09-06-1981', 'dd-mm-yyyy'), 2450.00, null, 10);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000.00, null, 10);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500.00, 0.00, 30);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100.00, null, 20);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950.00, null, 30);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000.00, null, 20);
insert into emp_b (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300.00, null, 10);
新建 包 emp.dao ,emp.dto,emp.service,emp.serviceimpl,以及controller 结构如下
创建 EmpDto
package com.example.app.emp.dto;
public class EmpDto {
private String empno;
private String ename;
private String job;
private String mgr;
private Double sal;
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMgr() {
return mgr;
}
public void setMgr(String mgr) {
this.mgr = mgr;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
创建 接口 EmpDao
package com.example.app.emp.dao;
import com.example.app.emp.dto.EmpDto;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface EmpDao {
public List findAllEmp ();
}
创建接口 EmpService
package com.example.app.emp.service;
import com.example.app.emp.dto.EmpDto;
import java.util.List;
public interface EmpService {
public List findAllEmp ();
}
创建EmpService实现类 EmpServiceImpl
package com.example.app.emp.service.impl;
import com.example.app.emp.dao.EmpDao;
import com.example.app.emp.dto.EmpDto;
import com.example.app.emp.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "empService")
public class EmpServicceImpl implements EmpService{
@Autowired
private EmpDao empDao;
@Override
public List findAllEmp() {
return empDao.findAllEmp();
}
}
创建 EmpController
package com.example.app.controller;
import com.example.app.emp.dto.EmpDto;
import com.example.app.emp.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Map;
@Controller
public class EmpController {
@Autowired
private EmpService empService;
@RequestMapping("/showAllEmps")
public String showAllEmps(Mapmap){
Listemps=empService.findAllEmp();
map.put("emps",emps);
return "showAllEmps";
}
}
AppApplication 代码如下
package com.example.app;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.app.emp.dao")
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}
代码结构如下
在resource 目录下 新建mapper目录,再新建EmpMapper 配置文件
http://127.0.0.1:8080/showAllEmps
spring boot整合oracle主要难点在于引入oracle 依赖包ojdbc。