springboot学习笔记(四)

2023-12-20 18:37:24

本节笔记主要是MybatisPlus的快速上手笔记。

????????????????????????1.ORM介绍

????????????????????????2.MybatisPlus介绍

????????????????????????3.MybatisPlus CRUD操作

1.ORM介绍:

????????之前学习过Django和mangodb,所以对ORM有一定的了解,简单来说ORM(Object Relational Mapping 对象关系映射)是为了解决面向对象与关系数据库存在的互不匹配现象的一种技术。

????????ORM通过使用描述对象和数据库之间的映射的元数据将程序中的对象自动持久化到关系数据库中。

????????ORM框架的本质是简化变成中操作数据库的编码。

2.MybatisPlus介绍

MyBatis是一款优秀的数据持久层的ORM框架,被广泛地应用于应用系统。

Mybatis能够非常灵活地实现动态SQL,可以使用XML或者注解配置和映射原生信息,能够轻松地将JAVA的POJO与数据库中的表和字段进行映射关联。

Mybatis-Plus是一个Mybatis的增强工具,在Mybatis的基础上做了增强,简化了开发。

a.添加依赖

 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>

b.全局配置:

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-calss-name = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

c.添加@MapperScan注解:

package com.example.demo.mpdemo;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mpdemo")
public class MpdemoApplication {
	public static void  main(String[] args) {
		SpringApplication.run(MpdemoApplication.class, args);
		
	}

}

3.MybatisPlus CRUD操作

a.编写CRUD操作

package com.example.demo.mpdemo;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.demo.mpdemo.User;

@Mapper
public interface UserMapper {
//	@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
//	int add(User user);
	@Select("select * from user")
	public List<User> findall();
	@Update("update user set username=#{username},password=#{password},birthday=#{birthday} where id =#{id}")
	public int update(User user);
	@Delete("delete from user where id=#{id}")
	public int delete(int id);
	@Select("select * from user where id=#{id}")
	public User findById(int id);
}

b.编写Controller

package com.example.demo.mpdemo;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class UserMapperController {
	@Autowired
	private UserMapper userMapper;

	@GetMapping("/usermp/{id}")
	public Userquery(@PathVariable int id) {
		User stringDemo = userMapper.findById(id);
		System.out.println(stringDemo.toString());
		return stringDemo;
	}
	@PostMapping("/usermp/{id}")
	public String update(User user) {
		userMapper.update(user);
		return "更新用户";
	}

	@DeleteMapping("/usermp/{id}")
	public String delete(@PathVariable int id) {
		userMapper.delete(id);
		return "删除用户";
	}
	@GetMapping("/usermp/findAll")
	public List<User> queryAll() {
		List<User> stringDemo = new ArrayList<User>();
		stringDemo = userMapper.findall();
		return stringDemo;
	}
	
}

User代码:

package com.example.demo.mpdemo;

public class User {
	private int id;
	private String username;
	private String password;
	private String birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", birthday=" + birthday + "]";
	}

}

c.事先在Mysql数据库中编写了几条数据:

d.启动Springboot:

文章来源:https://blog.csdn.net/qq_40377374/article/details/135104355
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。