WHCSRL 技术网

Spring-boot整合Mybatis增删改查操作(Mac系统)_weixin

Spring-boot整合Mybatis增删改查操作(Mac系统)

创建Maven项目

配置Pom.xml

4.0.0
First
demo
0.0.1-SNAPSHOT

<dependencies>
	<!--mysql数据库驱动包 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>8.0.27</version>
	</dependency>

	<!--引入mybatis包 -->
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>2.1.4</version>
	</dependency>

	<!--引入junit测试包 -->
	<dependency> 
		<groupId>junit</groupId> 
		<artifactId>junit</artifactId> 
		<version>4.12</version> 
		<scope>test</scope>
     </dependency>

	<!-- 导入SpringBoot依赖 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<version>2.4.1</version>
	</dependency>

	
	
	<!-- <dependency> <groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-test</artifactId>
	 	<version>2.3.9.RELEASE</version>
   </dependency> -->


	<!-- 导入Druid的依赖:是阿里巴巴提供的数据库连接池的实现 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.2.6</version>
	</dependency>


</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

}

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 List<UserMessage> selectAll();
public abstract UserMessage selectId(Integer stuId);
  • 1
  • 2
  • 3
  • 4
  • 5

}

com.test.service(声明dao中的方法)@Service

package com.test.service;

import java.util.List;

import com.test.entity.UserMessage;

public interface UserService {

public abstract List<UserMessage> selectAll();
public abstract int insert(UserMessage userMessage);
public abstract int deleteById(Integer stuId);
public abstract UserMessage selectId(Integer stuId);
public abstract int updateUserNameById(UserMessage userMessage);
  • 1
  • 2
  • 3
  • 4
  • 5

}

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 List<UserMessage> selectAll() {
	// 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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

}

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 List<UserMessage> selectAll(){
	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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

}
@GetMapping(value=“传入的值”)在相应的方法上添加

配置xml文件在Mapper文件夹中建议创建在resourse中
dao中扫描的此文件夹@Mapper

<?xml version="1.0" encoding="UTF-8"?> insert into user_message ( stu_id, stu_name, stu_gender, stu_phone) values ( #{stuId}, #{stuName}, #{stuGender}, #{stuPhone} ) delete from user_message where stu_id=#{stuId} update user_message set stu_name=#{stuName} where stu_id=#{stuId}
<select id="selectAll" resultType="com.test.entity.UserMessage">
	select
	stu_id,
	stu_name,
	stu_gender,
	stu_phone
	from user_message
</select>
<select id="selectId" resultType="com.test.entity.UserMessage" parameterType="com.test.entity.UserMessage">
	select *
	from user_message
	where stu_id=#{stuId}
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意:sql语法不要出错

推荐阅读