Springboot 整合MyBaits

一、创建一个新项目:

1.1 选择---spring innitializr。

1.2 输入项目名称 选择Java版本为8。

1-3 添加本项目中你会获取的依赖,一个✔代表着pom里会出现的坐标。

1-4  点击完成。

 1-5 项目创建成功后,检查pom里是否存在这两个坐标,如果没有可以手动添加或者重复以上步骤尝试。

<!--导入对应的starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

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

二、配置

 2-1 在application.yml配置数据库链接 注意属性后要空格才能识别变色

#<!--配置相关信息-->
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssmbuiLd?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
    usename: root
    password: 123456

2-2 创建数据库表

2-3 创建数据表对象层--domain-Books类(新手入门还没有用到注解)

 

 

package com.itheima.springboot_05_mybatis.damain;

public class Books {
    private int bookID;
    private  String bookName;
    private String bookCounts;
    private String detail;

    public int getBookID() {
        return bookID;
    }

    public void setBookID(int bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookCounts() {
        return bookCounts;
    }

    public void setBookCounts(String bookCounts) {
        this.bookCounts = bookCounts;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    @Override
    public String toString() {
        return "Books{" +
                "bookID=" + bookID +
                ", bookName='" + bookName + '\'' +
                ", bookCounts='" + bookCounts + '\'' +
                ", detail='" + detail + '\'' +
                '}';
    }
}

 2-4 创建dao层-BookDao类

 

package com.itheima.springboot_05_mybatis.dao;

import com.itheima.springboot_05_mybatis.damain.Books;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface BooksDao {
    @Select("select * from books where bookID= #{bookID}")
    public static Books getBookID(int bookID);

}

 三、测试类

package com.itheima.springboot_05_mybatis;

import com.itheima.springboot_05_mybatis.dao.BooksDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot05MybatisApplicationTests {

    @Autowired(required=false)
    private BooksDao booksDao;

    @Test
    void contextLoads() {
        System.out.println(BooksDao.getBookID(1));
    }

}

Spring Boot 是一个快速开发框架,MyBatis 是一款优秀的基于 Java 的持久层框架。在 Spring Boot 中使用 MyBatis 可以极大地提高开发效率和运行效率。 具体步骤如下: 1. 引入 MyBatisMyBatis-SpringBoot-Starter 依赖。 2. 配置数据源和 MyBatis。 3. 编写实体类和映射文件。 4. 编写 DAO 层接口和 SQL 语句。 5. 在 Service 层中调用 DAO 层接口。 6. 在 Controller 层中调用 Service 层方法,返回结果给前端。 示例代码: 1. 引入依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置数据源和 MyBatis: ``` spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 编写实体类和映射文件: 实体类: ``` public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 映射文件: ``` <?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.dao.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long"> select * from user where id = #{id} </select> </mapper> ``` 4. 编写 DAO 层接口和 SQL 语句: ``` public interface UserMapper { User selectByPrimaryKey(Long id); } ``` 5. 在 Service 层中调用 DAO 层接口: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectByPrimaryKey(id); } } ``` 6. 在 Controller 层中调用 Service 层方法,返回结果给前端: ``` @RestController public class UserController { @Autowired private UserService userService; @GetMapping(value = "/user/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值