ORM介绍
ORM(对象关系映射)是为了解决面向对象与关系数据库存在的互不匹配现象的技术。通过使用描述对象和数据库之间映射的元数据将
程序中的对象
自动持久化到关系数据库
中。
ORM框架本质是简化编程中操作数据库的编码。
MyBatis-Plus
MyBatis是一款数据持久层ORM框架,被广泛地应用到应用系统中。
MyBatis能够非常灵活地实现动态SQL,可以使用XML
或注解
来配置和映射原生信息,能够轻松将java的POJO
(Plain Ordinary Java Object,普通的Java对象)与数据库中的表和字段
进行映射关联。
MyBatis-Plus是一个MyBatis的增强工具,简化了开发。
MyBatis-Plus依赖
<!-- MyBatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MySQL驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据连接池druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
数据库配置
#MyBatisPlus配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book?useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
@SpringBootApplication
@MapperScan("com.nkk.demo_echarts.mapper") //mapper包的地址
public class DemoEchartsApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEchartsApplication.class, args);
}
}
IDEA实现MyBatis
package com.nkk.demo_echarts.mapper;
import com.nkk.demo_echarts.Bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from admin")
public List<User> findAll();
@Insert("insert into admin values (#{id},#{username},#{password})")
public int insertUser(User user);
}
package com.nkk.demo_echarts.Controller;
import com.nkk.demo_echarts.Bean.User;
import com.nkk.demo_echarts.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class MapperController {
// 注入,使userMapper有值
@Autowired
private UserMapper userMapper;
@GetMapping("/user/findAll")
public List findAll() {
List<User> userList = userMapper.findAll();
System.out.println(userList);
return userList;
}
@PostMapping("/user/userAdd")
public String userAdd(User user) {
int i = userMapper.insertUser(user);
if (i > 0) {
return"插入成功";
} else {
return "插入失败";
}
}
}
IDEA实现MyBatisPlus
User类
package com.nkk.demo_echarts.Bean;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("admin") //类名和数据表名不一致
public class User {
private int aid;
private String username;
private String password;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
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;
}
@Override
public String toString() {
return "User{" +
"aid=" + aid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
@TableName:
如果数据库表名和类名不一致,需要使用@TableName
注解表明是哪个数据库
@TableId:
@TableId(type = IdType.AUTO) aid是主键且自增。添加数据时,可以自动添加aid值到数据库;如果不用该注解,虽然数据表中aid值自增,但代码查询到的aid值还是0
@TableField:
数据表字段名和类里的变量名不一致,或者数据表中不包含某个字段名(exist = false)
MyBatisPlus注解:https://baomidou.com/pages/223848/#tablefield
UserMapper接口
package com.nkk.demo_echarts.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nkk.demo_echarts.Bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper extends BaseMapper<User> {
// @Select("select * from admin")
// public List<User> findAll();
//
// @Insert("insert into admin values (#{id},#{username},#{password})")
// public int insertUser(User user);
}
UserMapper继承BaseMapper,BaseMapper中有关于对数据库的操作,可以直接使用
MapperController
在MapperController中可以直接使用BaseMapper中的方法
package com.nkk.demo_echarts.Controller;
import com.nkk.demo_echarts.Bean.User;
import com.nkk.demo_echarts.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class MapperController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/findAll")
public List findAll() {
// List<User> userList = userMapper.findAll();
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
return userList;
}
@PostMapping("/user/userAdd")
public String userAdd(User user) {
// int i = userMapper.insertUser(user);
int i = userMapper.insert(user);
if (i > 0) {
return"插入成功";
} else {
return "插入失败";
}
}
}
参考视频: https://www.bilibili.com/video/BV1nV4y1s7ZN/?p=6&spm_id_from=333.880.my_history.page.click&vd_source=4635338a4ba3d138feac02fc9c626af7