springBoot 整合 mybatis+Oracle

  现在比较流行的操作数据库操作层框架Mybatis,下面我们就来看看Springboot如何整合mybatis, 之前一直在用xml形式写sql,这次依然用xml的方式感觉这种还是比较灵活方便。

  添加mybatis关键就是要引入mybatis-spring-boot-starter到pom文件中,如果你用MySQL,那就引入MySQL的pom文件,这里我用Oracle,淡然要引入Oracle的依赖了。添加完成mybatis和Oracle 在pom.xml 文件中的引入。

<!-- 链接Oracle数据库  oracle ojdbc不免费,需要手动引入jar包 -->
     <dependency>
        <groupId>oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
     </dependency>
     <!-- 集成mybatis -->
     <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
     </dependency>
     <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.0</version>
     </dependency>

  

注意:

如果你用的是Oracle数据库在pom.xml中有可能会报这个错Missing artifact oracle:ojdbc6:jar 因为Oracle的ojdbc.jar是收费的,所以maven的中央仓库没有这个资源,只能配置本地仓库才能加载到项目中去。

配置application.properties

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
#oracle database  jdbc
spring.datasource.url=jdbc:oracle:thin:@//localhost:1521/orcl
spring.datasource.username=root
spring.datasource.password=123456
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

  

指定了mybatis的配置地址,你可以在mybatis/mybatis-config.xml中添加一些其他配置

完成之后你就可以去mapper文件夹下自由潇洒的去写sql去了

我们来测试一下看看是否成功,去写个sql试试效果怎么样?就返回用户的一些基本信息吧。实现代码如下:

Controller层:

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public User findUserByName(@RequestParam(value = "userName", required = true) String userName) {
        return userService.findUserByName(userName);
    }

}

  

Service层:只写方法具体实现在UserServiceImpl类中

@Service
public interface UserService {

    User findUserByName(String userName);

}

  

Service层的实现类,实现UserService中的方法

@Component
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUserByName(String userName) {
        User user = userDao.findUserByName(userName);
        return  user;
    }

}

  

Dao层 注意要添加@Mapper注解,否则会报错的

@Mapper
public interface UserDao {
    User findUserByName(@Param("userName") String userName);
}

  

实体类

public class User {
    private String code;
    private String username;
    private String name;
    //get/set方法省略
}

  

userMapper.xml  要注意namespace能否正确跳转(路径是否正确)

<?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.power.manger.dao.UserDao">
    <select id="findUserByName" resultMap=" com.XXX.model.User " parameterType="java.lang.String">
        select code, user_name as username, name
        from tb_user_info
         where name=#{userName}
    </select>
</mapper>

  这些都完成之后,那么接下来就是见证奇迹的时刻了,启动项目,浏览器输入地址显示如下

数据库中查询结果如下

成功整合了mybatis和Oracle!

  如有不当和错误之处,请指出,我们一起交流学习,共同进步!谢谢!

原文地址:https://www.cnblogs.com/zhang-dongliang/p/10944342.html

时间: 2024-07-28 19:14:24

springBoot 整合 mybatis+Oracle的相关文章

springboot整合mybatis+oracle

第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包. 第二步 打开项目的结构,发现有了有趣的部分 在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件

springboot整合mybatis实现逆向工程

springboot整合mybatis创建逆向工程,快速的创建pojo实体,dao接口,mapper xml文件 第一步添加依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 htt

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:s

springboot整合mybatis(注解)

springboot整合mybatis(注解) 1.pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringBoot整合Mybatis【非注解版】

接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ? 选择Spring Initializr,配置JDK版本 ? 输入项目名 ? 选择构建web项目所需的staters(启动器) ? 选择与数据库相关的组件 ? 分析:Spring Boot基本上将我们实际项目开发中所遇到的所有场景都做了封装.它将所有的功能场景都抽取出来,做成了一个个的staters(启动器),只需要在项目的pom.xml配置文件里面引入这些starter相关场景的所有依赖都会导入进来.需要什

SpringBoot 整合MyBatis案例详解

SpringBoot约定大于配置的特点,让框架的配置非常简洁,与传统的SSM框架相比,简化了大量的XML配置,使得程序员可以从繁琐的配置中脱离出来,将精力集中在业务代码层面,提高了开发效率,并且后期的维护成本也大大降低. 从源码中可以看到,每一个springboot集成的jar包也是一个maven的子module,springboot已经将相关依赖的jar包自动添加到工程中,不需要开发人员手动去添加了,这也是springboot能够简化配置的一个重要原因. 下面开始说明springboot是如何