springboot与mybatis的整合

官方文档: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 "删除成功";
         }
     
     }
    
    

启动项目访问进行测试!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值