版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33223299/article/details/86698949
一 前言
本篇记录的是springboot整合mybatis框架,这里用的数据库是mysql,工具spring-tool,用的版本是spring boot 1.5
二 项目搭建
通过spring-tool工具新建项目,如下图所示
新建完成后的项目,根据自己的习惯命名分层建立控制层,业务层,数据处理层以及实体类的包名,如下图所示
pom.xml依赖::
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties配置:
#mysql数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootMybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#d端口号设置
server.port=8023
#显示数据库执行语句
spring.jpa.show-sql=true
#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
三 代码模拟
用代码简单的模拟一个用户余额的功能,简单的建立一张表
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
通过mybatis自动生成代码工具,生成对应的实体类与dao层
如下:
Account:
package com.chp.model;
public class Account {
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 == null ? null : name.trim();
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
Mapper:
package com.chp.dao;
import com.chp.model.Account;
import com.chp.model.AccountExample;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface AccountMapper {
int countByExample(AccountExample example);
int deleteByExample(AccountExample example);
int deleteByPrimaryKey(Integer id);
int insert(Account record);
int insertSelective(Account record);
List<Account> selectByExample(AccountExample example);
Account selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Account record, @Param("example") AccountExample example);
int updateByExample(@Param("record") Account record, @Param("example") AccountExample example);
int updateByPrimaryKeySelective(Account record);
int updateByPrimaryKey(Account record);
}
新建自己的业务层接口与其实现类:
service:
public interface AccountService {
List<Account> selectAccount() throws Exception;
int updateAccount(int id) throws Exception;
int deleteAccount(int id) throws Exception;
}
serviceImpl:
package com.chp.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.chp.dao.AccountMapper;
import com.chp.model.Account;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
/**
* 模拟查询信息业务
*/
@Override
public List<Account> selectAccount() throws Exception {
return accountMapper.selectByExample(null);
}
/**
* 模拟更改信息业务
*/
@Override
@Transactional
public int updateAccount(int id) throws Exception {
Account account = new Account();
account.setMoney((double) 200);
account.setName("张三丰");
account.setId(id);
return accountMapper.updateByPrimaryKeySelective(account);
}
/**
* 模拟删除信息业务
*/
@Override
public int deleteAccount(int id) throws Exception {
return accountMapper.deleteByPrimaryKey(id);
}
}
控制层mvc层:
package com.chp.controller;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.chp.model.Account;
import com.chp.service.AccountService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
@RestController
@RequestMapping("/account")
public class AccountController {
@Resource
private AccountService accountService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Account> getAccounts() throws Exception {
List<Account> list = accountService.selectAccount();
return list;
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String updateAccount(@RequestParam(value = "id", required = true) int id) {
int t;
try {
t = accountService.updateAccount(id);
if (t == 1) {
return "success";
} else {
return "fail";
}
} catch (Exception e) {
return "请联系管理员";
}
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteAccount(@RequestParam(value = "id", required = true) int id) {
int t;
try {
t = accountService.deleteAccount(id);
if (t == 1) {
return "success";
} else {
return "fail";
}
} catch (Exception e) {
return "请联系管理员";
}
}
}
四 测试
运行Application,在浏览器中输入http://localhost:8023/account/list进行测试
测试成功!
springboot确实方便!!