从JDBC到Mybatis

本文介绍了MyBatis作为一款优秀的持久层框架,其主要优点在于减少了JDBC的繁琐代码,提供了XML或注解方式配置SQL,支持动态SQL,实现了对象与数据库记录的映射。通过一个简单的MyBatis项目实例,展示了从创建数据库配置到编写Mapper接口和XML映射文件,再到Service和Controller层的实现,最后进行测试的过程。
摘要由CSDN通过智能技术生成

一、MyBatis介绍

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
MyBatis的优点

与JDBC相比,减少了50%以上的代码量,消除了JDBC大量冗余的代码,不需要手动开关连接。
很好的与各种数据库兼容,开发人员不需要考虑数据库的差异性。
MyBatis相当灵活,不会对应用程序或者数据库的现有设计强加任何影响,SQL写在XML里,从程序代码中彻底分离,解除sql与程序代码的耦合,便于统一管理和优化,并可重用。
提供XML标签,支持编写动态SQL语句。
提供映射标签,支持对象与数据库的ORM字段关系映射。
提供对象关系映射标签,支持对象关系组建维护。

JDBC编程的步骤:
1、 加载数据库驱动
2、 创建并获取数据库链接
3、 创建jdbc statement对象
4、 设置sql语句
5、 设置sql语句中的参数(使用preparedStatement)
6、 通过statement执行sql并获取结果
7、 对sql执行结果进行解析处理
8、 释放资源(resultSet、preparedstatement、connection

二、Mybatis 测试

  1. 新建项目
    在这里插入图片描述配置数据库
    在这里插入图片描述

根据自己的数据库名、用户名和密码,修改
application.properties:

server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

在com.example.demo下创建四个package(controller、entity、mapper、service):
在这里插入图片描述
在这里插入图片描述

package com.example.demo.entity;

public class Student {
    private int stu_id;
    private String stu_name;
    private int stu_age;

    public int getStu_id() {
        return stu_id;
    }

    public void setStu_id(int stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public int getStu_age() {
        return stu_age;
    }

    public void setStu_age(int stu_age) {
        this.stu_age = stu_age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuID=" + stu_id +
                ", stuName='" + stu_name + "'" +
                ", stuAge=" + stu_age +
                "}";
    }
}

在mapper下创建接口StuMapper:
在这里插入图片描述

package com.example.demo.mapper;
import com.example.demo.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StuMapper {
    public List<Student> findAllStudent();
    public List<Student> findStudentByStudentID(int stuID);
}

在resources下创建目录mapper:
在这里插入图片描述
在刚刚创建的resources下的mapper下创建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">
<mapper namespace="com.example.demo.mapper.StuMapper">
    <resultMap id="result" type="com.example.demo.entity.Student">
        <result column="stu_id" jdbcType="INTEGER" property="stu_id" />
        <result column="stu_name" jdbcType="VARCHAR" property="stu_name" />
        <result column="stu_age" jdbcType="INTEGER" property="stu_age" />
    </resultMap>

    <select id="findAllStudent" resultType="com.example.demo.entity.Student">
        select * from student;
    </select>

    <select id="findStudentByStudentID" resultType="com.example.demo.entity.Student">
        select * from student where stu_id=#{stuID};
    </select>
</mapper>

在包Service下创建类StudentService:
在这里插入图片描述

package com.example.demo.service;


import com.example.demo.entity.Student;
import com.example.demo.mapper.StuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired(required = false)
    public StuMapper stuMapper;
    public List<Student> findAllStudent(){
        return stuMapper.findAllStudent();
    }
    public List<Student> findStudentByStuID(int stuID){
        return stuMapper.findStudentByStudentID(stuID);
    }
}

在包controller下创建类StudentController:
在这里插入图片描述

package com.example.demo.controller;


import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/getAllStudent")
    public List<Student> findAll(){
        return studentService.findAllStudent();
    }

    @RequestMapping("/getStudentByStudentID/{stuID}")
    public List<Student> findStudentByStudentID(@PathVariable int stuID){
        return studentService.findStudentByStuID(stuID);
    }


}

整体结构如下
在这里插入图片描述
运行代码
在这里插入图片描述打开http://localhost:8080/student/getAllStudent
在这里插入图片描述测试成功。

参考

https://blog.csdn.net/weixin_56102526/article/details/120825699?spm=1001.2014.3001.5501

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值