项目结构
因为Oracle授权问题,maven不能直接添加ojdbc依赖,lib里面的ojdbc8.jar(提取码:k3gm)
然后右键选择Add as Library即可
Maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
UserController.java
package com.daniel.controller;
import com.daniel.mapper.UserMapper;
import com.daniel.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.List;
/**
* @Author Daniel
* @Description Controller
**/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/getAllUser")
public String getUsers() {
List<User> allUser = userMapper.findAllUser();
if (allUser != null) {
return this.toJson(allUser);
}
return "error";
}
public static String toJson(Object obj) {
return JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue);
}
}
UserMapper.java
package com.daniel.mapper;
import com.daniel.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @Author Daniel
* @Description Mapper
**/
@Mapper
public interface UserMapper {
List<User> findAllUser();
}
User.java
package com.daniel.pojo;
import java.io.Serializable;
/**
* @Author Daniel
* @Description Mapper
**/
/*序列化就是对实例对象的状态(State 对象属性而不包括对象方法)进行通用编码(如格式化的字节码)并保存,以保证对象的完整性和可传递性。
简而言之:序列化,就是为了在不同时间或不同平台的JVM之间共享实例对象
*/
public class User implements Serializable {
private int id;
private String userName;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
Application.java
package com.daniel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author Daniel
* @Description Application启动类
**/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplication(Application.class).run(args);
}
}
UserMapper.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">
<!--UserMapper的全类名-->
<mapper namespace="com.daniel.mapper.UserMapper">
<!--返回数据集,column为数据库的名字,jdbcType为数据库的类型,property为pojo的名字-->
<resultMap id="resultMap" type="com.daniel.pojo.User">
<result column="id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
</resultMap>
<!--id要对应上,注意这里不要带分号-->
<select id="findAllUser" resultMap="resultMap">
select *
from my_user
</select>
</mapper>
application.yml
server:
port: 8080
session-timeout: 0
servlet:
context-path: /
spring:
datasource:
url: jdbc:oracle:thin:@localhost:1521:orcl
username: scott
password: root
driver-class-name: oracle.jdbc.OracleDriver
max-idle: 10
max-wait: 10000
min-idle: 5
initial-size: 5
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.daniel.mapper
logging:
level:
com.daniel.mybatis: debug
user.sql
CREATE TABLE my_user (
id INTEGER,
user_name VARCHAR2(200),
password VARCHAR2(200)
);![在这里插入图片描述](https://img-blog.csdnimg.cn/20200923195446880.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E4MDU4MTQwNzc=,size_16,color_FFFFFF,t_70#pic_center)
INSERT INTO my_user (id,user_name,password) VALUES (1,'Daniel','123456');
INSERT INTO my_user (id,user_name,password) VALUES (3,'Tyler','777777');
INSERT INTO my_user (id,user_name,password) VALUES (2,'Sophia','666666');
INSERT INTO my_user (id,user_name,password) VALUES (3,'Tyler','777777');
在Application中启动项目后访问http://localhost:8080/getAllUser
出现数即整合成功!!!