springboot学习笔记(四)

本节笔记主要是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:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值