• spring boot 集成mybatis连接oracle数据库


    1. POM文件添加依赖

    <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!-- oracle -->
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0.1.0</version>
            </dependency>

    oracle驱动需要手动添加依赖

    2.controller

    @Resource
        private ControllerService service;
        @RequestMapping(value="/GetIndoorCheckItemEntities",method=RequestMethod.GET)
        @ApiOperation("获取数据库内容")
        public ArrayList<IndoorCheckItemEntity> GetIndoorCheckItemEntities(){
            return service.GetIndoorCheckItemEntities();
        }

    3. service

    @Service
    public class ControllerService   {
    
        @Autowired
        private IIndoorInfoDao dao;
        
        public ArrayList<IndoorCheckItemEntity> GetIndoorCheckItemEntities() {
            return dao.GetWscheckItemsOfIndoor();
        }
    }

    4. DAO

    public interface IIndoorInfoDao {
        public ArrayList<IndoorCheckItemEntity> GetWscheckItemsOfIndoor();
    }

    5. entity

    public class IndoorCheckItemEntity implements Serializable {
        private static final long serialVersionUID = -6556793741331167103L;
        private int id;
        private int itemId;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public int getItemId() {
            return itemId;
        }
        public void setItemId(int itemId) {
            this.itemId = itemId;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
    }

    6. 数据库连接字符串:

    spring.datasource.url = jdbc:oracle:thin:@(DESCRIPTION =    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.7.88)(PORT = 1521))    (CONNECT_DATA =      (SERVER = DEDICATED)      (SERVICE_NAME = orcl)    ))
    spring.datasource.username = XXXXXX
    spring.datasource.password = YYYYYY
    
    spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
    spring.datasource.max-idle=10
    spring.datasource.max-wait=10000
    spring.datasource.min-idle=5
    spring.datasource.initial-size=5
    #XML文件位置 指定DAO与SQL语句关联
    mybatis.mapperLocations=classpath:mapper/*.xml

    7.入口函数配置扫描mapper

    @SpringBootApplication
    @EnableSwagger2
    @MapperScan("com.pkyou.Sample.Dao")
    public class Main {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Main.class, args);
        }
    }

    8. SQL语句

    <?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.pkyou.Sample.Dao.IIndoorInfoDao">
    
        <select id="GetWscheckItemsOfIndoor" resultType="com.pkyou.Sample.Entyties.IndoorCheckItemEntity">
            select id itemId,name from yn_app_wscheckitems
            where parent_id=0 and type='1'
    
        </select>
    </mapper>

    9. 项目结构

     10 代码地址 https://github.com/pkyou/SpringBoot.git

  • 相关阅读:
    XPath使用示例
    CSS3中的弹性布局——"em"的用法
    Sublime Text3快捷键实用总结
    学习笔记——关于HTML(含HTML5)的块级元素和行级(内联)元素总结
    JavaScript中的伪数组理解
    深入理解浏览器兼容性模式
    javascript 中使用instanceof需要注意的一点
    用人工智能学习,凡亿推出PCB问题解答智能搜索机器人:pcb助手
    Altium中坐标的导出及利用坐标快速布局
    Altium中Logo的导入方法及大小调整
  • 原文地址:https://www.cnblogs.com/pangkang/p/8296666.html
Copyright © 2020-2023  润新知