1.创建SpringBoot项目
创建时勾选依赖
- devtools
- lombok
- spring-web
- mysql-driver
2.导入SpringBoot集成MyBatisPlus依赖
<!-- SpringBoot集成MyBatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
3.配置application.properties文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/gamedb?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
# 开启sql日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 无需加入开启驼峰命名映射,MyBatisPlus默认使用驼峰命名进行属性-字段映射
#mybatis-plus.configuration.map-underscore-to-camel-case=true
4.根据数据表创建实体类
实体类的属性名命名方式:
- MyBatisPlus默认使用驼峰命名对字段和属性进行映射。如将字段stu_name对应的属性写为stuName
- 如果字段名和属性名不一致,在属性名上加入*@TableField(value = "字段名")*
- 主键字段对应的属性,需要加入@TableId注解,其type属性表示主键生成策略
- @TableId(type = IdType.AUTO)表示主键自增,在数据库也要将主键设置为自增
- @TableId(type = IdType.ASSIGN_ID)//IdType.ASSIGN_ID表示使用"雪花算法"(根据时间和机器特征码)生成一个id
- @TableId(type = IdType.ASSIGN_UUID)//IdType.ASSIGN_UUID表示使用UUID生成一个随机字符串id
@Data
public class Hero {
//type表示主键生成策略,
@TableId(type = IdType.AUTO)// IdType.AUTO表示主键自增,在数据库也要将主键设置为自增
//@TableId(type = IdType.ASSIGN_ID)//IdType.ASSIGN_ID表示使用"雪花算法"(根据时间和机器特征码)生成一个id
//@TableId(type = IdType.ASSIGN_UUID)//IdType.ASSIGN_UUID表示使用UUID生成一个随机字符串id
private Integer id;
//如果属性名和字段名不一致
@TableField(value = "name")
private String heroName;
private String position;
private String sex;
private Integer price;
private String shelfDate;
}
5.编写数据访问层接口
可以不用写@Repository,继承BaseMapper接口,设置泛型
/*
* 数据访问层可以称为dao或mapper层
* 可以不用加@Repository注解
* */
public interface HeroMapper extends BaseMapper<Hero> {
}
6.在SpringBoot的启动类中,扫描数据访问层所在包
@SpringBootApplication
@MapperScan("com.hqyj.sbmp01.mapper")
public class Sbmp01Application {
public static void main(String[] args) {
SpringApplication.run(Sbmp01Application.class, args);
}
}
测试
在SpringBoot自带的单元测试类中,注入HeroMapper对象,调用BaseMapper中定义的方法即可实现CURD。
BaseMapper接口
BaseMapper接口中定义了常用的增删改查方法,
在数据访问层接口中继承该接口
方法列表
使用
public interface HeroMapper extends BaseMapper<Hero> {
}
BaseMapper接口中的常用方法
方法名 | 参数 | 作用 |
---|---|---|
selectList(Wrapper wrapper) | 条件构造器 | 根据条件查询集合,如果实参为null表示查询所有,返回List集合 |
selectById(Serializable id) | 主键 | 根据主键查询单个对象,返回单个对象 |
selectOne(Wrapper wrapper) | 条件构造器 | 条件查询单个对象,返回单个对象 |
insert(T entity) | 实体对象 | 添加单个实体 |
updateById(T entity) | 实体对象 | 根据实体对象单个修改,对象必须至少有一个属性和主键 |
update(T entity,Wrapper wrapper) | 实体对象和条件构造器 | 根据条件修改全部,对象必须至少有一个属性 |
deleteById(Serializable id/T entity) | 主键/实体对象 | 根据主键删除单个对象 |
deleteBatchIds(Collection ids) | 主键集合 | 根据集合删除 |
delete(Wrapper wrapper) | 条件构造器 | 根据条件删除,如果实参为null表示无条件删除所有 |
测试
package com.hqyj.sbmp01;
import com.hqyj.sbmp01.entity.Hero;
import com.hqyj.sbmp01.mapper.HeroMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class Sbmp01ApplicationTests {
@Autowired
//@Resource
private HeroMapper mapper;
@Test
void contextLoads() {
//查询所有
List<Hero> list = mapper.selectList(null);
for (Hero hero : list) {
System.out.println(hero);
}
}
@Test
void test1() {
//根据主键查询单个对象
System.out.println(mapper.selectById(20));
}
@Test
void test2() {
//根据id删除,参数为id
//System.out.println(mapper.deleteById(20));
//根据id删除,参数为带有id的对象
/*Hero hero = new Hero();
hero.setId(100);
System.out.println(mapper.deleteById(hero));*/
//删除所有
//mapper.delete(null);
//根据id集合删除
ArrayList<Integer> list = new ArrayList<>();
list.add(99);
list.add(100);
list.add(88);
list.add(69);
System.out.println(mapper.deleteBatchIds(list));
}
@Test
void test3() {
//根据id修改
//参数为一个对象,这个对象至少要有一个非主键属性有值
Hero hero = new Hero();
hero.setHeroName("a");
hero.setPosition("b");
hero.setSex("c");
hero.setShelfDate("d");
hero.setPrice(123);
hero.setId(999);
//System.out.println(mapper.updateById(hero));
// 修改全部数据
// mapper.update(修改后的对象,条件构造器)
mapper.update(hero,null);
}
@Test
void test4() {
Hero hero = new Hero();
hero.setHeroName("asdfsdfsdf");
hero.setPosition("b");
hero.setSex("c");
hero.setShelfDate("1999-9-9");
hero.setPrice(123);
mapper.insert(hero);
}
}
IService接口
IService接口减少业务逻辑层的代码,并对BaseMapper进行了拓展
在业务流程类中继承该接口
部分方法列表
使用
-
1.创建一个业务层接口,继承IService接口,设置泛型
/* * 创建业务逻辑层接口,继承IService<T>接口,设置泛型 * */ public interface HeroService extends IService<Hero> { }
-
2.创建一个业务层接口的实现类,添加@Service注解,继承 ServiceImpl<M, T>,实现上一步创建的接口
- M是数据访问层接口
- T是实体类
/* * 1.添加@Service * 2.继承ServiceImpl<M, T> M是Mapper类 T是实体类 * 3.实现自定义Service接口 * */ @Service public class ImpHeroService extends ServiceImpl<HeroMapper, Hero> implements HeroService { }
IService接口中的常用方法
方法 | 作用 |
---|---|
list() | 无条件查询所有 |
list(Wrapper wrapper) | 条件查询素有 |
page(Page page) | 无条件分页查询,Page是分页模型对象 |
page(Page page,Wrapper wrapper) | 条件分页查询,Page是分页模型对象 |
getById(Serializable id) | 根据主键查询单个对象 |
getOne(Wrapper wrapper) | 条件查询单个对象 |
save(T entity) | 添加单个对象 |
save(Collection col) | 批量添加对象的集合 |
updateById(T entity) | 修改,参数至少有一个属性值和主键 |
saveOrUpdate(T entity) | 添加或修改。如果实参对象的主键值不存在则添加,存在则修改 |
update(T entity,Wrapper wrapper) | 条件修改,条件为null则修改全部数据 |
removeById(Serializable id/T entity) | 根据主键或包含主键的对象删除 |
removeBatchByIds(Collection ids) | 根据集合删除 |
remove(Wrapper wrapper) | 根据条件删除,条件为null则删除全部 |
测试
package com.hqyj.sbmp01;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.sbmp01.entity.Hero;
import com.hqyj.sbmp01.mapper.HeroMapper;
import com.hqyj.sbmp01.service.HeroService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
class IServiceTest {
@Autowired
private HeroService heroService;
@Test
void test1() {
//无条件查询所有
//heroService.list().forEach(System.out::println);
//条件查询,如果条件为null表示查询所有
//heroService.list(null).forEach(System.out::println);
//根据主键查询
//Hero hero = heroService.getById(22);
//System.out.println(hero);
//根据条件查询,如果条件为null表示查询所有,会导致异常
//System.out.println(heroService.getOne(null));
}
@Test
void test2() {
//添加save(T entity);参数对象至少要有一个属性值
//添加后,主键自增的值会自动赋值给主键属性
/*
Hero hero = new Hero();
hero.setHeroName("cvb");
hero.setSex("男");
hero.setPrice(2000);
hero.setPosition("战士");
heroService.save(hero);
System.out.println(hero);
*/
//批量添加,参数为要添加的对象集合
/* Hero h1 = new Hero(0, "1", "1", "1", 100, "1999-9-9");
Hero h2 = new Hero(0, "2", "1", "1", 100, "1999-9-9");
Hero h3 = new Hero(0, "3", "1", "1", 100, "1999-9-9");
List<Hero> heroes = Arrays.asList(h1, h2, h3);
heroService.saveBatch(heroes);*/
Hero h1 = new Hero(0, "666", "1", "1", 100, "1999-9-9");
//如果添加的对象主键值已存在执行修改,不存在则添加
heroService.saveOrUpdate(h1);
}
@Test
void test3(){
//根据对象修改
Hero h1 = new Hero(999, "修改后", "修改后", "女", 100, "1999-9-9");
//如果对象主键值已存在执行修改,不存在则添加
//heroService.saveOrUpdate(h1);
//根据主键修改
heroService.updateById(h1);
//根据条件修改
//heroService.update(null);
//根据实体和条件修改,如果条件为空,会将所有数据修改为指定实体的数据
//heroService.update(h1,null);
}
@Test
void test4(){
//根据主键或带有主键值的对象删除
//heroService.removeById(1103302663);
/*Hero hero = new Hero();
hero.setId(1103302662);
heroService.removeById(hero);*/
//批量删除
//heroService.removeBatchByIds(Arrays.asList(77,88,99));
//根据条件删除,条件为null则删除全部
heroService.remove(null);
}
}