SpringBoot整合MyBatis

点击上方 Java老铁,并选择 设为星标

优质文章和资料会及时送达

一、建表

1.建表语句

create table if not exists student(
    id int(10) primary key Auto_Increment,
    stu_name varchar(10) not null ,
    sex char(2) not null ,
    age int(10) not null
);

2.插入数据

insert into student(stu_name, sex, age) values ("阿萌","女",30);
insert into student(stu_name, sex, age) values ("阿辙","男",50);

二、创建Maven项目

1.父工程

pom.xml


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
        
    


    
        UTF-8
        UTF-8
        1.8
    


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

2.创建子项目springBoot-myBatis

pom.xml




        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        


        
        


        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        


        
        
            mysql
            mysql-connector-java
            runtime
        


    

3.在资源目录下创建application.yml配置文件

server:
  port: 9001 #端口
spring:
  application:
    name: springBoot-myBatis #服务名
  datasource: #连接池四大参数
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springbootdemo?characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    druid:
      initial-size: 1 #初始化连接数
      min-idle: 1 #最小空闲连接
      max-active: 20 #最大活动连接
      test-on-borrow: true #获取连接时测试是否可用
      stat-view-servlet:
        allow: true #监控页面启动
mybatis:
  type-aliases-package: com.eu.demo.pojo  #别名扫描
  mapper-locations: classpath:mapper/*.xml  #加载配置文件
  configuration:
    map-underscore-to-camel-case: true  #开启驼峰式命名

4.编写pojo

package com.eu.demo.pojo;


public class Student {


    private Integer id;
    private String stuName;
    private String sex;
    private Integer age;


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getStuName() {
        return stuName;
    }


    public void setStuName(String stuName) {
        this.stuName = stuName;
    }


    public String getSex() {
        return sex;
    }


    public void setSex(String sex) {
        this.sex = sex;
    }


    public Integer getAge() {
        return age;
    }


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


5.编写dao

@Repository
public interface StudentMapper {


    List findAll();
}

6.编写mapper

在资源目录下创建mapper目录,并且编写xml文件




    

7.编写service

@Service
public class StudentService {


    @Autowired
    private StudentMapper studentMapper;


    public List findAll(){
        return studentMapper.findAll();
    }
}

8.编写controller

@RequestMapping("/students")
@RestController
public class StudentController {


    @Autowired
    private StudentService studentService;


    @GetMapping
    public List getStudents(){
        return studentService.findAll();
    }
}

9.编写启动类

@MapperScan("com.eu.demo.dao")
@SpringBootApplication
public class MyBatisApplication {


    public static void main(String[] args) {
        SpringApplication.run(MyBatisApplication.class);
    }
}

10.项目结构

SpringBoot整合MyBatis_第1张图片

11.启动测试

SpringBoot整合MyBatis_第2张图片

关注我

获取更多
Java干货

原创文章

视频资料

技术交流群

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