springboot整合mybatis连接oracle
Posted chong-zuo3322
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合mybatis连接oracle相关的知识,希望对你有一定的参考价值。
pom.xml:
<!-- 链接:https://pan.baidu.com/s/1agHs5vWeXf90r3OEeVGniw 提取码:wsgm --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc8</artifactId> <version>12.2.0.1</version> </dependency> <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 (c) 2020, All Rights Reserved. * */ package com.demo.server.mybatis; import java.io.Serializable; /** * 此处应有类说明<br/> * * @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 (c) 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; /** * 此处应有类说明<br/> * * @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 (c) 2020, All Rights Reserved. * */ package com.demo.server.mybatis; import org.apache.ibatis.annotations.Mapper; /** * 此处应有类说明<br/> * * @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"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.demo.server.mybatis.UserMapper"> <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> </mapper>
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 (c) 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; /** * * 此处应有类说明<br/> * * @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整合成功
以上是关于springboot整合mybatis连接oracle的主要内容,如果未能解决你的问题,请参考以下文章
springboot-2-整合myBatis及druid连接池
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合MyBatis,HiKariDruid连接池的使用