SpringBoot学习之路(X3)- 整合MyBatis

应用创建

1. 还是使用Spring initalizr创建应用

在这里插入图片描述
在这里插入图片描述

还需要选中web模块
在这里插入图片描述
应用创建完成,现在查看pom文件中依赖:

<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>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>

2. 图表查看mybatis-spring-boot-starter引用依赖

在这里插入图片描述

查看mybatis-spring-boot-starter又依赖了那些:
在这里插入图片描述

应用配置

1)、配置druid数据源

查看上一篇章SpringBoot学习之路(X2)- JDBC数据访问、自动配置原理、druid数据源配置

2)、创建数据库、表

这里使用本地(localhost)的student学生表
在这里插入图片描述

3)、创建javaBean

在这里插入图片描述

package com.yhhy.springboot.bean;
import java.util.Date;
public class Student {
    private Integer id;
    private String  name;
    private String pwd;
    private String md5;
    private Date date;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getMd5() {
        return md5;
    }
    public void setMd5(String md5) {
        this.md5 = md5;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
}

4)、创建Controller

package com.yhhy.springboot.controller;
import com.yhhy.springboot.bean.Student;
import com.yhhy.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {

    //这里就不写实现类了
    @Autowired
    StudentMapper studentMapper;
    //PathVariable 取值
    @GetMapping("/student/{id}")
    public Student getStudent(@PathVariable("id") Integer id){
        return studentMapper.getStudentById(id);
    }
    @GetMapping("/student")
    public Student insertStudent(Student student){
        studentMapper.insertStudent(student);
        return student;
    }
}

在这里插入图片描述

这里的日期使用http参数传送有点问题,之后再修改

5)、创建 Mapper

1)、注解版 - Mapper
package com.yhhy.springboot.mapper;
import com.yhhy.springboot.bean.Student;
import org.apache.ibatis.annotations.*;
@Mapper
public interface StudentMapper {
    /**
     * 查询一个学生
     * @param id
     * @return
     */
    @Select("select * from student where id = #{id}")
    public Student getStudentById(Integer id);

    /**
     * 删除
     * @param id
     * @return 返回受影响的行数
     */
    @Delete("delete from student where id=#{id}")
    public Integer delStudentById(Integer id);

    /**
     * 添加
     * @param student
     * @return
     */
    //使用自动生成的主键,并指定那个属性,用于添加记录后取主键id所用
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into student(name,pwd,md5,date) values(#{name},#{pwd},#{md5},#{date})")
    public Integer insertStudent(Student student);

    @Update("update student set  name=#{name},pwd=#{pwd},md5=#{md5},date=#{date} where id = #{id}")
    public Integer updaeStudent(Student student);
}
2)、配置文件版 - Mapper

无论是注解版还是配置文件版,都需要将接口扫描到才行

1. 创建接口
package com.yhhy.springboot.mapper;

import com.yhhy.springboot.bean.Student;

//@Mapper或者@MapperScan将接口扫描装配到容器中
//这里的启动类已经标注了扫描
public interface Student02Mapper {

    public Student getStudentById(Integer id);

    public void insertStu(Student student);
}

2. 创建mapper.xml配置文件

这里在resources下创建:
在这里插入图片描述
快速配置mybatis中的配置文件:
到官方查看快速配置相关文档

1)、StuMapper.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">
<!-- 跟接口绑定,把接口的全类名复制下来,IDEA右键 Copy Reference-->
<mapper namespace="com.yhhy.springboot.mapper.Student02Mapper">

    <!-- 使用ID就是方法名称 -->
    <select id="getStudentById" resultType="com.yhhy.springboot.bean.Student">
        SELECT * FROM student WHERE id=#{id}
    </select>

    <insert id="insertStu">
        INSERT INTO student(stuName,pwd,md5) VALUES (#{stuName},#{pwd},#{md5})
    </insert>
</mapper>
2)、mybatis-config.xml 配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
3)、yml 配置(得让程序知道myBatis存在)
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml #指定全局配置文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml  #指定sql映射文件的位置

在这里插入图片描述

4)、Controller
package com.yhhy.springboot.controller;
import com.yhhy.springboot.bean.Student;
import com.yhhy.springboot.mapper.Student02Mapper;
import com.yhhy.springboot.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {

    @Autowired
    Student02Mapper student02Mapper;

    /**
     * 查询一个学生
     * PathVariable  获取传送的id值
     */
    @GetMapping("/stu/{id}")
    public Student getStu(@PathVariable("id") Integer id){
        return student02Mapper.getStudentById(id);
    }
}
5)、测试结果

在这里插入图片描述
数据库中的字段为stu_name,而实体类为stuName,是因为我们在mybatis-config.xml中配置了:mapUnderscoreToCamelCase

 <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
 </settings>

也可以查看官方文档中的配置项设置
http://www.mybatis.org/mybatis-3/configuration.html#settings
在这里插入图片描述
在这里插入图片描述
可以看到注解版和配置文件版是可以混合使用的:
在这里插入图片描述

3)、扫描mapper包下文件,省略各个mapper接口上的@Mapper注解

在启动类上添加扫描:
@MapperScan(value = “com.yhhy.springboot.mapper”)
在这里插入图片描述

注释掉mapper上的注解@Mapper,测试结果依然可以;

6)、自定义MyBatis的配置规则

例如:
在mysql中的字段为stu_name ,而实体类中的属性为stuName,怎么才能映射上

1)、创建配置类,重写ConfigurationCustomizer接口的customize方法
package com.yhhy.springboot.config;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;

//此注解使用的是全类名,因为上面已经导入了org.apache.ibatis.session.Configuration
@org.springframework.context.annotation.Configuration
public class MyBatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(Configuration configuration) {
                //自定义的驼峰命名规则
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}
2)、结果测试

在这里插入图片描述

可以映射上;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值