Spring Boot 整合Mybatis

一、注解方式实现增删改查

1.1 添加相关依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

1.2 插入数据时,页面出不来

  • 缺少相关的依赖
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  • 配置文件中修改driver-class-name
com.mysql.cj.jdbc.Driver
  • yml文件的名字改为
application.yml

1.3 数据的增删改查Controller层

@RestController
public class controller {
    @Autowired
    department dept;
    @GetMapping("/dept/{id}")
    public Department department(@PathVariable("id")Integer id){
        return dept.getDepId();
    }
    @GetMapping("/dept")
    public Department insertDept(Department department){
        dept.insertDept(department);
        return department;
    }
}

1.4 增删改查Mapper接口

@Mapper
public interface department {
    @Select("select * from department where id=#{id}")
    public Department getDepId();

    @Delete("delete from department where id=#{id}")
    public int deleteDepId(Integer id);

    @Insert("insert into department(departmentName) values(#{departmentName})")
    public int insertDept(Department department);

    @Update("update department set departmentName=#{departmentName} where id=#{id}")
    public int updatedep(Department Department);
}

1.5 页面显示主键的值

//是不是使用自动生成的主键,主键是哪个
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);

1.6 批量扫描mapper文件

@MapperScan(value = "com.xx.mybatis.demo.mapper")

二、配置文件的方式进行增删改查

2.1 自动执行sql语句

  • sql文件放在类路径下
  • yml中需加入配置
schema:
  - classpath:sql/Department.sql
  - classpath:sql/employee.sql

2.2 全局配置文件

<?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>

</configuration>

2.3 接口对应配置文件

<?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.xx.mybatis.demo.mapper.EmployeeMapper">
   <select id="getEmpId" resultType="com.xx.mybatis.demo.Bean.Employee">
       select * from employee where id=#{id}
   </select>
    <insert id="InsertEmp">
        insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

2.4 配置文件中相关配置

mybatis:
  #全局配置文件的位置
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

2.5 逻辑处理

@RestController
public class controller {
    @Autowired
    department dept;
    @GetMapping("/dept/{id}")
    public Department department(@PathVariable("id")Integer id){
        return dept.getDepId();
    }
}

2.6 Mapper接口

@Mapper
public interface EmployeeMapper {
    public Employee getEmpId(Integer id);
    public void InsertEmp(Employee employee);
}

三、spring boot数据库配置

3.1 添加依赖

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
</dependency>
  • 配置文件中配置内容
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=123456
# JDBC Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JDBC URL
spring.datasource.url=jdbc:mysql://localhost:3306/db_student?serverTimezone=Asia/Shanghai
#spring.datasource.url=jdbc:mysql://localhost:3306/db_student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true



spring:
  datasource:
    password: 123456
    username: root
    url: jdbc:mysql://localhost:3306/db191125_guli_edu
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  • com.mysql.cj.jdbc.Driver是新版本的写法,版本5是com.mysql.jdbc.Driver

3.2 测试数据库连接

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.SQLException;

@SpringBootTest
public class testDataSource {
    @Autowired
    DataSource dataSource;
    @Test
    public void testMysqlConnect() throws SQLException {
        System.out.println(dataSource.getConnection());
    }
}

3.3 classPath下没有xml

  • pom中添加
<build>
        <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • build->rebuild project
  • 重启项目

四、xml实现crud

4.1 yml配置

spring:
  datasource:
    password: 123456
    username: root
    url: jdbc:mysql://localhost:3306/db191125_guli_edu
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:com/example/demo/mapper/*.xml

4.2 启动类添加

@MapperScan("com.example.demo.mapper")

4.3 pom添加

<build>
        <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值