Springboot与MyBatis的快速配置

本文介绍了如何在SpringBoot项目中使用MyBatis进行数据库配置,包括数据库连接信息、实体类定义、Repository接口及MapperXML映射,以及Controller层的RESTfulAPI实现。
摘要由CSDN通过智能技术生成

pom.xml

 <parent>
        <artifactId>spring-boot-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <dependencies>
        <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>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

application.yml配置数据库和myBatis映射

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mbtest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: hsp
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: com.entity

实体类:

import java.util.Date;
@Data
public class Student {
    private Long id;

    private String name;

    private Double score;

    private Date birthday;
}

repository

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

对应的映射:

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.repository.StudentRepository">

<!--    <resultMap id="StudentResultMap" type="com.example.model.Student">-->
<!--        <id property="id" column="id"/>-->
<!--        <result property="name" column="name"/>-->
<!--        <result property="score" column="score"/>-->
<!--        <result property="birthday" column="birthday"/>-->
<!--    </resultMap>-->

    <select id="findById" parameterType="java.lang.Long" resultType="Student">
        SELECT * FROM student WHERE id = #{id}
    </select>

    <select id="findAll" resultType="Student">
        SELECT * FROM student
    </select>

    <insert id="save" parameterType="Student">
        INSERT INTO student (name, score, birthday)
        VALUES (#{name}, #{score}, #{birthday})
    </insert>

    <update id="update" parameterType="Student">
        UPDATE student
        SET name = #{name}, score = #{score}, birthday = #{birthday}
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        DELETE FROM student WHERE id = #{id}
    </delete>

</mapper>

controller:

@RestController
public class StudentHandler {
    @Autowired
    private StudentRepository studentRepository;
    @GetMapping("/findAll")
    private List<Student> findAll(){
        return  studentRepository.findAll();
    }
    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id")Long id){
        return studentRepository.findById(id);
    }
    @PostMapping("/save")
    public void save(@RequestBody Student student){
        studentRepository.save(student);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.update(student);
    }
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id")Long id){
        studentRepository.deleteById(id);
    }
}

启动类

@SpringBootApplication
@MapperScan("com.repository")
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值