SpringBoot整合MyBatis实现增删改查案例完整版
1.Spring Boot
Spring Boot是⼀个简化Spring开发的框架。⽤来监护spring应⽤开发,约定⼤于配置,去繁就简,just run 就能创建⼀个独⽴的,产品级的应⽤。
我们在使⽤Spring Boot时只需要配置相应的Spring Boot就可以⽤所有的Spring组件,简单的说,spring boot就是整合了很多优秀的框架,不⽤我们⾃⼰⼿动的去写⼀堆xml配置然后进⾏配置。从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。
闲话少说,上图⽂:
2.开始
新建⼀个项⽬
选择Spring Initializr项⽬
创建项⽬的⽂件结构以及jdk的版本,本次以demo为例
选择项⽬所需要的依赖
修改项⽬名,finish完成,本次选择为修改
来看⼀眼pom⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-
instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId&le</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改配置⽂件本,⽂不使⽤application.properties⽂件⽽使⽤更加简洁的l⽂件。
修改为l
内容:
server:
port: 8080
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: sql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*l
type-aliases-package: ity.demo
logging:
level:
com:
example:
mapper : debug
创建表:
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;⽬录结构:
UserController.java
ller;
ity.User;
ample.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
/**
* @author yn2333
* @Date 2020/03/03
*/
@RestController
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/selectUserByid", produces = "application/json;charset=UTF-8", method = RequestMethod.GET) @ResponseBody
public String GetUser(User user){
return userService.Sel(user).toString();
}
@RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public String Add(User user){
return userService.Add(user);
}
@RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = Reques
spring framework组件tMethod.GET)
public String Update(User user){
return userService.Update(user);
}
@RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public String Delete(User user){
return userService.Delete(user);
}
}
User.java
ity;
/**
* @author yn2333
* @Date 2020/03/03
*/
public class User {
private Integer id;
private String username;
private String password;
private String address;
public User(Integer id, String username, String password, String address) {
this.id = id;
this.username = username;
this.password = password;
this.address = address;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getAddress() {
return address;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", address='" + address + '\'' +
'}';
}
}
UserMapper.java
ample.demo.mapper;
ity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/
**
* @author yn2333
* @Date 2020/03/03
*/
@Repository
public interface UserMapper {
User Sel(@Param("user")User user);
int Add(@Param("user")User user);
int Update(@Param("user")User user);
int Delete(@Param("user")User user);
}
UserService.java
ample.demo.service;
ity.User;
ample.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
/**
* @author yn2333
* @Date 2020/03/03
*/
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User Sel(User user) {
return userMapper.Sel(user);
}
public String Add(User user) {
int a = userMapper.Add(user);
if (a == 1) {
return "添加成功";
} else {
return "添加失败";
}
}
public String Update(User user) {
int a = userMapper.Update(user);
if (a == 1) {
return "修改成功";
} else {
return "修改失败";
}
}
public String Delete(User user) {
int a = userMapper.Delete(user);
if (a == 1) {
return "删除成功";
} else {
return "删除失败";
}
}
}
DemoApplication.java
ample.demo;
batis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* @author yn2333
* @Date 2020/03/03
*/
@MapperScan("ample.demo.mapper") //扫描的mapper
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论