Spring boot整合MyBatis

  步骤一:导入依赖

"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
        
            mysql
            mysql-connector-java
            5.1.32
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
            5.1.32
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  步骤二:application.properties文件

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8
spring.datasource.password=123
spring.datasource.username=root

  步骤三:数据库和实体类

  Spring boot整合MyBatis_第1张图片

package com.example.demo.entity;

public class student {
    private Integer stuid;
    private String stuname;
    private Integer age;

    public Integer getStuid() {
        return stuid;
    }

    public void setStuid(Integer stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

  步骤四:dao

@Repository("istudao")
public interface Istudao {

    @Select("select * from student")
    public List allstu();
}

  步骤五:service与serviceimpl

public interface studentservice {
    public List allstu();
}
@Service
public class studentserviceimpl implements studentservice {

    @Resource
    private Istudao istudao;


    @Override
    public List allstu() {
        return istudao.allstu();
    }
}

  步骤六:Controller

 

@Controller
public class myconterller {

    @Resource
    private studentservice studentservice;

    @RequestMapping("/stu")
    @ResponseBody
    public Object getAll(){
        return studentservice.allstu();
    }
}

  步骤七:启动

@SpringBootApplication()
@MapperScan("com.example.demo.dao")
public class Springboot {
    public static void main(String[] args) {
        SpringApplication.run(Springboot.class, args);
    }
}

  步骤八:完成

 

 

 

 

 

 

 

 

你可能感兴趣的:(Spring boot整合MyBatis)