JAVA SpringBoot-13:整合MyBatis

整合MyBatis

  • M:数据业务
  • V:交接
  • C:HTML

官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.4
在这里插入图片描述


准备工作

  • 1.导入依赖
  • 2.配置文件
  • 3.mybatis配置
  • 4.编写SQL
  • 5.业务层调用dao层
  • 6.controller调用service层

创建项目springboot-06-mybatis ,导入依赖Spring WebJDBC APIMysql Driver

在这里插入图片描述
在这里插入图片描述


整合测试

1、导入 MyBatis 所需要的依赖

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

官方的依赖,以spring开头
在这里插入图片描述

2、配置数据库连接信息(不变)

spring:
  datasource:
    username: root
    password: root
    #?serverTimezone=UTC解决时区的报错
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

3、测试数据库是否连接成功!

@SpringBootTest
class Springboot06MybatisApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        System.out.println(dataSource.getConnection());
    }
}

连接成功:
在这里插入图片描述
DIEA绑定mysql:
在这里插入图片描述

3、创建实体类

User.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String pwd;
}

4、创建对应的 Mapper 接口

创建mapper目录以及对应的 Mapper 接口

//@Mapper : 表示本类是一个 MyBatis 的 Mapper
@Mapper
@Repository
public interface UserMapper {

    // 获取所有部门信息
    List<User> queryUserList();

    // 通过id获得部门
    User queryUserById(Integer id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

5.在application.yml中绑定xml

# 整合mybatis
mybatis:
  type-aliases-package: com.cy.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

6、对应的Mapper映射文件

UserMapper.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.cy.mapper.UserMapper">
    <select id="queryUserList" resultType="User">
        select * from user
    </select>

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

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

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

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

</mapper>

7、编写部门的 UserController 进行测试!

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //查询所有用户
    @GetMapping("/query")
    public List<User> queryUserList(){
        List<User> userList=userMapper.queryUserList();
        for (User user : userList){
            System.out.println(user);
        }
        return userList;
    }

    //添加一个用户
    @GetMapping("/add")
    public String addUser(){
        userMapper.addUser(new User(5,"张三","456789"));
        return "添加成功";
    }

    //修改一个用户
    @GetMapping("/update")
    public String updateUser(){
        userMapper.updateUser(new User(5,"李四","159753"));
        return "修改成功";
    }

    //根据id删除用户
    @GetMapping("/delete")
    public String deleteUser(){
        userMapper.deleteUser(5);
        return "删除成功";
    }
}

启动项目访问进行测试!

  • http://localhost:8080/query
  • http://localhost:8080/add
  • http://localhost:8080/update
  • http://localhost:8080/delete

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值