目录
- 一、依赖
- 二、启动类注解
- 三、表结构
- 四、配置文件
- 五、代码
- 1、实体类
- 2、持久层
- 3、服务层
- 4、逻辑层
- 六、测试
一、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.jeffreyning</groupId>
<artifactId>mybatisplus-plus</artifactId>
<version>1.5.1-RELEASE</version>
<scope>compile</scope>
</dependency>
二、启动类注解
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@EnableMPP
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、表结构
CREATE TABLE `product` ( `type` varchar(255) NOT NULL, `number` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`number`,`type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
四、配置文件
server:
port: 8888
#数据库配置
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/avlicy?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&allowMultiQueries=true&rewriteBatchedstatements=true
username: root
password: a123456
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
#mybatis-plus
mybatis-plus:
#映射mapper.xml文件存放路径
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.example.demo.entity.base,com.example.demo.entity.integration
configuration:
#下划线转驼峰配置
map-underscore-to-camel-cas: true
#使用二级缓存容易出现脏读,建议避免使用二级缓存
cache-enabled: false
#指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。
#FULL 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置
auto-mapping-behavior: full
#控制台输出日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
五、代码
1、实体类
@Data
public class Product implements Serializable {
@MppMultiId
@TableField(value = "type")
private String type;
@MppMultiId
@TableField(value = "number")
private Integer number;
@TableField(value = "name")
private String name;
}
2、持久层
public interface ProductMapper extends MppBaseMapper<Product> {
}
3、服务层
public interface ProductService extends IMppService<Product> {
}
@Service
public class ProductServiceImpl extends MppServiceImpl<ProductMapper, Product> implements ProductService {
}
4、逻辑层
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@PostMapping("/saveProduct")
public List<Product> querySchoolStudent2(@RequestBody List<Product> product){
productService.saveOrUpdateBatchByMultiId(product);
return product;
}
}
六、测试

Creating a new SqlSession Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring ==> Preparing: SELECT type,number,name FROM product WHERE number=? and type=? ==> Parameters: 1(Integer), 电脑(String) <== Columns: type, number, name <== Row: 电脑, 1, 红米 <== Total: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@391601d9] will be managed by Spring ==> Preparing: UPDATE product SET type=?, number=?, name=? WHERE number=? and type=? ==> Parameters: 电脑(String), 1(Integer), 红米(String), 1(Integer), 电脑(String) Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] from current transaction ==> Preparing: SELECT type,number,name FROM product WHERE number=? and type=? ==> Parameters: 1(Integer), 冰箱(String) <== Columns: type, number, name <== Row: 冰箱, 1, 美的 <== Total: 1 Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] ==> Parameters: 冰箱(String), 1(Integer), 美的(String), 1(Integer), 冰箱(String) Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@585df33b]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)