码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot集成MyBatis

时间:2020-01-27 00:07:19      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:local   can   public   tostring   cat   实现类   sources   cal   管理   

1.创建Maven工程,pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Mybatis和Spring整合依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--Mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2.创建数据表并编写对应实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable {
    private Long id;
    private String name;
    private Integer age;
}

3.创建StudentRepository接口

public interface StudentRepository {
    public List<Student> findAll();
    public Student findAllById(Long id);
    public void save(Student student);
    public void update(Student student);
    public void deleteById(Long id);
}

4.在resources/mapping路径下创建StudentRepository接口对应的Mapper.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.sbmybatis.repository.StudentRepository">

    <select id="findAll" resultType="com.sbmybatis.entity.Student">
        select * from student;
    </select>

    <select id="findById" parameterType="java.lang.Long" resultType="com.sbmybatis.entity.Student">
        select * from student where id = #{id};
    </select>

    <insert id="save" parameterType="com.sbmybatis.entity.Student">
        insert into student(id,name,age) values(#{id},#{name}.#{age});
    </insert>
    
    <insert id="update" parameterType="com.sbmybatis.entity.Student">
        update student set name=#{name},age=#{age} where id=#{id}
    </insert>

    <delete id="deleteById" parameterType="java.lang.Long">
        delete student where id = #{id}
    </delete>
</mapper>

5.创建StudentHandler,将StudentRepository注入进去

@RestController
public class StudentHandler {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping(value = "/findAll")
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @GetMapping(value = "/findById/{id}")
    public Student findById(@PathVariable("id") Long id) {
        return studentRepository.findById(id);
    }

    @PostMapping(value = "/save")
    public int save(@RequestBody Student student) {
        return studentRepository.save(student);
    }

    @PutMapping(value = "/update")
    public int update(@RequestBody Student student) {
        return studentRepository.update(student);
    }

    @DeleteMapping(value = "/deleteById/{id}")
    public int save(@PathVariable("id") Long id) {
        return studentRepository.deleteById(id);
    }
    
}

6.配置Springboot(application.yml)

server:
  port: 8888

#数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
    username: root
    password: zxcv
#Mapper文件路径
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  #设置别名
  type-aliases-package: com.sbmybatis.entity

8.创建Application

@SpringBootApplication
/**
 * 因为repository不是Spring管理的,所以需要将他扫描添加到Ioc容器中
 * 添加@MapperScan(“com.sbmybatis.repository”)注解以后,com.sbmybatis.repository包下面的接口类,在编译之后都会生成相应的实现类
 */
@MapperScan(value = "com.sbmybatis.repository")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

SpringBoot集成MyBatis

标签:local   can   public   tostring   cat   实现类   sources   cal   管理   

原文地址:https://www.cnblogs.com/leizzige/p/12235035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!