一、概述
使用IDEA社区+SpringBoot+MyBatisPlus+MySQL实现数据的保存修改与查询。主要记录一下踩坑过程。
注意事项:
1.社区版IDEA并不能直接创建SpringBoot工程,所以我采用的方式是在Spring官网上,让其帮助我创建一个,创建好后,直接下载。
//参考案例
https://blog.csdn.net/fen_fen/article/details/123464256
//以下是官网的创建及快速开始步骤
https://start.spring.io/
https://spring.io/quickstart
2.数据库安装的是5.5
3.maven安装的是3.8.8,安装完成后配置记得配置一下环境变量
4.在springboot官网下载好demo项目后,用社区版IDEA打开,打开后的配置类似下面的目录结构
a.在demo上下载下来的application.properties改为application.yml。这里面用于配置应用信息,如:端口号、数据库连接配置、mybatis配置、文件上传服务配置等
spring:
# mysql连接配置与mybaits一致
datasource:
# 数据库地址、端口、数据库名
#jdbc:mysql://ip地址:端口号/数据库名称?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
username: root #数据库账号
password: 123456 #数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: mapper/*.xml #这个配置是用来连接mapper接口和mapper配置类
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #运行日志
map-underscore-to-camel-case: true #驼峰转换
type-aliases-package: com.tony.demo.model #实体扫描包
b.pom.xml主要存放的是项目依赖包的信息,如果要新增或者删除依赖包,都是在这里面进行。下面是简单示例
c.DemoApplication是入口,如下图所示,里面有一个main函数
d.在目录结构中可以看到controller、mapper以及service包。这三个包是有关联的。
StudengController:是api请求的方法路径入口
StudentMapper:
StudentServiceImpl:
model中的实体和数据库中的表做映射:
总结一下controller、service、mapper:例如:用户调用登录接口api/login,首先会找到controller,controller再找到service,service再找到对应的mapper,最终用mapper的增啥改查方法,进行数据库表的操作
二、代码实现
代码实现不做过多解释,直接把完整代码贴出来方便作对比:
1.StudentController.java
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public void addStudent(@RequestBody Student student) {
studentService.saveStudent(student);
}
@RequestMapping(value = "addOrUpdateStudent",method = RequestMethod.POST)
public void saveOrUpdateStudent(@RequestBody Student student){
studentService.saveOrUpdate(student);
}
/**
* 根据指定的id查询用户
*
* @param s_id 用户id
* @return
*/
@RequestMapping(value = "/queryStudentById/{s_id}", method = RequestMethod.GET)
public Student queryStudentById(@PathVariable("s_id") String s_id) {
return studentService.getStudentById(s_id);
}
/**
* 根据条件查询用户列表
*
* @param student 指定用户
* @return
*/
@RequestMapping(value = "/getList", method = RequestMethod.POST)
public List<Student> getList(@RequestBody Student student) {
return studentService.getList(student);
}
/**
* 根据用户名获取用户列表
*
* @param name 用户名
* @return
*/
@RequestMapping(value = "/getStudentsByName", method = RequestMethod.GET)
public List<Student> getStudentsByName(@RequestParam("s_name") String name) {
return studentService.getStudentByName(name);
}
}
2.StudentServiceImpl.java、StudentService.java
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
@Autowired
private StudentMapper studentMapper;
/**
* 添加学生
* @param student 学生
*/
@Transactional
@Override
public void saveStudent(Student student) {
// studentMapper.addStudent(student);
studentMapper.insert(student);
}
@Transactional
@Override
public List<Student> getList(Student student) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
if (student.getSId() != null) {
wrapper.eq("s_id", student.getSId());
}
if (student.getSName() != null && !Objects.equals(student.getSName(), "")) {
wrapper.like("s_name", student.getSName());
}
return studentMapper.selectList(wrapper);
}
@Transactional
@Override
public List<Student> getStudentByName(String s_name) {
return studentMapper.getUserByName(s_name);
}
@Transactional
@Override
public Student getStudentById(String s_id) {
Student student = studentMapper.getStudentById(s_id);
if (student != null) {
System.out.println("不为空");
} else {
System.out.println("为空");
}
return student;
}
}
public interface StudentService extends IService<Student> {
/**
* 添加学生
* @param student 学生
*/
void saveStudent(Student student);
/**
* 查询条件查询
* @param student 学生
* @return
*/
List<Student> getList(Student student);
/**
* 根据学生名称查询学生列表
* @param s_name
* @return
*/
List<Student> getStudentByName(String s_name);
/**
* 根据学生id查询学生
* @param s_id
* @return
*/
Student getStudentById(String s_id);
}
3.StudentMaper.java
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
// @Select("select * from student where s_name=#{s_name}")
List<Student> getUserByName(@Param("s_name") String s_name);
// @Select("select * from student where s_id=#{s_id}")
Student getStudentById(@Param("s_id") String s_id);
// void addStudent(@Param("s_id,s_name,s_sex,s_age,s_description")String s_id,String s_name,String s_sex,String s_age,String s_description);
void addStudent(@Param("student") Student student);
}
4.StudentMapper.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.tony.demo.mapper.StudentMapper">
<resultMap id="studentResultMap" type="com.tony.demo.model.Student">
<id property="sId" jdbcType="VARCHAR" column="s_id" />
<result property="sName" jdbcType="VARCHAR" column="s_name" />
<result property="sAge" jdbcType="VARCHAR" column="s_age" />
<result property="sSex" jdbcType="VARCHAR" column="s_sex" />
<result property="sDescription" jdbcType="VARCHAR" column="s_description" />
</resultMap>
<insert id="addStudent" parameterType="com.tony.demo.model.Student">
insert into student values(student.sId,student.sName,student.sSex,student.sAge,student.sDescription)
</insert>
<select id="getUserByName" parameterType="java.lang.String" resultType="com.tony.demo.model.Student">
select * from student where s_name=#{s_name}
</select>
<select id="getStudentById" resultType="com.tony.demo.model.Student">
select * from student where s_id=#{s_id}
</select>
</mapper>
5.Student.java
@Data
@Getter
@Setter
@ToString
@TableName("student")
public class Student {
@TableId(value = "s_id")
private String sId= UUID.randomUUID().toString();
@TableField(value = "s_name")
private String sName;
@TableField(value = "s_sex")
private String sSex;
@TableField(value = "s_age")
private String sAge;
@TableField(value = "s_description")
private String sDescription;
}
6.DemoApplication
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}