springboot整合mybatis+oracle

wu1ao2ya3      2022-04-23     407

关键词:

第一步

认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合。

创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包。

第二步

打开项目的结构,发现有了有趣的部分

在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件,此文件可以作为主函数的入口,相同的是再整合mybatis时,我们仍然将dao层改为Mapper层,而且这里只定义接口。

往下看,在resources的目录下,有com,lib,static,templates,views文件夹,以及一个springboot最为主要的一个配置文件。

介绍一下这些文件夹或者文件的作用:

com   下是和上面java下的目录结构一样,不多只有一个Mapper文件夹和一个config文件夹,mapper里放对应接口的xml文件来提供sql语句,config文件夹放的是修改过的mybatis-config.xml配置文件.

lib 里面放一些添加的jar包

static 里面放的是静

态的文件css,js,img之类

templates 未知...

views放我们的页面

第三步

导入jar包

我们只需要导入需要的jar包,关于spring产品的jar包springboot帮我们安排好了,我们大概需要这些jar包。

        <!--oracle jar包-->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.1.0.6.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/main/resources/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
    </dependency>
    <!--mysql jar包-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
    <!--springboot和mybatis结合的jar包-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>
    <!--fastjson包-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <!--springboot模板-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--日志-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>
    <!--使用不严格的页面标签-->
    <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
    </dependency>

配置配置文件

application.properties文件

#spring.datasource.platform=mysql
#spring.datasource.url=jdbc:mysql://localhost/mydb
#spring.datasource.username=root
#spring.datasource.password=hnqy
#spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.platform=oracle
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver

server.port=8080
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8

#####springboot 整合 mybatis
mybatis.mapper-locations= classpath:/com/qy/springboot01/mapper/*Mapper.xml
mybatis.config-location= classpath:/com/qy/springboot01/mapper/config/mybatis-config.xml
#####定义别名
#####mybatis.type-aliases-package=com.qy.springboot01.domain


#-----------------------日志------------------------------
 #启用调试日志。
#debug = true
 #启用跟踪日志。
#trace = true

#LOGGING
 #日志配置文件的位置。例如,Logback的`classpath:logback.xml`。
#logging.config =
 #记录异常时使用的转换字。
#logging.exception-conversion-word =%wEx
#名称可以是确切的位置或相对于当前目录。
#logging.file =  #?
 #要保留的归档日志文件的最大数量。仅支持默认的登录设置。
#logging.file.max-history = 0
  #最大日志文件大小。仅支持默认的登录设置。
#logging.file.max-size = 10MB
 #日志级别严重性映射。例如`logging.level.org.springframework = DEBUG`。
#logging.level.* =
  #日志文件的位置。例如,`/ var
#logging.path =
#-------------------------模板------------------------------
###Thymeleaf配置
spring.thymeleaf.prefix=classpath:/view/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=LEGACYHTML5
###过滤中文乱码
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true

配置mybatis-config配置文件

 

<?xml version="1.0" encoding="UTF-8" ?>  
  <!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<settings>

        <!--是否使用缓存 开发中禁用-->

        <setting name="cacheEnabled" value="false"/>

        <!--下划线和驼峰式命名法的匹配  -->

        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <!--使用启用懒加载机制  true启用-->

        <setting name="lazyLoadingEnabled" value="true"/>

        <!--配置myabtis日志-->

        <setting name="logImpl" value="LOG4J2"/>
<setting name="multipleResultSetsEnabled" value="true"/>
    <setting name="useColumnLabel" value="true"/>
    <setting name="useGeneratedKeys" value="true"/>
    <setting name="autoMappingBehavior" value="PARTIAL"/>
    <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
    <setting name="defaultExecutorType" value="SIMPLE"/>
    <setting name="defaultStatementTimeout" value="25"/>
    <setting name="defaultFetchSize" value="100"/>
    <setting name="safeRowBoundsEnabled" value="false"/>
    <setting name="localCacheScope" value="SESSION"/>
    <setting name="jdbcTypeForNull" value="OTHER"/>
    <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>
<typeAliases>
    <!--给类起别名-->
    <!--给包中所有的类起别名 默认名字为类名 -->
    <package name=""></package>
</typeAliases>
</configuration>

 

第四步

一些细节

1.我们发现,controller层我们的mybatis需要@Controller

但是我们的springboot所需要的确是@Controller

2.service层注解没有变化

但在Mapper层我们要写上@Mapper和@conment两个注解

3.我们不需要tomcat,应为springboot内置了tomcat,而且启动的很快

4.在配置文件里的细节自己去深究吧。

5.这里只是简单的介绍,具体实操还需要自己去熟悉。

6.还有,config文件夹在mapper下面。。。

附上目录结构图技术分享图片

 

springboot---整合druid连接池---连接oracle数据库---整合mybatis---整合thymeleaf---日志配置

目录在新建的springboot项目pom文件中新添druid连接池的依赖在application.properties配置文件中添加配置配置静态文件目录和模板文件目录@(springboot---整合druid连接池---连接oracle数据库---整合mybatis---整合thymeleaf---日志配置)在新建的springb... 查看详情

springboot整合mybatis连接oracle

pom.xml:<!--链接:https://pan.baidu.com/s/1agHs5vWeXf90r3OEeVGniw提取码:wsgm--><dependency><groupId>com.oracle</groupId><artifactId>ojdbc8</artifactId><version>12.2 查看详情

springboot整合其他框架--springboot整合mybatis(代码片段)

1.SpringBoot整合Mybatis需求:SpringBoot整合MyBatis。实现步骤:搭建SpringBoot工程引入mybatis起步依赖,添加mysql驱动编写DataSource和MyBatis相关配置定义表和实体类编写dao和mapper文件/纯注解开发测试1.0公共步骤1.0.1搭建SpringBoot... 查看详情

springboot整合mybitas怎么配置oracle?

...入mybatis依赖。引入mysql驱动。项目pom.xml一览。这样完成springboot整合mybitas配置oracle。参考技术A身份认证框架,Shiro,实现用户、角色、身份认证常用功能。DB连接池:Druid,可实现大数据的快速查询和分析,性能很优秀。持久层框... 查看详情

springboot:springboot整合mybatis案例

文章目录SpringBoot整合Mybatis案例一、导入依赖二、编写配置文件三、编写功能代码 查看详情

springboot学习笔记-整合mybatis

Springboot学习笔记(二)- 整合MyBatis SpringBoot中整合MyBatis,并通过注解方式实现映射。整合MyBatis以Springboot学习笔记(一)-Helloworld 为基础项目,在pom.xml中添加如下依赖 <dependency>  <groupId>org.mybatis.spring.b... 查看详情

springboot_数据访问之mybatis整合(代码片段)

...:sqlserver,Oracle,Mysql;NOSQL包括:MongoDB和redis。 二:springboot与jdbc整合 1.1  首先添加依赖     <dependency><groupId>org.s 查看详情

springboot整合mybatis

1、springboot配置数据库连接池druid2、springboot整合mybatis3、springboot整合pagehelper  springboot配置数据库连接池druid 新建springboot项目   相关pom依赖druid所需pom依赖 <dependency><groupId> 查看详情

springboot.04.springboot整合mybatis

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot整合mybatis

SpringBoot整合MyBatis创建一个SpringBoot项目需要的pom依赖列表如下图右侧:准备数据createdatabasenyf;USEnyf;CREATETABLEcategory_(idint(11)NOTNULLAUTO_INCREMENT,namevarchar(32)DEFAULTNULL,PRIMARYKEY(id))ENGINE=MyISAMAUTO_INCREMEN 查看详情

springboot整合mybatis方式2:使用注解方式整合mybatis(代码片段)

SpringBoot整合Mybatis简介SpringBoot整合Mybatis方式2:使用注解方式整合Mybatis1.先用idea创建一个添加mybatis需要的相关依赖的工程。2.准备数据库和表3.创建表映射类4.创建mapper代理接口5.创建Service层和Service的实现层6.创建控制层࿰... 查看详情

基于springboot的完成mybatis整合(代码片段)

SpringBoot🍌掌握基于SpringBoot框架的程序开发步骤🍌使用SpringBoot配置信息修改服务器配置🍌SpringBoot完成SSM整合之SpringBoot整合junit今日目标:基于SpringBoot的完成mybatis整合一、SpringBoot整合mybatis1.1回顾Spring整合MybatisS... 查看详情

springboot+mybatis整合

    LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架... 查看详情

springboot与mybatis整合(*)

在pom.xml文件中加入数据库、spring-mybatis整合<!--springboot整合mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId> 查看详情

springboot+mybatis整合

  LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架搭建真的... 查看详情

springboot.04.springboot整合mybatis(代码片段)

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot.04.springboot整合mybatis(代码片段)

SpringBoot.04.SpringBoot整合MyBatis准备工作1.mapper模板2.t_user整合MyBatis1.新建Module2.pom.xml3.application.yml4.Springboot04MybatisApplication.java5.User.java6.UserMapper.java7.UserMapper.xml8.UserService9.User 查看详情

springboot+mybatis整合入门

SpringBoot+Mybatis四步整合第一步添加依赖springBoot+Mybatis相关依赖<!--springBoot相关--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifa 查看详情