springboot整合mybatis实现简单增删改查
1.新建一个springboot项目
2.pom文件引入相关依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--postgresql-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
3.配置文件application.yml
# 数据库连接配置
spring:
datasource:
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: 123456
# mybatis 配置
mybatis:
mapperLocations: classpath:mapper/*Mapper.xml
configuration:
map-underscore-to-camel-case: true
type-aliases-package: com.example.springbootdemo.domain
4.业务代码
4.1 创建数据库表
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
4.2 创建实体类Company
package com.springboot.mybatis.domain;
import lombok.Data;
import org.apache.ibatis.type.Alias;
import java.io.Serializable;
@Data
@Alias("Company")
public class Company implements Serializable {
private static final Long serialVersionUID=1L;
private Long id;
private String name;
private int age;
private String address;
private Double salary;
}
4.3 Controller层
package com.springboot.mybatis.controller;
import com.springboot.mybatis.domain.Company;
import com.springboot.mybatis.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class CompanyController {
@Autowired
CompanyService companyService;
/**
* 查询所有员工
* @return
*/
@RequestMapping("/selectAllStaff")
public List<Company> selectAllStaff(){
return companyService.selectAllStaff();
}
/**
* 新增员工
* @return
*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String insertStaff(@RequestBody Company company){
int id = company.getId();
List<Company> list = companyService.selectAllStaffById(id);
if(list.isEmpty()){
return companyService.insertStaff(company);
}
return "id已经存在";
}
/**
* 修改
*/
@RequestMapping(value = "/update",method = RequestMethod.POST)
public String updateStaffById(@RequestBody Company company){
int id = company.getId();
List<Company> list = companyService.selectAllStaffById(id);
if(list.isEmpty()){
return "id不存在,修改失败";
}
return companyService.updateStaffById(company);
};
/**
* 批量删除
*/
@RequestMapping(value = "/delete",method = RequestMethod.POST)
public String delete(@RequestParam("ids") List<Integer> ids){
return companyService.delete(ids);
};
}
4.4 Service层
package com.springboot.mybatis.service;
import com.springboot.mybatis.domain.Company;
import java.util.List;
public interface CompanyService {
List<Company> selectAllStaff();
String insertStaff(Company company);
List<Company> selectAllStaffById(int id);
String updateStaffById(Company company);
String delete(List<Integer> ids);
}
4.5 ServiceImpl实现类
package com.springboot.mybatis.service.impl;
import com.springboot.mybatis.dao.CompanyMapper;
import com.springboot.mybatis.domain.Company;
import com.springboot.mybatis.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CompanyServiceImpl implements CompanyService {
@Autowired
private CompanyMapper companyMapper;
@Override
public List<Company> selectAllStaff() {
return companyMapper.selectAllStaff();
}
@Override
public String insertStaff(Company company) {
int addResult = companyMapper.insertStaff(company);
if(addResult == 1){
return "添加成功";
}else{
return "添加失败";
}
}
@Override
public List<Company> selectAllStaffById(int id) {
return companyMapper.selectAllStaffById(id);
}
@Override
public String updateStaffById(Company company) {
int addResult = companyMapper.updateStaffById(company);
if(addResult == 1){
return "修改成功";
}else{
return "修改失败";
}
}
@Override
public String delete(List<Integer> ids) {
int addResult = companyMapper.delete(ids);
if(addResult > 0){
return "删除成功";
}else{
return "删除失败";
}
}
}
4.6 dao层
package com.springboot.mybatis.dao;
import com.springboot.mybatis.domain.Company;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CompanyMapper{
List<Company> selectAllStaff();
int insertStaff(Company company);
List<Company> selectAllStaffById(int id);
int updateStaffById(Company company);
int delete(List<Integer> ids);
}
4.7 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.springboot.mybatis.dao.CompanyMapper">
<resultMap id="BaseResultMap" type="com.springboot.mybatis.domain.Company">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="salary" jdbcType="FLOAT" property="salary" />
</resultMap>
<select id="selectAllStaff" resultType="com.springboot.mybatis.domain.Company">
select * from company
</select>
<select id="selectAllStaffById" resultType="com.springboot.mybatis.domain.Company">
select * from company where id = #{id}
</select>
<insert id="insertStaff" useGeneratedKeys="true" keyProperty="id">
insert into company (id,name,AGE,address,salary) VALUES (#{id}, #{name}, #{age}, #{address}, #{salary});
</insert>
<update id="updateStaffById" parameterType="com.springboot.mybatis.domain.Company" >
update company set name = #{name}, age = #{age} ,address = #{address}, salary = #{salary} where id = #{id}
</update>
<!--批量删除-->
<delete id="delete">
delete from company where id in
<foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</delete>
</mapper>
4.8 启动类添加注解
@MapperScan("com.springboot.mybatis.dao")
5.目录结构
6. postman测试
6.1 查询
6.2 新增
6.3 修改
6.4 批量删除
7.开启控制台日志打印
logging:
level:
com.springboot.mybatis.dao : debug
8.git代码地址
https://github.com/bruce6708/springboot_Mybatis.
由于工作原因,很久没有正式写过后台代码,此文用于回顾一下springboot项目的搭建,以及整合mybatis对postgresql数据库进行简单的增删改查操作。