SpringBoot+MyBatis整合

步骤:

1、导入JDBC、MySQL、MyBatis的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>


    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

2、编写实体类

public class User {
    private int id;
    private String name;
    private int age;

3、编写DAO层接口

package springboot06datamysql.Mapper;


import org.springframework.stereotype.Repository;
import springboot06datamysql.Entity.User;


@Repository
public interface UserMapper {


    //增加一个成员
    public void insertUser(User user);


    //删除一个成员
    public void deleteUser(int id);


    //查找一个成员
    public User selectUser(int id);
}

4、在mapper.xml中匹配DAO的方法

<?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="springboot06datamysql.Mapper.UserMapper"//DAO接口的全类名>
    <insert id="insertUser" parameterType="springboot06datamysql.Entity.User" >
        insert into employee values(#{id},#{name},#{age})
    </insert>

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

    <select id="selectUser" parameterType="int" resultType="springboot06datamysql.Entity.User">
        select * from employee where id = #{id}
    </select>
</mapper>

5、编写service层

package springboot06datamysql.Service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot06datamysql.Entity.User;
import springboot06datamysql.Mapper.UserMapper;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public void insert(User user){
        userMapper.insertUser(user);
    }
    public void delete(int id){
        userMapper.deleteUser(id);
    }
    public User select(int id){
        User user = userMapper.selectUser(id);
        return user;
    }
}

6、编写controller层

package springboot06datamysql.Controller;


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 springboot06datamysql.Entity.User;
import springboot06datamysql.Service.UserService;


@RestController
public class controller {

    @Autowired
    UserService userService;

    @GetMapping("/insert")
    public void insert(User user){
        userService.insert(user);
    }

    @GetMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        userService.delete(id);
    }

    @GetMapping("/selete/{id}")
    public User selete(@PathVariable("id") int id){
        User user = userService.select(id);
        return user;
    }
}

7、编写响应的配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://xxx?serverTimezone=UTC
    username: xxx
    password: xxx
mybatis:
  mapper-locations: classpath:mapping/mapper.xml

8、在主配置类添加Mapper扫描

@MapperScan(value = "springboot06datamysql")
@SpringBootApplication
public class SpringBoot06DataMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBoot06DataMysqlApplication.class, args);
    }
}

犯的两个错误

1、没有在主配置类上加Mapper的扫描,导致UserMapper注入失败
2、在select方法对应的mapper中,忘记了写返回值类型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值