注意
1、maven配置好镜像
2、jdk1.8
3、mysql
创建springboot项目
项目结构
需要的依赖pom
<?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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jack</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入所需要的数据库连接依赖mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--该依赖提供三个功能
1、对数据源的装配
2、提供一个JdbcTemplate简化使用
3、事务处理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--简化实体类-->
<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.22</version>
</dependency>
<!--进行单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!--该插件主要是maven之mybatis代码生成器-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
<!--以下操作用来扫描resources里面xml文件,默认只能扫描dao层里面-->
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
主启动类
package com.jack.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.jack.springbootmybatis.dao")
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
需要的实体类
public class Student {
private String stuNo;
private String stuName;
private Date stuBornDate;
private String stuSex;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo == null ? null : stuNo.trim();
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName == null ? null : stuName.trim();
}
public Date getStuBornDate() {
return stuBornDate;
}
public void setStuBornDate(Date stuBornDate) {
this.stuBornDate = stuBornDate;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex == null ? null : stuSex.trim();
}
@Override
public String toString() {
return "Student{" +
"stuNo='" + stuNo + '\'' +
", stuName='" + stuName + '\'' +
", stuBornDate=" + stuBornDate +
", stuSex='" + stuSex + '\'' +
'}';
}
}
对应数据层
package com.jack.springbootmybatis.dao;
import com.jack.springbootmybatis.pojo.Student;
import java.util.List;
public interface StudentMapper {
/**
* @description 根据主键删除
* @param stuNo 学号
* @return 受影响行数
*/
int deleteByPrimaryKey(String stuNo);
int insert(Student record);
int insertSelective(Student record);
/**
* @description 根据学号查询学生信息
* @param stuNo 学号
* @return 该学生信息
*/
Student selectByPrimaryKey(String stuNo);
/**
* @description 多条件查询学生信息
* @param student 学生信息
* @return 若是参数为null,全表查询,否则多条件查询
*/
List<Student> selectSelective(Student student);
/**
* @description 不同性别不同学生信息
* @param student
* @return
*/
List<Student> selectSelectiveSex(Student student);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
对应的视图层
package com.jack.springbootmybatis.controller;
import com.alibaba.fastjson.JSONObject;
import com.jack.springbootmybatis.dao.StudentMapper;
import com.jack.springbootmybatis.pojo.Student;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/student")
public class StudentController {
@Resource
private StudentMapper studentMapper ;
/**
* 通过@PathVariable占位符方式传参
*/
@RequestMapping("/selectByPrimaryKey/{stuNo}")
public Student selectByPrimaryKey(@PathVariable String stuNo){
return studentMapper.selectByPrimaryKey(stuNo);
}
/**
* 通过@RequestParam
*/
@GetMapping("/selectByPrimaryKey1")
public Student selectByPrimaryKey1(@RequestParam("s1") String stuNo){
return studentMapper.selectByPrimaryKey(stuNo);
}
@GetMapping("/selectByPrimaryKey2")
public Student selectByPrimaryKey2(@RequestBody String stuNo) {
// 使用fastjson解析json格式字符串为json对象
JSONObject jsonObject = JSONObject.parseObject(stuNo);
// 获取学号
String stuNo1 = jsonObject.getString("stuNo");
return studentMapper.selectByPrimaryKey(stuNo1);
}
@GetMapping("/selectSelective")
public List<Student> selectSelective(@RequestBody Student student){
return studentMapper.selectSelective(student);
}
@GetMapping("/selectSelectiveSex")
public List<Student> selectSelectiveSex(@RequestBody Student student){
return studentMapper.selectSelectiveSex(student);
}
}
对应的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.jack.springbootmybatis.dao.StudentMapper" >
<resultMap id="BaseResultMap" type="com.jack.springbootmybatis.pojo.Student" >
<id column="stu_no" property="stuNo" jdbcType="VARCHAR" />
<result column="stu_name" property="stuName" jdbcType="VARCHAR" />
<result column="stu_born_date" property="stuBornDate" jdbcType="DATE" />
<result column="stu_sex" property="stuSex" jdbcType="CHAR" />
</resultMap>
<sql id="Base_Column_List" >
stu_no, stu_name, stu_born_date, stu_sex
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from student
where stu_no = #{stuNo,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from student
where stu_no = #{stuNo,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.jack.springbootmybatis.pojo.Student" >
insert into student (stu_no, stu_name, stu_born_date,
stu_sex)
values (#{stuNo,jdbcType=VARCHAR}, #{stuName,jdbcType=VARCHAR}, #{stuBornDate,jdbcType=DATE},
#{stuSex,jdbcType=CHAR})
</insert>
<insert id="insertSelective" parameterType="com.jack.springbootmybatis.pojo.Student" >
insert into student
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="stuNo != null" >
stu_no,
</if>
<if test="stuName != null" >
stu_name,
</if>
<if test="stuBornDate != null" >
stu_born_date,
</if>
<if test="stuSex != null" >
stu_sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="stuNo != null" >
#{stuNo,jdbcType=VARCHAR},
</if>
<if test="stuName != null" >
#{stuName,jdbcType=VARCHAR},
</if>
<if test="stuBornDate != null" >
#{stuBornDate,jdbcType=DATE},
</if>
<if test="stuSex != null" >
#{stuSex,jdbcType=CHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.jack.springbootmybatis.pojo.Student" >
update student
<set >
<if test="stuName != null" >
stu_name = #{stuName,jdbcType=VARCHAR},
</if>
<if test="stuBornDate != null" >
stu_born_date = #{stuBornDate,jdbcType=DATE},
</if>
<if test="stuSex != null" >
stu_sex = #{stuSex,jdbcType=CHAR},
</if>
</set>
where stu_no = #{stuNo,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.jack.springbootmybatis.pojo.Student" >
update student
set stu_name = #{stuName,jdbcType=VARCHAR},
stu_born_date = #{stuBornDate,jdbcType=DATE},
stu_sex = #{stuSex,jdbcType=CHAR}
where stu_no = #{stuNo,jdbcType=VARCHAR}
</update>
<select id="selectSelective" resultMap="BaseResultMap" parameterType="com.jack.springbootmybatis.pojo.Student">
select
<include refid="Base_Column_List" />
from student
<where>
<if test="stuNo != null and stuNo != ''">
stu_no=${stuNo}
</if>
<if test="stuName != null and stuName != ''">
stu_name=${stuName}
</if>
<if test="stuBornDate != null and stuBornDate != ''">
stu_born_date=${stuBornDate}
</if>
<if test="stuSex != null and stuSex != ''">
stu_sex=${stuSex}
</if>
</where>
</select>
<select id="selectSelectiveSex" resultMap="BaseResultMap" parameterType="com.jack.springbootmybatis.pojo.Student">
select
<include refid="Base_Column_List" />
from student
<where>
<choose>
<when test="stuSex != null ">
stu_sex = #{stuSex}
</when> <when test="stuName != null ">
stu_name = #{stuName}
</when>
<otherwise>
stu_sex = "中"
</otherwise>
</choose>
</where>
</select>
</mapper>
数据库配置文件
server:
port: 8080 # 服务端口号
spring:
datasource:
username: root # mysql用户名
password: root # mysql密码
url: jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver # mysql8.0版本
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # 扫描mapper文件xml
type-aliases-package: com.jack.springbootmybatis.pojo # 别名映射
# 控制台显示SQL语句
logging:
level:
com:
jack:
springbootmybatis:
dao : debug