spring boot-mybatis框架整合

整体目录结构

spring boot-mybatis框架整合_第1张图片

sql语句

use course;

create table if not exists student(
    id int primary key auto_increment comment '学号',
    name varchar(4) not null unique comment '姓名',
    sex  char(1)  not null comment '性别',
    classId int not null comment '班级'
);

实体类

package com.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class Student {
    private Integer id;
    private String name;
    private String sex;
    private Integer classId;
}

dao接口

package com.dao;

import com.model.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface StudentDao {

    List<Student> selectAllStudents();

    void insert(Student student);

}

service层

package com.service;

import com.dao.StudentDao;
import com.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentService {
    @Autowired(required = false)
    private StudentDao studentDao;

    private static int i;

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

    public void insert(){
        Student student=new Student();
        student.setName("种子"+i+"号");
        student.setId(i++);
        student.setClassId(1);
        student.setSex("男");
        studentDao.insert(student);
    }

}

controller层

package com.controller;

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

import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/select")
    public List<Student> selectAllStudents(){
        return studentService.selectAllStudents();
    }

    @RequestMapping("/insert")
    public String insert(){
        try{
            studentService.insert();
        }catch(Exception e){
            return e.getMessage();
        }
        return "ok";
    }
}

spring-boot启动类

package com;

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


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

mybatis-config.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.dao.StudentDao">
    <select id="selectAllStudents" resultType="com.model.Student">
        select * from student
    </select>

    <insert id="insert" parameterType="com.model.Student">
        insert into student(name,sex,classId) values (#{name},#{sex},#{classId})
    </insert>

</mapper>

application.properties


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

mybatis.mapper-locations=classpath:mapper/*.xml

pom.xml文件


<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>spring-boot-mybatisartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.4.RELEASEversion>
    parent>


    <properties>
        <mysql.version>5.1.47mysql.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.1version>
        dependency>


        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.21version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>

    dependencies>

project>

你可能感兴趣的:(框架)