进入Maven中央仓库选择自己所需要的依赖,maven仓库地址:Maven Central
完整Maven依赖如下:
4.0.0
com.mgx
springboot-mgx
0.0.1-SNAPSHOT
springboot-mgx
springboot项目整合
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
1.8
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
mysql
mysql-connector-java
8.0.27
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-maven-plugin
2.1.4.RELEASE
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
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='用户基础信息表';
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
id, `name`, phone, age, sex, address, income
delete from t_info_user
where id = #{id,jdbcType=BIGINT}
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 into t_info_user
`name`,
phone,
age,
sex,
address,
income,
#{name,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{sex,jdbcType=INTEGER},
#{address,jdbcType=VARCHAR},
#{income,jdbcType=DECIMAL},
update t_info_user
`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 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}
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成功。