Springboot整合mybatis

之前一直都在用这个框架,但是自己没有从头开始整合过,有机会整合了一下,发现还是有些坑要踩,总结一下。

1.新建一个maven项目

项目如下:

pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yuanyuan.test</groupId>
  <artifactId>springboot-mybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
  
    <dependencies>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- spring boot jdbc -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- spring boot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mysql -->
        <!-- <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency> -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!--<version>3.8.1</version>-->
            <scope>test</scope>
        </dependency>
        <!-- Oracle数据库 -->
        <dependency>
			<groupId>com.yuanyuan.test</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0.3.0</version>
			<scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc14-10.2.0.3.0.jar</systemPath>
		</dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Tips

需要整合mysql还是Oracle,根据自己的情况引入相关依赖。但是一般的maven库里面都没有Oracle的依赖,所以需要手动下载jar包,添加到lib文件下,然后在pom中引入即可。

2.application.yml配置服务和数据库的相关信息

server:
    port: 8080
    context-path: /springboot-mybatis/
spring:
    #数据库配置
    datasource:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:oracle:thin:@127.0.0.1:orcl
        username: 用户名
        password: 密码
mybatis:
    #mybatis mapper.xml文件路径
    mapper-locations: com.yuanyuan.mapper/*.xml
    #实体类包路径
    type-aliases-package: com.yuanyuan.entity
tips:

context-path是项目的路径,不写的话默认/

3.启动类:

package com.yuanyuan;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@MapperScan(basePackages= {"com.yuanyuan.mapper"}) //mapper类的包,可以去掉这个,在mapper类上添加@mapper注解,但是太麻烦了,每个都要加
public class ItmpApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ItmpApplication.class, args);
	}
	
	/**
	 * 解决跨域访问
	 * @return
	 */
	@Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*");
            }
        };
    }
}
tips:

启动类中注入了WebMvcConfigurer类,重写了addCorsMappings()方法。这样解决了跨域访问的问题。直接访问接口是没有问题,但是当前端是一个单独的项目,我自己用的element+vuejs的框架,在访问接口时就会报Response to preflight request doesnt pass access control check: No Access-Control-Allow-Originheader is present on the requested resource. Origin xxxis therefore not allowed access. The response had HTTP status code 403的错误。参考点击打开链接

4.利用mybatis自动生成工具生成mapper和实体类

5.编写service,实现类需要加@service注解
6.编写controller
7.测试

浏览器访问对应接口:

可以看到已经成功了,service和controller就不贴代码了,很简单,根据自己实际情况编写即可。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页