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

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>

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 List<UserMessage> selectAll();
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 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);

}

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);
}

}

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);
}

}
@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>

注意:sql语法不要出错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值