Springboot2.0 + MyBatis整合

pom依赖


    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
    

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


在application.properties文件中加入

spring.datasource.url=jdbc:mysql://localhost:3306/exam?useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Springboot2.0 + MyBatis整合_第1张图片
image

创建实体类 Student.java

public class Student {

    private int sid;

    private String sname;

    private int age;

    private  String gander;

    private  String province;

    private String tuition;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getAge() {
        return age;
    }

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

    public String getGander() {
        return gander;
    }

    public void setGander(String gander) {
        this.gander = gander;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getTuition() {
        return tuition;
    }

    public void setTuition(String tuition) {
        this.tuition = tuition;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", age=" + age +
                ", gander='" + gander + '\'' +
                ", province='" + province + '\'' +
                ", tuition='" + tuition + '\'' +
                '}';
    }
}

创建Mapper

import com.demo.test.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudentMapper {

    @Select("select * from stu where sname = #{name} ")
    Student findByName(@Param("name") String name);

    @Select("select * from stu where age = #{age} ")
    List findByAge(@Param("age") String age);
}

创建Service

import com.demo.test.mapper.StudentMapper;
import com.demo.test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;

    public Student findByName(String name){   
        return studentMapper.findByName(name);
    }
    public List findByAge(String age){
        return studentMapper.findByAge(age);
    }
}

创建运行类 (最好把该文件放在项目跟目录下)

import com.demo.test.Service.StudentService;
import com.demo.test.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
public class Test implements CommandLineRunner{
    @Autowired
    private StudentService studentService;
    @Override
    public void run(String... args) throws Exception {
        Student name = studentService.findByName("张娜");
        System.out.println(name.toString());
        List byAge = studentService.findByAge("23");
        for (Student student : byAge) {
            System.out.println(student.toString());
        }
    }
}

创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    }

}

Springboot2.0 + MyBatis整合_第2张图片
image

你可能感兴趣的:(Springboot2.0 + MyBatis整合)