Spring-boot整合Mybatis增删改查操作(Mac系统)
创建Maven项目
配置Pom.xml
4.0.0
First
demo
0.0.1-SNAPSHOT
mysql mysql-connector-java8.0.27 org.mybatis.spring.boot mybatis-spring-boot-starter2.1.4 junit junit4.12 test org.springframework.boot spring-boot-starter-web2.4.1 com.alibaba druid1.2.6
MySql创建数据库:
workbench可视化操作创建库和表
本文用到的表为userMessage
column:stu_id, stu_name, stu_gender, stu_phone
创建三个包:
com.test(测试)@SpringBootApplication
@MapperScan(com.test.dao)
package com.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(“com.test.dao”)
public class TestSpringBoot {
public static void main(String[] args) {
SpringApplication.run(TestSpringBoot.class, args);
}
}
com.test.entity(实体类)对应数据库
package com.test.entity;
public class UserMessage implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private Integer stuId;
private String stuName;
private String stuGender;
private Integer stuPhone;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuGender() {
return stuGender;
}
public void setStuGender(String stuGender) {
this.stuGender = stuGender;
}
public Integer getStuPhone() {
return stuPhone;
}
public void setStuPhone(Integer stuPhone) {
this.stuPhone = stuPhone;
}
}
com.test.dao(底层连接数据库) 接口和抽象方法(增删改查),@Mapper来扫描
package com.test.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.test.entity.UserMessage;
@Mapper
public interface UserMessageDao {
public abstract int insert(UserMessage userMessage); public abstract int updateUserNameById(UserMessage userMessage); public abstract int deleteById(Integer stuId); public abstract ListselectAll(); public abstract UserMessage selectId(Integer stuId);
}
com.test.service(声明dao中的方法)@Service
package com.test.service;
import java.util.List;
import com.test.entity.UserMessage;
public interface UserService {
public abstract ListselectAll(); public abstract int insert(UserMessage userMessage); public abstract int deleteById(Integer stuId); public abstract UserMessage selectId(Integer stuId); public abstract int updateUserNameById(UserMessage userMessage);
}
com.test.ServiceImpl(实现dao中的方法this.)
package com.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.dao.UserMessageDao;
import com.test.entity.UserMessage;
import com.test.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired private UserMessageDao userMessageDao; @Override public ListselectAll() { // TODO Auto-generated method stub return this.userMessageDao.selectAll(); } public int insert(UserMessage userMessage) { return this.userMessageDao.insert(userMessage); } public int deleteById(Integer stuId) { return this.userMessageDao.deleteById(stuId); } public UserMessage selectId(Integer stuId) { return this.userMessageDao.selectId(stuId); } public int updateUserNameById(UserMessage userMessage) { return this.userMessageDao.updateUserNameById(userMessage); }
}
com.test.ServiceController(接收Service的信息注入到Spring容器中)@RestController @RequestMapping(value="/user")
package com.test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.entity.UserMessage;
import com.test.service.UserService;
@RestController
@RequestMapping(value="/user")
public class UserController {
@Autowired private UserService userService; @GetMapping(value="/selectAll") public ListselectAll(){ try { return this.userService.selectAll(); } catch (Exception e) { e.printStackTrace(); } return null; } @GetMapping(value="/insert") public int insert(UserMessage userMessage) { return this.userService.insert(userMessage); } @GetMapping(value="/deleteById") public int deleteById(Integer stuId) { return this.userService.deleteById(stuId); } @GetMapping(value="/selectId") public UserMessage selectId(Integer stuId) { try { return this.userService.selectId(stuId); } catch (Exception e) { e.printStackTrace(); } return null; } @GetMapping(value="/updateUserNameById") public int updateUserNameById(UserMessage userMessage) { return this.userService.updateUserNameById(userMessage); }
}
@GetMapping(value=“传入的值”)在相应的方法上添加
配置xml文件在Mapper文件夹中建议创建在resourse中
dao中扫描的此文件夹@Mapper
注意:sql语法不要出错



