mybatis-plus初次引入

nacos
DongZeLiang 2023-10-08 18:44:22 +08:00
parent dd68d0ab40
commit 3bd782cd38
10 changed files with 177 additions and 0 deletions

View File

@ -53,6 +53,10 @@
<artifactId>ruoyi-generator</artifactId> <artifactId>ruoyi-generator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,5 +1,6 @@
package com.ruoyi; package com.ruoyi;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
* @author ruoyi * @author ruoyi
*/ */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.ruoyi.system.mapper")
public class RuoYiApplication { public class RuoYiApplication {
public static void main (String[] args) { public static void main (String[] args) {
// System.setProperty("spring.devtools.restart.enabled", "false"); // System.setProperty("spring.devtools.restart.enabled", "false");

View File

@ -0,0 +1,29 @@
package com.ruoyi.web.controller.system;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.system.domain.BookInfo;
import com.ruoyi.system.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author DongZl
* @description:
* @Date 2023-10-7 09:37
*/
@RestController
@RequestMapping("/book")
public class BookInfoController {
@Autowired
private BookInfoService bookInfoService;
@PostMapping
public Result add(@RequestBody BookInfo bookInfo){
bookInfoService.save(bookInfo);
return Result.success();
}
}

View File

@ -82,6 +82,14 @@ mybatis:
# 加载全局的配置文件 # 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml configLocation: classpath:mybatis/mybatis-config.xml
mybatis-plus:
# 搜索指定包别名
typeAliasesPackage: com.ruoyi.**.domain
# 配置mapper的扫描找到所有的mapper.xml映射文件
mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
configLocation: classpath:mybatis/mybatis-config.xml
# PageHelper分页插件 # PageHelper分页插件
pagehelper: pagehelper:
helperDialect: mysql helperDialect: mysql

View File

@ -0,0 +1,39 @@
package com.myplus;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.system.domain.BookInfo;
import com.ruoyi.system.mapper.BookInfoMapper;
import com.ruoyi.system.service.BookInfoService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author DongZl
* @description:
* @Date 2023-10-8 04:39
*/
@SpringBootTest(classes = RuoYiApplication.class)
public class BookTestService {
@Autowired
private BookInfoService bookInfoService;
@Autowired
private BookInfoMapper bookInfoMapper;
@Test
public void save(){
bookInfoMapper.insert(
BookInfo.builder()
.name("水浒传")
.price(new BigDecimal("65.36"))
.create_by("zhangsan")
.create_time(new Date())
.build()
);
}
}

View File

@ -41,6 +41,13 @@
<artifactId>pagehelper-spring-boot-starter</artifactId> <artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency> </dependency>
<!-- mybatis - plus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- 自定义验证注解 --> <!-- 自定义验证注解 -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,46 @@
package com.ruoyi.system.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
/**
* @author DongZl
* @description:
* @Date 2023-10-7 09:29
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("book_info")
public class BookInfo {
/**
*
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
*
*/
private String name;
/**
*
*/
private BigDecimal price;
/**
*
*/
private String create_by;
/**
*
*/
private Date create_time;
}

View File

@ -0,0 +1,14 @@
package com.ruoyi.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.BookInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* @author DongZl
* @description: mapper
* @Date 2023-10-7 09:31
*/
@Mapper
public interface BookInfoMapper extends BaseMapper<BookInfo> {
}

View File

@ -0,0 +1,12 @@
package com.ruoyi.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.system.domain.BookInfo;
/**
* @author DongZl
* @description:
* @Date 2023-10-7 09:35
*/
public interface BookInfoService extends IService<BookInfo> {
}

View File

@ -0,0 +1,16 @@
package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.BookInfo;
import com.ruoyi.system.mapper.BookInfoMapper;
import com.ruoyi.system.service.BookInfoService;
import org.springframework.stereotype.Service;
/**
* @author DongZl
* @description:
* @Date 2023-10-7 05:15
*/
@Service
public class BookInfoServiceImpl extends ServiceImpl<BookInfoMapper, BookInfo> implements BookInfoService {
}