[Springboot] springboot used to operate the database mybatis

New springboot project, choose a good web, mybatis, JDBC

Application.properties disposed in or application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studentManagerBoot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password:

Student entity class:

package com.hj.studentmanagerboot.user.entity;

public class Student {
    private int stuId;
    private int stuNum;
    private String stuName;
    private String stuBirthday;
    private int stuEnterYear;
    private int stiState;

    @Override
    public String toString() {
        return "Student{" +
                "stuId=" + stuId +
                ", stuNum=" + stuNum +
                ", stuName=" + stuName +
                ", stuBirthday='" + stuBirthday + '\'' +
                ", stuEnterYear=" + stuEnterYear +
                ", stiState=" + stiState +
                '}';
    }
    public String getStuName() {
        return stuName;
    }

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

    public int getStuId() {
        return stuId;
    }

    public void setStuId(int stuId) {
        this.stuId = stuId;
    }

    public int getStuNum() {
        return hours; 
    } 

    Public  void settled ( int times) {
         this .stuNum = hours; 
    } 

    Public String getStuBirthday () {
         return stuBirthday; 
    } 

    Public  void setStuBirthday (String stuBirthday) {
         this .stuBirthday = stuBirthday; 
    } 

    Public  int getStuEnterYear () {
         return stuEnterYear; 
    } 

    Public  void setStuEnterYear ( int stuEnterYear) {
         this .stuEnterYear = stuEnterYear; 
    }

    public int getStiState() {
        return stiState;
    }

    public void setStiState(int stiState) {
        this.stiState = stiState;
    }
}
View Code

Write new StudentMapper dao interface package added getStudent (int stuId) Method

package com.hj.studentmanagerboot.user.dao;

import com.hj.studentmanagerboot.user.entity.Student;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentMapper {
    Student getStudent(int stuId);
}

New service package getStudentService, and automatic assembly StudentMapper, the method of adding getStudentService

package com.hj.studentmanagerboot.user.service;

import com.hj.studentmanagerboot.user.dao.StudentMapper;
import com.hj.studentmanagerboot.user.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceTest {
    @Autowired
    StudentMapper studentMapper;

    public Student getStudent(int stuId) {
        return studentMapper.getStudent(stuId);
    }
}

Controller in the automatic assembly StudentService

package com.hj.studentmanagerboot.user.handler;


import com.hj.studentmanagerboot.user.service.StudentServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("testStudent")
public class StudentHandlerTest {
    @Autowired
    private StudentServiceTest studentServiceTest;
    @RequestMapping("getUser/{id}")
    public String getUser(@PathVariable("id") int stuId) {
        return studentServiceTest.getStudent(stuId).toString();
    }

}

Mybatis increase the annotation, the program scans the inlet dao package in SpringBoot

@MapperScan("com.hj.studentmanagerboot.user.dao")
public class ...{

}

New mapper file in the Resource folder store Mapper.xml, and increase the information stored mybatis the Mapper configuration files in application.yml or application.properties in:

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.hj.studentmanagerboot.user.entity

Writing the configuration information 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.hj.studentmanagerboot.user.dao.StudentMapper">
    <select id="getStudent" resultType="com.hj.studentmanagerboot.user.entity.Student">
        select * from stuInfo where stuId = #{stuId}
    </select>
</mapper>

 

Guess you like

Origin www.cnblogs.com/to-red/p/11368292.html