记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记
1.添加Mybatis和Mysql依赖   

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

2.创建pojo,mapper,service,controller

此时项目结构

SpringBoot 集成Mybatis 连接Mysql数据库

3.配置application配置文件

SpringBoot 集成Mybatis 连接Mysql数据库

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


server.port=8080
server.tomcat.uri-encoding=UTF-8

#mybatis.config= classpath:mybatis-config.xml
mybatis.typeAliasesPackage=com.zld.student.bean
mybatis.mapperLocations=classpath:mappers/*Mapper.xml
4.添加接口

package com.zld.student.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zld.student.pojo.Student;
import com.zld.student.service.StudentService;

@RestController
@RequestMapping("student")
public class StudentController {

@Autowired
StudentService studentService;

@RequestMapping(value = "/add", method = { RequestMethod.GET, RequestMethod.POST })
public String add(Student student) {
return studentService.add(student);

}

@RequestMapping(value = "/delete", method = { RequestMethod.GET, RequestMethod.POST })
public String delete(
@RequestParam(value = "ids", required = false) String[] ids) {
if (ids != null && ids.length <= 0) {
return "Ids不能为空";
}
return studentService.delete(ids);
}

@RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST })
public String update(Student student) {
return studentService.update(student);
}

@RequestMapping(value = "/findList", method = { RequestMethod.GET, RequestMethod.POST })
public Map<String, Object> findEqList(
) {
Map<String, Object> data = new HashMap<String, Object>();
List<Student> list=studentService.findEqList();
if (list.isEmpty()) {
data.put("msg", "无数据");
return data;
}
data.put("list", list);
return data;
}

@RequestMapping(value = "/findById", method = { RequestMethod.GET, RequestMethod.POST })
public Student findByIds(
@RequestParam(value = "id", required = false) Integer id) {
return studentService.findById(id);
}

}

package com.zld.student.service;

import java.util.List;

import com.zld.student.pojo.Student;

public interface StudentService {

    String add(Student student);

    String delete(String[] ids);

    String update(Student student);

    List<Student> findEqList();

    Student findById(Integer id);

}
StudentService

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2021-04-21
  • 2021-08-06
  • 2022-12-23
  • 2021-04-15
  • 2021-06-20
猜你喜欢
  • 2021-11-30
  • 2021-08-20
  • 2022-12-23
  • 2021-05-03
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
相关资源
相似解决方案