<!--确定spring boot的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<!-- web 开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MySQL数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--支持lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud_db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志
map-underscore-to-camel-case: true #驼峰命名
global-config:
db-config:
table-prefix: tmp_ #表名前缀
type-aliases-package: com.czxy.domain #别名包扫描路径
mapper-locations: classpath*:/mapper/**/*.xml #映射文件位置
数据库和表
CREATE DATABASE cloud_db1;
USE cloud_db1;
CREATE TABLE `tmp_customer` (
`cid` INT(11) NOT NULL AUTO_INCREMENT,
`cname` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(32) DEFAULT NULL,
`telephone` VARCHAR(11) DEFAULT NULL,
`money` DOUBLE DEFAULT NULL,
`version` INT(11) DEFAULT NULL,
`create_time` DATE DEFAULT NULL,
`update_time` DATE DEFAULT NULL,
PRIMARY KEY (`cid`)
);
INSERT INTO `tmp_customer`(`cid`,`cname`,`password`,`telephone`,`money`,`version`,`create_time`,`update_time`)
VALUES (1,'jack','1234','110',1000,NULL,NULL,NULL),(2,'rose','1234','112',1000,NULL,NULL,NULL),(3,'tom','1234','119',1000,NULL,NULL,NULL);
配置JavaBean
-
@TableName
表名注解,value属性设置表名
package com.czxy.domain;
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 lombok.Data;
import java.util.List;
@Data
@TableName("tmp_customer")
public class Customer {
@TableId(type = IdType.AUTO)
private Integer cid;
private String cname;
private String password;
private String telephone;
private String money;
private Integer version;
@TableField(exist = false)
private List<Integer> ids;
}
编写dao
package com.czxy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.mp.domain.Customer;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {
}
编写启动类
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
编写测试类
package com.czxy;
import com.czxy.mp.MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.mapper.CustomerMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class TestDemo01 {
@Resource
private CustomerMapper customerMapper;
// 查询所有
@Test
public void testFindAll() {
List<Customer> list = customerMapper.selectList(null);
list.forEach(System.out::println);
}
@Test // Map条件查询
public void testMap(){
Map map = new HashMap();
//map.put("cname","rose");
map.put("password","1234");
List list = customerMapper.selectByMap(map);
list.forEach(System.out::println);
}
@Test // 添加
public void testInsert(){
Customer customer = new Customer();
// 设置什么加什么
customer.setCname("测试");
customerMapper.insert(customer);
System.out.println(customer);
}
@Test //更新
public void testUpdate(){
Customer customer = new Customer();
customer.setCid(4);
customer.setCname("测试666");
customer.setPassword("666");
customer.setMoney(1000d);
customer.setTelephone("111");
// 需要给 Customer 设置 @TableId
customerMapper.updateById(customer);
// 更新所有用户信息
//customerMapper.update(customer,null);
System.out.println(customer);
}
@Test //删除
public void testDelete(){
// 需要给 Customer设置 @TableId
int i = customerMapper.deleteById(4);
// 批量删除
//int i = customerMapper.deleteBatchIds(Arrays.asList(2,3));
System.out.println(i);
}
}