一、项目结构展示
二、开始整合
1、引入pom依赖
进入Maven中央仓库选择自己所需要的依赖,maven仓库地址:Maven Central
完整Maven依赖如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mgx</groupId>
<artifactId>springboot-mgx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mgx</name>
<description>springboot项目整合</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mysql相关-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
2、添加application 或 bootstrap 配置
server:
# 端口号
port: 8080
spring:
# 配置数据源
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mgx_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置mybatis
mybatis:
#指定位置扫描Mapper接口对应的XML文件 classpath:xml文件位置
mapper-locations: classpath:mapper/*.xml
#指定扫描包位置让mybatis自动扫描到指定义的entity包下
type-aliases-package: com.mgx.entity
3、创建表
CREATE TABLE `t_info_user` (
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`name` varchar(64) NOT NULL COMMENT '姓名',
`phone` varchar(11) NOT NULL COMMENT '手机号',
`age` int(3) NOT NULL COMMENT '年龄',
`sex` int(1) NOT NULL COMMENT '性别 0-女 1-男 2-其他',
`address` varchar(255) NOT NULL COMMENT '家庭地址',
`income` decimal(19,2) DEFAULT NULL COMMENT '年收入',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户基础信息表';
4、项目结构
5、类内容
UserController
package com.mgx.controller;
import com.mgx.entity.InfoUser;
import com.mgx.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author mgx
* @date 2023/9/15 3:25 PM
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@PostMapping("/addUserInfo")
public String addUserInfo(@RequestBody InfoUser infoUser) {
boolean flag = userService.addUserInfo(infoUser);
if (flag){
return "添加成功!";
} else {
return "添加失败!";
}
}
@GetMapping("/getUserInfo")
public InfoUser getUserInfo(@Param("id") Long id) {
return userService.getUserInfo(id);
}
@DeleteMapping("/deleteUserInfo")
public String deleteUserInfo(@Param(value = "id") Long id){
boolean flag = userService.deleteUserInfo(id);
if (flag){
return "删除成功!";
} else {
return "删除失败!";
}
}
@PutMapping("/updateUserInfo")
public String updateUserInfo(@RequestBody InfoUser infoUser){
boolean flag = userService.updateUserInfo(infoUser);
if (flag){
return "更新成功!";
} else {
return "更新失败!";
}
}
}
UserService
package com.mgx.service;
import com.mgx.entity.InfoUser;
/**
* @author mgx
* @date 2023/9/15 3:38 PM
*/
public interface UserService {
boolean addUserInfo(InfoUser infoUser);
InfoUser getUserInfo(Long id);
boolean deleteUserInfo(Long id);
boolean updateUserInfo(InfoUser infoUser);
}
UserServiceImpl
package com.mgx.service.impl;
import com.mgx.entity.InfoUser;
import com.mgx.mapper.InfoUserMapper;
import com.mgx.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author mgx
* @date 2023/9/15 3:38 PM
*/
@Service
public class UserServiceImpl implements UserService {
@Resource
private InfoUserMapper infoUserMapper;
@Override
public boolean addUserInfo(InfoUser infoUser) {
int addRow = infoUserMapper.insert(infoUser);
return addRow == 1;
}
@Override
public InfoUser getUserInfo(Long id) {
return infoUserMapper.selectByPrimaryKey(id);
}
@Override
public boolean deleteUserInfo(Long id) {
int deleteRow = infoUserMapper.deleteByPrimaryKey(id);
return deleteRow == 1;
}
@Override
public boolean updateUserInfo(InfoUser infoUser) {
int updateRow = infoUserMapper.updateByPrimaryKeySelective(infoUser);
return updateRow == 1;
}
}
InfoUser
package com.mgx.entity;
import java.math.BigDecimal;
/**
* 用户基础信息表
* @author mgx
*/
public class InfoUser {
/**
* 自增主键
*/
private Long id;
/**
* 姓名
*/
private String name;
/**
* 手机号
*/
private String phone;
/**
* 年龄
*/
private Integer age;
/**
* 性别 0-女 1-男 2-其他
*/
private Integer sex;
/**
* 家庭地址
*/
private String address;
/**
* 年收入
*/
private BigDecimal income;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public BigDecimal getIncome() {
return income;
}
public void setIncome(BigDecimal income) {
this.income = income;
}
}
InfoUserMapper
package com.mgx.mapper;
import com.mgx.entity.InfoUser;
/**
* @author mgx
* @date 2023/9/15 3:29 PM
*/
public interface InfoUserMapper {
int deleteByPrimaryKey(Long id);
int insert(InfoUser record);
int insertSelective(InfoUser record);
InfoUser selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(InfoUser record);
int updateByPrimaryKey(InfoUser record);
}
InfoUserMapper.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.mgx.mapper.InfoUserMapper">
<resultMap id="BaseResultMap" type="com.mgx.entity.InfoUser">
<!--@mbg.generated-->
<!--@Table t_info_user-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="sex" jdbcType="INTEGER" property="sex" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="income" jdbcType="DECIMAL" property="income" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, `name`, phone, age, sex, address, income
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
<!--@mbg.generated-->
select
<include refid="Base_Column_List" />
from t_info_user
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
<!--@mbg.generated-->
delete from t_info_user
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.mgx.entity.InfoUser" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into t_info_user (`name`, phone, age,
sex, address, income
)
values (#{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{sex,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR}, #{income,jdbcType=DECIMAL}
)
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.mgx.entity.InfoUser" useGeneratedKeys="true">
<!--@mbg.generated-->
insert into t_info_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
`name`,
</if>
<if test="phone != null">
phone,
</if>
<if test="age != null">
age,
</if>
<if test="sex != null">
sex,
</if>
<if test="address != null">
address,
</if>
<if test="income != null">
income,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="sex != null">
#{sex,jdbcType=INTEGER},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
<if test="income != null">
#{income,jdbcType=DECIMAL},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.mgx.entity.InfoUser">
<!--@mbg.generated-->
update t_info_user
<set>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=INTEGER},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="income != null">
income = #{income,jdbcType=DECIMAL},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.mgx.entity.InfoUser">
<!--@mbg.generated-->
update t_info_user
set `name` = #{name,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
sex = #{sex,jdbcType=INTEGER},
address = #{address,jdbcType=VARCHAR},
income = #{income,jdbcType=DECIMAL}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
SpringbootMgxApplication
package com.mgx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author mgx
*/
@MapperScan("com.mgx.mapper")
@SpringBootApplication
public class SpringbootMgxApplication {
public static void main(String[] args) {
System.out.println("开始启动");
SpringApplication.run(SpringbootMgxApplication.class, args);
System.out.println("启动成功");
}
}
三、验证springboot整合mybatis是否成功
1、启动项目,端口号8080
2、使用postman调用接口
增:
改:
查:
删:
到此springboot整合mybatis成功。