• SpringBoot集成MyBatis


    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: you password
    #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);
        }
    }
    
  • 相关阅读:
    Debian如何永久添加静态路由
    一句话换个说法的软件
    高级程序员简历,技术总监喜欢什么简历?
    CP936实际上是GBK,编码问题再次让我熬夜
    句子说法转换的软件,基于AI技术
    输入关键词自动生成文章(2020年人工智能写作)
    自动写文章的智能软件(基于AI写作)
    python调用接口,python接收post请求接口(附完整代码)
    人工智能是铁饭碗还是铁坑,看看人工智能博士怎么说
    远离外包公司,就是远离码农的血汗工厂
  • 原文地址:https://www.cnblogs.com/leizzige/p/12235035.html
Copyright © 2020-2023  润新知