springboot整合mybatis连接oracle
pom.xml:
复制代码
com.oracle
ojdbc8
12.2.0.1
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>orai18n</artifactId>
<version>12.1.0.2.0</version>
</dependency>
复制代码
User:
复制代码
/**
- Copyright © 2020, All Rights Reserved.
*/
package com.demo.server.mybatis;
import java.io.Serializable;
/**
- 此处应有类说明
- @author chong.zuo
- @Date 2020年2月22日 下午7:40:28
- @since 1.0.0
/
public class User implements Serializable{
/*
* serialVersionUID:
*/
private static final long serialVersionUID = 1L;
private int id;
private String username;
private String password;
private String danWeiTID;
@Override
public String toString() {
return "User [danWeiTID=" + danWeiTID + ", id=" + id + ", password="
+ password + ", username=" + username + "]";
}
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;
}
public String getDanWeiTID() {
return danWeiTID;
}
public void setDanWeiTID(String danWeiTID) {
this.danWeiTID = danWeiTID;
}
}
复制代码
UserController:
复制代码
/**
- Copyright © 2020, All Rights Reserved.
*/
package com.demo.server.mybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.demo.server.utils.JsonUtil;
/**
- 此处应有类说明
- @author chong.zuo
- @Date 2020年2月22日 下午7:38:32
- @since 1.0.0
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/getAllUser")
public String getUsers() {
User users = userMapper.findAllUser();
if(users != null) {
return JsonUtil.toJson(users);
}
return "err";
}
}
复制代码
UserMapper:
复制代码
/**
- Copyright © 2020, All Rights Reserved.
*/
package com.demo.server.mybatis;
import org.apache.ibatis.annotations.Mapper;
/**
- 此处应有类说明
- @author chong.zuo
- @Date 2020年2月22日 下午7:40:01
- @since 1.0.0
/
@Mapper //添加此注解,便可以被扫描到
public interface UserMapper {
/*
* 返回所有用户列表
* @return
*/
public User findAllUser();
}
复制代码
UserMapper.xml:
复制代码
<?xml version="1.0" encoding="UTF-8"?><resultMap id="resultMap" type="com.demo.server.mybatis.User">
<result column="id" jdbcType="VARCHAR" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="username"/>
<result column="pass_word" jdbcType="VARCHAR" property="password"/>
<result column="danweit_id" jdbcType="VARCHAR" property="danWeiTID"/>
</resultMap>
<select id="findAllUser" resultMap="resultMap">
select * from sys_user
</select>
复制代码 application.yml:
复制代码
server:
port: 7070
session-timeout: 0
context-path: /
spring:
datasource:
url: jdbc:oracle:thin:@192.168.5.105:1521:orcl
username: tom
password: tom123
driver-class-name: oracle.jdbc.driver.OracleDriver
max-idle: 10
max-wait: 10000
min-idle: 5
initial-size: 5
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.demo.server.mybatis
复制代码
建表sql:
复制代码
– Create table
create table SYS_USER
(
id VARCHAR2(20) not null,
user_name VARCHAR2(20) not null,
pass_word VARCHAR2(20) not null,
danweit_id VARCHAR2(20)
)
tablespace TOMSPACE
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
– Add comments to the table
comment on table SYS_USER
is ‘用户表’;
复制代码
Application:
复制代码
/**
- Copyright © 2020, All Rights Reserved.
*/
package com.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
- 此处应有类说明
- @author chong.zuo
- @Date 2020年2月20日 下午10:07:09
- @since 1.0.0
*/
@SpringBootApplication
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.setWebEnvironment(true);
app.run(args);
LOG.info("**************** Startup Success ****************");
}
}
复制代码
springboot与mybatis整合成功