SpringBoot整合Mybatis再连接Mysql数据库

发布于:2021-10-17 12:19:20

一: Idea 快速搭建SpringBoo项目 ,直接来操作流程
注 : 此案例是闲来无事写着玩的,后期还要改正 . 各位大佬 非喜勿喷
1:

2:

3:

4: 启动成功
二: 整合Mybatis再连接Mysql数据库
pom 文件配置



xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

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


com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot


1.8




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



org.springframework.boot
spring-boot-starter-jdbc



org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2



mysql
mysql-connector-java
runtime



org.projectlombok
lombok
provided




io.springfox
springfox-swagger2
2.7.0





io.springfox
springfox-swagger-ui
2.7.0



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


org.junit.vintage
junit-vintage-engine








org.springframework.boot
spring-boot-maven-plugin







三: 测试结构

四: 测试数据

五: 代码
UserMapper.xml



"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >






UserMapper


package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;

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

@Mapper
public interface UserMapper {

List findAll();
}


UserService


package com.example.demo.service;

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

public interface UserService {

List findAll();
}


UserServiceImpl


package com.example.demo.service.serviceimpl;

import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;

@Override
public List findAll() {
return userMapper.findAll();
}
}


UserController


package com.example.demo.controller;

import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@ResponseBody
@RequestMapping("/ceshi")
@Api(value = "查找", tags = "查")
public class UserController {
@Autowired
private UserService userService;

@ResponseBody
@RequestMapping(value = "/find", method = RequestMethod.POST)
@ApiOperation("查")
public List findAll() {
List list = userService.findAll();
return list;
}

}


application.yml


spring:
profiles:
active: dev

application-dev.yml



测试结果:

相关推荐

最新更新

猜你喜欢