[Spring boot] Spring boot连接数据库(mybatis注解方式)

第一步:在pom文件加入mybatis和mysql依赖;

<!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

第二步:在application.properties文件中加入数据库连接配置;

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot
spring.datasource.username=root
spring.datasource.password=root

第三步:创建建entity包,用于存储实体类,用于与dao层形成映射关系;

              创建dao包,存放Mapper类,用于给service层提供数据库操作;

              创建service包,存放服务类Bean,用于向controller层提供服务;

              创建controller包,用于创建控制器类,响应请求;

              在以上各个包中创建本次示例文件,具体如下图: 

各个加入的文件代码如下:

1、实体类Student(设置成员变量,与数据库中的字段对应,并且写好get\set方法)

package com.zct.test_hello.entity;

import java.util.Date;

public class Student
{
  private Integer id;
  private Integer student_id;
  private String name;
  private String sex;
  private Integer age;
  private Date brithday;

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

  public void setStudent_id(Integer student_id) {
    this.student_id = student_id;
  }

  public void setName(String name) {
    this.name = name;
  }

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

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

  public void setBrithday(Date brithday) {
    this.brithday = brithday;
  }

  public Integer getId() {
    return id;
  }

  public Integer getStudent_id() {
    return student_id;
  }

  public String getName() {
    return name;
  }

  public String getSex() {
    return sex;
  }

  public Integer getAge() {
    return age;
  }

  public Date getBrithday() {
    return brithday;
  }
}

2、Mapper类StudentDao(数据库操作,@Mapper将StudentDao声明为一个Mapper接口,@Results是结果映射列表,@Result中property是User类的属性名,colomn是数据库表的字段名,@Select, @Insert 分别代表了被标注的方法要执行的真实SQL)

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StudentDao
{
//@Results这段也可以不用写
  @Results({
      @Result(property = "id", column = "id"),
      @Result(property = "student_id", column = "student_id"),
      @Result(property = "name", column = "name"),
      @Result(property = "sex", column = "sex"),
      @Result(property = "age", column = "age"),
      @Result(property = "brithday", column = "brithday")
  })
  @Select("SELECT * From student_info")
  List<Student> findAll();
}

3、服务类service(@Service用于将该类声明为一个服务类bean;@Autowired用于连接到StudentDao)

package com.zct.test_hello.service;

import com.zct.test_hello.dao.StudentDao;
import com.zct.test_hello.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService
{
  @Autowired
  private StudentDao studentDao;

  public List<Student> showAll()
  {
    return  studentDao.findAll();
  }
}

4、控制器类StudentController(@RestController 用于声明一个Restful风格的控制器 @Autowired用于连接到StudentService,@RequstMapping用于声明请求映射方法);

package com.zct.test_hello.controller;

import com.zct.test_hello.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class StudentController
{
  @Autowired
  private StudentService studentService;

  @RequestMapping(value = "/find")
  public Object showAll()
  {
    return studentService.showAll();
  }
}

第五步、启动类

package com.zct.test_hello;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//如果dao包是在默认包下可以不用下面这句
@MapperScan(basePackages = "com.zct.test_hello.dao")  //用于声明mapper类的位置
public class TestHelloApplication {

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

}

第六步,运行,输入localhost:8080/find即可看到查询出的结果。

总结:可以看出spring boot 查数据的机制,实体类和dao层形成映射关系;dao层用于查数据;service类连接dao层,形成对controller类提供服务的bean;最后controller根据请求,响应调用对应的服务响应给用户;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值