项目结构
config
package com.example.mpdemo.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author Fxdll
* @Date 2024/5/8 22:52
* @PackageName:com.example.mpdemo.config
* @ClassName: MyBatisPlusConfig
* @Description: TODO MybatisPlus分页插件配置
* @Version 1.0
*/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
//mybatis-plus分页插件拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分页插件 DbType 数据库类型
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
controller
package com.example.mpdemo.controller;
import com.example.mpdemo.entity.Order;
import com.example.mpdemo.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author Fxdll
* @Date 2024/5/7 23:34
* @PackageName:com.example.mpdemo.controller
* @ClassNRme: OrderController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@GetMapping("/order/findAll")
public List findAll() {
List orders = orderMapper.SelectAllOrdersAndUser();
return orders;
}
}
package com.example.mpdemo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mpdemo.entity.User;
import com.example.mpdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Author Fxdll
* @Date 2024/5/4 22:52
* @PackageName:com.example.mpdemo.controller
* @ClassName: UserController
* @Description: TODO
* @Version 1.0
*/
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
//0506更新--------------------------------------
@GetMapping("/user/findAll")
public List<User> query() {
// List<User> list = userMapper.find();
// System.out.println(list);
// return list;
return userMapper.SelectAllUserAndOrders();
// return userMapper.find();
}
//0508更新------------------------------------------
//条件查询
@GetMapping("/user/find")
public List<User> findByCond() {
QueryWrapper<User> queryWrapper = new QueryWrapper();
queryWrapper
.eq("username","2 ");
return userMapper.selectList(queryWrapper);
}
//分页查询
@GetMapping("/user/findByPage")
public IPage findByPage() {
Page<User> page = new Page<>(0, 2);
IPage iPage = userMapper.selectPage(page, null);
return iPage;
}
// 0504更新-------------------------------------------
@PostMapping("/user")
public String save(User user) {
int i = userMapper.insert(user);
if (i > 0) {
return "插入成功";
} else {
return "插入失败";
}
}
@PutMapping("/user")
public String update(User user) {
int i = userMapper.update(user);
if (i > 0) {
return "更新成功";
} else {
return "更新失败";
}
}
@DeleteMapping("/user/{id}")
public String delete(@PathVariable("id") int id) {
int i = userMapper.delete(id);
if (i > 0) {
return "删除成功";
} else {
return "删除失败";
}
}
}
entity
package com.example.mpdemo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
/**
* @Author Fxdll
* @Date 2024/5/7 23:25
* @PackageName:com.example.mpdemo.entity
* @ClassName: Order
* @Description: TODO
* @Version 1.0
*/
@TableName("T_order")
public class Order {
private int id;
private String ordertime;
private Double total;
private int uid;
@TableField(exist = false)
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", ordertime='" + ordertime + '\'' +
", total=" + total +
", uid=" + uid +
", user=" + user +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOrdertime() {
return ordertime;
}
public void setOrdertime(String ordertime) {
this.ordertime = ordertime;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
package com.example.mpdemo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import org.springframework.core.annotation.Order;
import java.util.List;
/**
* @Author Fxdll
* @Date 2024/5/4 23:11
* @PackageName:com.example.mpdemo.entity
* @ClassName: User
* @Description: TODO
* @Version 1.0
*/
@TableName("t_user")
public class User {
@TableId(type = IdType.AUTO)
private int id;
private String username;
private String password;
private String birthday;
// 描述用户的所有订单 selecet id,username,password,order from t_user;
@TableField(exist = false)
private List<Order> orders;
//alt+enter 生成getter和setter方法
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 + '\'' +
", orders=" + orders +
'}';
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
mapper
package com.example.mpdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mpdemo.entity.Order;
import com.example.mpdemo.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @Author Fxdll
* @Date 2024/5/7 22:53
* @PackageName:com.example.mpdemo.mapper
* @ClassName: OrderMapper
* @Description: TODO
* @Version 1.0
*/
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
//根据用户id查询所有订单
@Select("SELECT * FROM t_order WHERE id = #{uid}")
List<Order> selectByUid(int uid);
//查询所有订单及用户信息
@Select("select * from t_order")
@Results( {
@Result(column = "id", property = "id"),
@Result(column = "ordertime", property = "ordertime"),
@Result(column = "total", property = "total"),
@Result(column = "uid", property = "user", javaType = User.class,
//拷贝全路径
one = @One(select = "com.example.mpdemo.mapper.UserMapper.selectById")
),
})
List<Order> SelectAllOrdersAndUser();
}
package com.example.mpdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mpdemo.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @Author Fxdll
* @Date 2024/5/4 22:54
* @PackageName:com.example.mpdemo.mapper
* @ClassName: UserMapper
* @Description: TODO
* @Version 1.0
*/
//用于操作用户表,Mybatis会根据mapper注解,动态实现UserMapper接口(实现类),动态代理技术
//SPring会自动创建usermapper接口实现类对应的实例
@Mapper
public interface UserMapper extends BaseMapper<User> {
//查询所有用户 0507
@Select("select * from user")
public List<User> find();
//20240506
//查询用户,根据用户id查询信息
@Select("select * from t_user where id = #{id}")
User selectById(int id);
//查询用户及所有订单
@Select("select * from t_user")
//结果集映射,前面是数据库字段,后面是实体类属性 ,类里的字段
@Results( {
@Result(column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "password", property = "password"),
@Result(column = "id", property = "orders", javaType = List.class,
//通过OrderMapper的selectByUid方法查询订单 拷贝全路径
many = @Many(select = "com.example.mpdemo.mapper.OrderMapper.selectByUid")
),
})
List<User> SelectAllUserAndOrders();
//历史202405
// @Select("select * from user where id = #{id}")
@Update("update user set username = #{username},password = #{password},birthday = #{birthday} where id = #{id}")
public int update(User user);
@Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
public int insert(User user);
@Delete("delete from user where id = #{id}")
public int delete(Integer id);
@Select("select * from user where id = #{id}")
public User findById(Integer id);
}
application
package com.example.mpdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mpdemo.mapper")
public class MpdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MpdemoApplication.class, args);
}
}
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mpdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mpdemo</name>
<description>mpdemo</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.2</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis-plus 依赖-->
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.mpdemo.MpdemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>