一、项目
项目环境搭建SpringBoot+Maven+Mysql
pom.xml依赖配置:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.jna</groupId>
<artifactId>jna-sdk</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>me.zzp</groupId>
<artifactId>district</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<?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.example.springboot_test.mapper.UsersMapper"><!--对应的是相关的mapper层-->
<!--别名,数据库字段与实体类里面的对应,column是数据库,property是实体类
注:resultMap只有select查询时能引用-->
<resultMap id="result_Map" type="com.example.springboot_test.entity.Users">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="age" column="age"></result>
</resultMap>
<select id="getId" parameterType="com.example.springboot_test.entity.Users" resultMap="result_Map">
select * from user where id=#{id}
</select>
<!--前面的字段都是数据库里面的字段,#{}里面时实体类里面的字段-->
<update id="updateById" parameterType="com.example.springboot_test.entity.Users" >
update user
set <if test="userName !=null ">
user_name=#{userName},
</if>
<if test="age !=null">
age=#{age}
</if>
where id=#{id}
</update>
<insert id="insertById" parameterType="com.example.springboot_test.entity.Users" >
insert
into user
(<if test="id !=null">
id,
</if>
<if test="userName !=null">
user_name,
</if>
<if test="age !=null">
age
</if>
)
values
(<if test="id !=null">
#{id},
</if>
<if test="userName !=null">
#{userName},
</if>
<if test="age !=null">
#{age}
</if>
)
</insert>
<delete id="deleteById" parameterType="com.example.springboot_test.entity.Users">
delete from user where id=#{id}
</delete>
</mapper>
UsersMapper
package com.example.springboot_test.mapper;
import com.example.springboot_test.entity.Users;
import org.springframework.stereotype.Repository;
@Repository
public interface UsersMapper {
//查
Users getId(Integer id);
//改
int updateById(Users users);
//增
int insertById(Users users);
//删
int deleteById(Integer id);
}
UsersService
package com.example.springboot_test.service;
import com.example.springboot_test.entity.Users;
import org.springframework.data.relational.core.sql.In;
public interface UsersService {
Users getId(Integer id);
int updateById (Users users);
int insertById(Users users);
int deleteById(Integer id);
}
UsersServiceImpl
package com.example.springboot_test.service.impl;
import com.example.springboot_test.entity.Users;
import com.example.springboot_test.mapper.UsersMapper;
import com.example.springboot_test.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
UsersMapper usersMapper;
@Override
public Users getId(Integer id) {
return usersMapper.getId(id);
}
@Override
public int updateById(Users users) {
return usersMapper.updateById(users) ;
}
@Override
public int insertById(Users users) {
int Users=usersMapper.insertById(users);
return Users;
}
@Override
public int deleteById(Integer id) {
return usersMapper.deleteById(id);
}
}
UsersController
package com.example.springboot_test.controller;
import com.example.springboot_test.entity.Users;
import com.example.springboot_test.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class UsersController {
@Autowired
UsersService usersService;
//查询信息
@GetMapping("/getId")
public String getId(@RequestParam Integer id){
return usersService.getId(id).toString();
}
//修改信息
@GetMapping("/updateById")
public String updateById(Users users) {
int result = usersService.updateById(users);
if (result >= 1) {
return "修改成功";
} else
return "修改失败";
}
//增加信息
@GetMapping ("/insertById")
public int insertById(Users users) {
int insert=usersService.insertById(users);
return insert;
}
//删除信息
@GetMapping("/deleteById")
public String deleteById(@RequestParam Integer id){
int result=usersService.deleteById(id);
if(result>=1){
return "删除成功";
}else{
return "删除失败";
}
}
}
Users
package com.example.springboot_test.entity;
import lombok.Data;
@Data
public class Users {
private Integer id;
private String userName;
private Integer age;
}
在浏览器上输入
//根据id删除信息
http://localhost:8089/deleteById?id=6
//根据id查询信息
http://localhost:8089/getId?id=6
//根据id修改信息
http://localhost:8089/updateById?id=6&userName=张三&age=21
//插入信息
http://localhost:8089/insertById?id=10&userName=张三&age=21