SpringBoot+Mybatis的简单使用

时间:2021-09-19 16:53:29

Mybatis在现在的开发中已经用的很广泛了,但是在今后的微服务体系中,mybatis的使用率已经渐渐变低了,微服务通常采用的架构都是一个模块使用一个数据库,所以大大减轻了编程人员和DBA的工作量,常见的增删改查就可以满足业务的需要,加上Hibernate或者类似于JPA这种直接反向映射生成的ORM框架,对数据库的操作和建表要求就很低了,但是如果一些传统的大项目或者对业务逻辑要求比较高的企业,使用mybatis可以更快更自如的操纵数据库,而且在优化sql的同时,减轻了并发所带来的时间成本需求,可以说mybatis框架还是有很大的发展空间的。

这个小Demo只是简单的将mybatis和springboot整合在一起,方便以后在开发过程中随时查阅。
先上我的项目截图:

SpringBoot+Mybatis的简单使用

整个项目的架构很简单,只是采用了传统的MVC三层模式,所要注意的就是MyBatis在加入SpringBoot时,只需要在Maven中加入一个依赖就可以了:

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

下面这个是完整的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="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.springboottest</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mybatis</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

mybatis.config文件

<!--我不喜欢使用纯注解的形式,所以采用xml和注解结合的形式使用mybatis-->

<?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>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>

TestMapper.java

package com.springboottest.mybatis.mapper;

import org.springframework.stereotype.Component;

@Component
public interface TestMapper {
Integer countAll();
}

TestMapper.xml

<?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.springboottest.mybatis.mapper.TestMapper">
<select id="countAll" resultType="java.lang.Integer">
SELECT count(1) from test;
</select>
</mapper>

TestController.java

package com.springboottest.mybatis.controller;


import com.springboottest.mybatis.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@Autowired
private TestMapper mapper;

@RequestMapping("/testMapping")
public Integer findAll(){
Integer a = mapper.countAll();
return a;
}

}

MybatisApplication.java

package com.springboottest.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.springboottest.mybatis.mapper")
public class MybatisApplication {

public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}

application.properties

mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3308/blog?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root