SpringBoot——MyBatisPlus 短命女 2021-10-18 14:36 494阅读 0赞 # 1.引如MyBatisPlus依赖 # 下面是整个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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>world-address-dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>world-address-dao</name> <description>Demo project for Spring Boot</description> <parent> <artifactId>world</artifactId> <groupId>com.youyou</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--MybatisPlus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> # 2.定义个实体类 # 这里需要配置一个@TableName package com.youyou.address.entity; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** * 数据库实体,联系人 */ @Data @TableName("ts_contacter") public class ContacterEO { /** * 主键 */ private String id; /** * 姓名 */ private String name; /** * 性别(0,女;1,男) */ private String sex; /** * 年龄 */ private Integer age ; /** * 电话 */ private String phone; /** * 地址 */ private String location; /** * 删除(0,未删除;1,删除) */ private String dflag ; } # 3.创建dao # 这里需要继承BaseMapper<>接口, 继承BaseMapper接口之后,会继承一些CRUD方法,我们就可以直接调用了。 package com.youyou.address.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.youyou.address.entity.ContacterEO; import java.util.List; public interface ContacterDao extends BaseMapper<ContacterEO> { } # 4.创建service # package com.youyou.address.service; import com.youyou.address.dao.ContacterDao; import com.youyou.address.entity.ContacterEO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class ContacterService { @Autowired private ContacterDao dao; /** * 保存联系人 * * @param contacterEO */ public void saveOne(ContacterEO contacterEO) { dao.insert(contacterEO); } } # 5.创建controller # package com.youyou.address.controller; import com.youyou.address.entity.ContacterEO; import com.youyou.address.service.ContacterService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Api(description = "联系人接口") @RestController @RequestMapping(value = "/contacter") public class ContacterController { @Autowired private ContacterService service; @ApiOperation(value = "新增") @GetMapping("/add") public ContacterEO add(ContacterEO contacterEO) { System.out.println("添加一个联系人"); service.saveOne(contacterEO); return contacterEO; } } 这样我们就已经配置完成了,无需配置Mapper的XML文件就能使用CRUD方法了。 接下来介绍一下自定义方法的配置。 # 6.配置application.properties文件 # #设置mapper文件的位置 mybatis-plus.mapper-locations: classpath*:mapper/*.xml # 7.创建Mapper文件,文件放到mapper文件夹下。 # <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.youyou.address.dao.ContacterDao" > <select id="findAll" resultType="com.youyou.address.entity.ContacterEO"> select * from ts_contacter </select> </mapper>
还没有评论,来说两句吧...