springboot与mybatis的整合

导读:本篇文章讲解 springboot与mybatis的整合,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.3

image-20200728083023851

整合测试

  1. 导入 MyBatis 所需要的依赖

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.3</version>
    </dependency>
    
  2. 配置数据库连接信息

    	spring.datasource.username=root
    	spring.datasource.password=123456
    	spring.datasource.url=jdbc:mysql://localhost:3306/user?serverTimeZone=UTC&useUnicode=true&characterEncoding=utf-8
    	spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    	#与mybatis相关
    	mybatis.type-aliases-package=com.vincent.pojo
    	mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    
    
  3. 测试数据库是否连接成功

    package com.vincent;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.test.context.SpringBootTest;
    import javax.sql.DataSource;
    import java.sql.SQLException;
    
    @SpringBootTest
    class Test01ApplicationTests {
        @Autowired
        @Qualifier("dataSource")
    	private DataSource dataSource;
    
    
    	@Test
    	void contextLoads() throws SQLException {
            System.out.println(dataSource.getClass());
            System.out.println(dataSource.getConnection());
    	}
    
    }
    
    
  4. 创建实体类,导入Lombok

    package com.vincent.mapper;
    
    import com.vincent.pojo.Student;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @Mapper
    @Repository
    public interface StudentMapper {
       List<Student> queryStudent();
       int addStudent(Student student);
       int updateStudent(Student student);
       int deleteStudent(int id);
    
    }
    
    
    
    
    
  5. 创建mapper目录以及对应的 Mapper 接口

    StudentMapper.java

    package com.vincent.mapper;
    
     import com.vincent.pojo.Student;
     import org.apache.ibatis.annotations.Mapper;
     import org.springframework.stereotype.Repository;
     import org.springframework.web.bind.annotation.RestController;
     
     import java.util.List;
     
     @Mapper
     @Repository
     public interface StudentMapper {
         List<Student> queryStudent();
         int addStudent(Student student);
         int updateStudent(Student student);
         int deleteStudent(int id);
     
     }
    
    
  6. 对应的Mapper映射文件

    StudentMapper.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.vincent.mapper.StudentMapper">
         <select id="queryStudent" resultType="Student">
             select * from student
         </select>
         <insert id="addStudent" parameterType="Student">
             insert into student (id,name,password) values(#{id},#{name},#{password})
     
         </insert>
         <update id="updateStudent" parameterType="Student">
             update student set name=#{name},password=#{password} where id=#{id}
     
         </update>
         <delete id="deleteStudent" parameterType="int">
             delete from student where id=#{id}
         </delete>
     </mapper>
    
    
  7. maven配置资源过滤问题

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
    
  8. 编写Controller 进行测试!

    package com.vincent.controller;
    
     import com.vincent.mapper.StudentMapper;
     import com.vincent.pojo.Student;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.web.bind.annotation.GetMapping;
     import org.springframework.web.bind.annotation.RestController;
     
     import java.util.List;
     
     @RestController
     public class StudentController {
         @Autowired
         private StudentMapper studentMapper;
         @GetMapping("/queryStudent")
         public List<Student> queryStudent(){
             List<Student> students = studentMapper.queryStudent();
             return students;
     
         }
         @GetMapping("/addStudent")
         public String addStudent(){
             studentMapper.addStudent(new Student(101,"liming",19999));
             return"添加成功";
         }
         @GetMapping("/updateStudent")
         public String updateStudent(){
             studentMapper.updateStudent(new Student(2,"xiaohong",3333333));
             return"添加成功";
     
         }
         @GetMapping("/deleteStudent")
         public String deleteStudent(){
             studentMapper.deleteStudent(2);
             return "删除成功";
         }
     
     }
    
    

启动项目访问进行测试!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107596.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!