springboot整合mybatis在开发中十分的常用,今天我们来说说整合的步骤:
1、pom文件引入依赖
<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>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</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>
</dependencies>
2、引入数据源
application.properties配置文件中引入数据源:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://61.236.95.122:3306/test2?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=114152681s4lu
# mybatis
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
3、建表
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
4、代码实现
po类:
package com.yql.springbootmybatis.po;
/**
* 账户实体类
*/
public class Account {
private int id;
private String name;
private double money;
public int getId() {
return id;
}
public void setId(int 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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
dao层:
package com.yql.springbootmybatis.dao;
import org.apache.ibatis.annotations.Param;
/**
*
*/
public interface AccountMapper {
int update(@Param("money") double money, @Param("id") int id);
}
mapper.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.yql.springbootmybatis.dao.AccountMapper">
<resultMap id="BaseResultMap" type="com.yql.springbootmybatis.po.Account">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="money" jdbcType="INTEGER" property="money"/>
</resultMap>
<update id="update">
UPDATE account set money=#{money} WHERE id=#{id}
</update>
</mapper>
service层
package com.yql.springbootmybatis.service;
public interface AccountService {
public void transfer() throws RuntimeException;
}
service实现层
package com.yql.springbootmybatis.service.impl;
import com.yql.springbootmybatis.dao.AccountMapper;
import com.yql.springbootmybatis.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
AccountMapper accountMapper;
@Transactional
public void transfer() throws RuntimeException{
accountMapper.update(90.0,1);//用户1减10块 用户2加10块
// int i=1/0;
accountMapper.update(110.0,2);
}
}
controller层
package com.yql.springbootmybatis.controller;
import com.yql.springbootmybatis.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
AccountService accountService;
@PutMapping(("/transfer"))
public void transfer() {
accountService.transfer();
}
}
通过postman测试通过。
项目地址:https://github.com/muyi25/springBoot 项目名称:spring-boot-mybatis
网友评论