Springboot 整合 Mybatis
创建SpringBoot项目
首先在IDEA中创建一个SpringBoot项目,注意Java Version 然后Packaging为Jar包形式,Type改为Maven形式。
在上图的下一步中可以选择相关依赖,也可以在项目里面的pom文件中自己添加相关依赖,然后进行import也可以。
1 创建maven工程添加依赖
<!--SpringBoot整合Mybatis的启动器--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><!--mysql驱动包--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--web模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
2 建数据库数据库表
复制这段代码至Navicat中,首先需要创建一个mybatis的数据库,然后在新建查询中粘贴代码,运行全部即可。
CREATE TABLE `account` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`money` double(10,2) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1005 DEFAULT CHARSET=utf8
3 创建数据模型
首先需要在项目中创建model实体类的文件夹,然后创建Account的实体类,里面可以添加lombok的插件,但是需要在IDEA插件市场中进行下载,并且添加依赖即可使用。添加如下依赖还可以对字段进行描述。并且生成Swagger-ui文档。
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
这里使用的是lombok依赖,可以生成无参、有参的构造函数,还可以生成ToString 方法等。
要实现数据永久存储必须实现序列化接口 implements Serializable 通常还有一个序列化ID,这里没有列出。
/* @author caojun/
public class Account implements Serializable {private Integer id;private String name;private double money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getMoney() {return money;}public void setMoney(double money) {this.money = money;}
}
4 创建接口和mapper配置文件
这是创建的接口,xml映射配置文件就是根据这里来实现的,特别注意接口的名称及接口下面的方法名称,如果没有与映射配置文件对应就会出现错误。
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.Account;
import org.apache.ibatis.annotations.Mapper;@Mapper//创建接口代理对象
public interface AccountMapper {Account findById(Integer id);
}
这里是映射配置文件,注意namespace的接口的路劲,下面的方法就是接口的方法,要注意一一对应。下面是对应的Sql语言编写方式,要注意#{}的用途及好处,防止SQL注入。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisdemo.mapper.AccountMapper"><!-- Account findById(Integer id);--><select id="findById" resultType="com.example.mybatisdemo.model.Account">select * from account where id=#{id}</select>
</mapper>
5 配置application.yml(数据源,Myatis配置)
这里没有进行配置端口,默认是8080,配置了数据库的相关,驱动、用户名、密码、数据库ip、端口、数据库名称。
下面是对mybatis的配置,主要是映射配置文件位置及实体类的别名,开启驼峰格式等。
spring:datasource:url: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 1111driver-class-name: com.mysql.jdbc.Driver# 配置mybatis规则
mybatis:#config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置mapper-locations: classpath:mapper/*.xml #sql映射文件位置 G:\\mybatisdemo\\src\\main\\resources\\mappertype-aliases-package: com.example.mybatisdemo.model #配置了实体的别名 com/example/mybatisdemo/modelconfiguration:map-underscore-to-camel-case: true #开启 指将带有下划线的表字段映射为驼峰格式的实体类属性。
6 创建业务层接口和实现类
这里是创建了接口
package com.example.mybatisdemo.service;import com.example.mybatisdemo.model.Account;public interface AccountService {Account findByIdService(Integer id);
}
@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountMapper accountMapper;@Overridepublic Account findByIdService(Integer id) {return accountMapper.findById(id);}
}
7 创建控制器controller
package com.example.mybatisdemo.controller;import com.example.mybatisdemo.model.Account;
import com.example.mybatisdemo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/account")
public class AccountController {@Autowiredprivate AccountService accountService;@RequestMapping("/findById")public Account findById(Integer id){return accountService.findByIdService(id);}
}
需要添加数据到mysql中,主键是自增类型的。
8 启动服务测试结果
如下是访问的地址,因为是get请求,可以直接在浏览器中测试,也可以在postman中进行如下测试。
访问地址: http://localhost:8080/account/findById?id=1
测试结果: