springboot:整合Mybatis

整合MyBatis

    官方文档

    Maven仓库地址
在这里插入图片描述

整合测试

    目录结构
在这里插入图片描述
    注意:标注的地方和之前的写法不大一样

  1. 导入需要整合的 MyBatis 依赖
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
  1. 配置数据库连接信息(不变)
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 整合mybatis
mybatis.type-aliases-package=com.zheng.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
  1. 测试数据库是否连接成功!
package com.zheng;

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
class Springboot05MybatisApplicationTests {

    @Autowired
    DataSource dataSource;


    @Test
    void contextLoads() throws SQLException {

        System.out.println(dataSource.getClass());
        System.out.println(dataSource.getConnection());

    }

}

  1. 创建实体类,导入 Lombok偷懒神器!
    User.java
package com.zheng.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
 *   @author 浩然
 *   @email Haor1250@outlook.com
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;

}

  1. 创建User实体类对应的 Mapper 接口
    UserMapper.java
package com.zheng.mapper;

import com.zheng.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 *   @author 浩然
 *   @email Haor1250@outlook.com
 */
//这个注解表示了这是一个mybatis的mapper类:Dao
@Mapper
@Repository
public interface UserMapper {

    List<User> queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

  1. 对应的Mapper映射文件
        UserMapper.xml注意看上面的配置,我们的UserMapper文件放在了resources/mybatis/mapper文件夹下
<?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.zheng.mapper.UserMapper">

    <select id="queryUserList" resultType="User">
        select * from mybatis.user
    </select>
    
    <select id="queryUserById" resultType="User">
        select * from mybatis.user where id = #{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>

    <update id="updateUser" parameterType="User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>

</mapper>
  1. maven配置资源过滤问题
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>
  1. 编写对应mapper的controller 进行测试!
package com.zheng.controller;

import com.zheng.mapper.UserMapper;
import com.zheng.pojo.User;
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;

import java.util.List;

/*
 *   @author 浩然
 *   @email Haor1250@outlook.com
 */
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
        List<User> userList = userMapper.queryUserList();

        for (User user : userList) {
            System.out.println(user);
        }

        return userList;
    }

    // 查询全部部门
    @GetMapping("/queryUserById/{id}")
    public User getDepartment(@PathVariable("id") Integer id){
        User user = userMapper.queryUserById(id);
        System.out.println(user);
        return user;
    }

    //添加用户
    @GetMapping("/addUser")
    public String addUser(){
        userMapper.addUser(new User(4,"小丽","123123"));
        return "ok";
    }
    //修改用户
    @GetMapping("update")
    public String updateUser(){
        userMapper.updateUser(new User(4,"阿毛","123321"));
        return "ok";
    }
    //删除用户
    @GetMapping("/deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(4);
        return "ok";
    }
}

    启动项目访问进行测试!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值