总体结构

1、引入jar包
#数据库包 mysql
mysql mysql-connector-java runtime
#mybatis 包
org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3
2、修改application.yml配置文件
server:
port: 8081
spring:
application:
name: Demo
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=true&userUnicode=true&serverTimezone=GMT
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
3、mapper
insert into user_info values (#{username})
4、controller
package com.example.demo.controller;
import com.example.demo.entity.UserLogin;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController("userController")
@Api(tags = "用户管理")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation(value = "查询全部信息", notes = " wanglina-2022-11-30")
@GetMapping("/getAll")
public List getAll(){
//查询全部信息
return userService.queryAll();
}
@ApiOperation(value = "插入数据", notes = " wanglina-2022-11-30")
@RequestMapping(value = "/addUser",method = RequestMethod.POST)
public UserLogin addUser(@RequestBody UserLogin userEntity){
return userService.add(userEntity);
}
}
5、service
package com.example.demo.service;
import com.example.demo.entity.UserLogin;
import java.util.List;
public interface UserService {
//查询
public List queryAll();
//添加数据
public UserLogin add(UserLogin userLogin);
//根据用户名查询数据
public UserLogin queryByName(String username);
}
package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.UserLogin;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List queryAll() {
return userDao.queryAll();
}
@Override
public UserLogin add(UserLogin userLogin) {
userDao.add(userLogin);
}
@Override
public UserLogin queryByName(String username) {
return userDao.queryByName(username);
}
}
6、dao
package com.example.demo.dao;
import com.example.demo.entity.UserLogin;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao {
//查询
public List queryAll();
//添加数据
public void add(UserLogin userLogin);
//根据用户名查询数据
public UserLogin queryByName(String username);
}
7、 启动类 增加注解
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo.dao")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}