赞
踩
备注:采用的是mysql8
CREATE TABLE `t_account` (
`id` int NOT NULL,
`user_name` varchar(255) DEFAULT NULL,
`balance` decimal(10,2) DEFAULT NULL COMMENT '账户余额',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
备注:本文建表采用的是可视化工具navicat界面中创建,并非上述sql,在此贴出,只是为了方便,该SQL正确性待证,见谅!
<?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>org.example</groupId> <artifactId>spring-boot-test</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.14</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> </dependencies> </project>
备注:springboot项目搭建见往期文章快速搭建springboot项目
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
package com.example.entity; import java.io.Serializable; import com.baomidou.mybatisplus.annotation.TableField; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; /** * @description (Account)表实体类 * @author wangsm */ @Data @AllArgsConstructor @NoArgsConstructor @TableName("t_account") public class Account { @TableId(value = "id", type = IdType.ASSIGN_ID) private Long id; @TableField("user_name") private String userName; // 账户余额 @TableField("balance") private BigDecimal balance; }
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Account;
/**
* @description (Account)表数据库访问层
* @author wangsm
*/
public interface AccountMapper extends BaseMapper<Account> {
}
package com.example.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.entity.Account;
/**
* @description (Account)表服务接口
* @author wangsm
*/
public interface AccountService extends IService<Account> {
}
package com.example.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.entity.Account; import com.example.mapper.AccountMapper; import com.example.service.AccountService; import org.springframework.stereotype.Service; /** * @description (Account)表服务实现类 * @author wangsm */ @Service("accountService") public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService { }
server:
port: 8888
spring:
datasource:
url: jdbc:mysql://ip:3306/transaction?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: 用户名
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
# 打印 mp 执行sql的日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@MapperScan("com.example.mapper")
备注:com.example.mapper 表示mapper接口所在的包
说明:为了测试更加方便,引入springboottest依赖,测试环境是否搭建正确。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
在添加依赖后,刷新maven,可在右侧maven中看到test的依赖。
package com.example.mapper; import com.example.entity.Account; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.math.BigDecimal; /** * @author wangsm * @description 账户mapper测试类 */ @Slf4j @SpringBootTest public class AccountMapperTest { @Autowired private AccountMapper accountMapper; @Test void test(){ Account entity = new Account(); entity.setUserName("张三"); entity.setBalance(new BigDecimal("1000.00")); int insertResult = accountMapper.insert(entity); log.info("添加结果->{}", insertResult == 1 ? "成功" : "失败"); } }
备注:其他查、改、删作可自行测试,在此不做展示!
若有错误,欢迎指正,十分感谢!原创不易,感谢点赞关注
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。