springboot:整合Mybatis

整合MyBatis

    官方文档

    Maven仓库地址
springboot:整合Mybatis_第1张图片

整合测试

    目录结构
springboot:整合Mybatis_第2张图片
    注意:标注的地方和之前的写法不大一样

  1. 导入需要整合的 MyBatis 依赖
<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>2.1.1version>
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 [email protected]
 */
@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 [email protected]
 */
//这个注解表示了这是一个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文件夹下



<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/javadirectory>
        <includes>
            <include>**/*.xmlinclude>
        includes>
        <filtering>truefiltering>
    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 [email protected]
 */
@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";
    }
}

    启动项目访问进行测试!

你可能感兴趣的:(Spring,Boot)