SpringBoot+MyBatis整合

步骤:

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

>
    >
        >org.springframework.boot>
        >spring-boot-starter-data-jdbc>
    >
    >
        >org.springframework.boot>
        >spring-boot-starter-jdbc>
    >
    >
        >org.springframework.boot>
        >spring-boot-starter-web>
    >
    >
        >org.mybatis.spring.boot>
        >mybatis-spring-boot-starter>
        >2.1.2>
    >


    >
        >mysql>
        >mysql-connector-java>
        >compile>
    >
    >
        >org.springframework.boot>
        >spring-boot-starter-test>
        >test>
        >
            >
                >org.junit.vintage>
                >junit-vintage-engine>
            >
        >
    >
>

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中,忘记了写返回值类型

你可能感兴趣的:(SpringBoot)