现在比较流行的操作数据库操作层框架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类中

public interface UserService {

    User findUserByName(String userName);

}

  

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

@Service
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!

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

springBoot 整合 mybatis+Oracle的更多相关文章

  1. springboot整合mybatis+oracle

    第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择sp ...

  2. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  3. springboot整合mybatis的时候报错Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    今天闲来无事,学习springboot整合mybatis,在bilibili看视频学的,视频中在dao层的interface上面加上org.apache.ibatis.annotations.Mapp ...

  4. SpringBoot整合Mybatis关于分页查询的方法

    最近公司在用到SpringBoot整合Mybatis时当web端页面数据增多时需要使用分页查询以方便来展示数据.本人对分页查询进行了一些步骤的总结,希望能够帮助到有需要的博友.如有更好的方式,也希望评 ...

  5. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  6. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  7. SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]

    SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...

  8. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  9. springboot整合mybatis出现的一些问题

    springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...

随机推荐

  1. 8 django 里面的API

    1.什么是API? 2.在djang里面写API 3.API实战效果 1.移动端的网页 4.restframework :老师方法 (0)安装 Django REST framework 是一个强大且 ...

  2. 2940: [Poi2000]条纹(Multi_SG)

    2940: [Poi2000]条纹 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 114  Solved: 72[Submit][Status][Dis ...

  3. HDU 4005 The war 双连通分量 缩点

    题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...

  4. Linux QA

    gitee: https://gitee.com/dhclly/icedog.script.test/blob/master/doc/linux/linux-qa.md 1. linux 中的 ll( ...

  5. day21&22&23:线程、进程、协程

    1.程序工作原理 进程的限制:每一个时刻只能有一个线程来工作.多进程的优点:同时利用多个cpu,能够同时进行多个操作.缺点:对内存消耗比较高当进程数多于cpu数量的时候会导致不能被调用,进程不是越多越 ...

  6. Hyper-v Server 2012 R2增强会话模式

    从Windows Server 2012 R2开始,通过使用Hyper-v增强会话模式Hyper-v中的虚拟机允许重定向虚拟机连接会话中的本地资源.这是因为Windows Server 2012 及早 ...

  7. OpenStack之虚机冷迁移代码简析

    OpenStack之虚机冷迁移代码简析 前不久我们看了openstack的热迁移代码,并进行了简单的分析.真的,很简单的分析.现在天气凉了,为了应时令,再简析下虚机冷迁移的代码. 还是老样子,前端的H ...

  8. 线段树&树状数组模板

    树状数组: #include <bits/stdc++.h> using namespace std; ; struct binit { int a[MAXN], n; void modi ...

  9. Selenium Java 自动化 介绍及开发工具的使用(一)

    前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以在学习前先要弄清楚版本号,这点非常重要.本系列依然以selenium2为基础,目前selenium3本人没做过研 ...

  10. mybatis的使用及详解

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...