Springboot(十)——整合mybatis

整合mybatis

整合mybatis

一、新建springboot项目,引入以下三个依赖

image

二、导入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>

三、编写properties配置文件,连接数据库

# 应用名称
spring.application.name=springboot-05-mybatis
# 应用服务 WEB 访问端口
server.port=8080
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456

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

@SpringBootTest
class Springboot05MybatisApplicationTests {

    @Autowired
    DataSource dataSource;

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

}

image

五、连接数据库

image

六、创建实体类

package com.study.pojo;

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

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

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

package com.study.mapper;

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

import java.util.List;

@Mapper
@Repository
public interface UserMapper {

    List<User> queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

八、在resource创建mybatis/mapper目录

image

九、编辑application.properties整合mybatis

#整合mybatis
#别名
mybatis.type-aliases-package=com.study.pojo
#地址
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

十、在resource/mybatis/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.study.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>

十一、编写controller层进行测试

package com.study.controller;

import com.study.mapper.UserMapper;
import com.study.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

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

    @GetMapping("/quid")
    public User queryUserById(){
        User user = userMapper.queryUserById(1);
        return user;
    }

    @GetMapping("/au")
    public String addUser(){
        userMapper.addUser(new User(7, "xxx", "123456"));
        return "ok";
    }

    @GetMapping("/uu")
    public String updateUser(){
        userMapper.updateUser(new User(5,"啊毛","123456"));
        return "ok";
    }
    @GetMapping("/du")
    public String deleteUser(){
        userMapper.deleteUser(5);
        return "ok";
    }
}

测试完成!

总结:springboot整合mybatis步骤

  • 1、导入依赖
  • 2、编写配置文件
  • 3、配置mybatis
  • 4、编写sql
  • 5、service层调dao层
  • 6、controller层调service层
posted @ 2021-07-02 00:32  转身刹那的潇洒  阅读(72)  评论(0编辑  收藏  举报