1.创建数据库
DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`score` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
)
INSERT INTO `movie` VALUES (1, '美丽人生', '罗伯托·贝尼尼', '9.6', '一个快乐的传说');
INSERT INTO `movie` VALUES (2, '放牛班的春天', ' 克里斯托夫·巴拉蒂', '9.3', ' 歌声伴我心');
INSERT INTO `movie` VALUES (3, '触不可及', '奥利维埃·纳卡什 / 埃里克·托莱达诺', '9.3', '闪亮人生');
INSERT INTO `movie` VALUES (4, '三傻大闹宝莱坞', '拉吉库马尔·希拉尼', '9.2', ': 三个傻瓜');
INSERT INTO `movie` VALUES (5, '怦然心动', '罗伯·莱纳', '9.1', '梧桐树之恋');
INSERT INTO `movie` VALUES (6, '绿皮书', '彼得·法雷里', '8.9', ' 绿簿旅友');
INSERT INTO `movie` VALUES (7, '功夫', ' 周星驰', '8.8', '功夫3D');
INSERT INTO `movie` VALUES (8, '悲伤逆流成河', '郭敬明', '8.8', '逆流成河');
INSERT INTO `movie` VALUES (9, '肖申克的救赎', '弗兰克·德拉邦特', '9.8', '月黑高飞');
INSERT INTO `movie` VALUES (14, '霸王别姬', '陈凯歌', '9.6', '再见,我的妾');
INSERT INTO `movie` VALUES (15, '泰坦尼克号', ' 詹姆斯·卡梅隆', '9.4', '铁达尼号');
INSERT INTO `movie` VALUES (16, '这个杀手不太冷', '吕克·贝松', '9.4', '终极追杀令');
INSERT INTO `movie` VALUES (17, '千与千寻', '宫崎骏', '9.4', '神隐少女');
2.新建一个名为springboot-mybatisplus的springboot工程
![](https://i-blog.csdnimg.cn/blog_migrate/e6bf776a4bc48eabc6d143a8ddbf8750.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d9d47b3c2cf73c1e74b8ef19808a9204.png)
3.配置yml
spring:
datasource:
url: jdbc:mysql:///你的数据库名?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: 你的数据库用户名
password: 你的数据库密码
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
5.创建分页插件
@Configuration
@EnableTransactionManagement
public class MPConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor=new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
6.创建实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Movie {
private Integer id;
private String name;
private String author;
private String score;
private String title;
}
7.创建数据层
@Mapper
public interface MovieMapper extends BaseMapper<Movie> {
}
8.创建服务接口类
public interface MovieService extends IService<Movie> {
}
9.创建服务接口实现类
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
}
10.测试
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
private MovieService movieService;
/**
* 查询所有
*/
@Test
public void testFindAll() {
List<Movie> movieList = movieService.list();
for (Movie movie : movieList) {
System.out.println(movie);
}
}
/**
* 根据id查询
*/
@Test
public void testFindById(){
Movie movie = movieService.getById(17);
System.out.println(movie);
}
/**
* 新增
*/
@Test
public void add(){
Movie movie=new Movie();
movie.setName("勇敢的心");
movie.setAuthor("梅尔·吉布森");
movie.setScore("9.5");
movie.setTitle("惊世未了缘 ");
boolean flag = movieService.save(movie);
System.out.println(flag);
}
/**
* 修改
*/
@Test
public void testUpdateById(){
Movie movie=new Movie();
movie.setId(19);
movie.setName("勇敢的心");
movie.setAuthor("梅尔·吉布森");
movie.setScore("9.4");
movie.setTitle("惊世未了缘 ");
boolean flag = movieService.updateById(movie);
System.out.println(flag);
}
/**
* 删除
*/
@Test
public void testDelete(){
boolean flag = movieService.removeById(19);
System.out.println(flag);
}
/**
* 分页查询
*/
@Test
public void testPage(){
Page<Movie> page=new Page<>(1,5);
movieService.page(page,null);
List<Movie> movieList = page.getRecords();
for (Movie movie : movieList) {
System.out.println(movie);
}
System.out.println("总条数为:"+page.getTotal());
System.out.println("当前页为:"+page.getCurrent());
System.out.println("当前页的条数为:"+page.getSize());
System.out.println("总页数为:"+page.getPages());
}
/**
* 条件查询
*/
@Test
public void testQuery(){
String name="美丽";
QueryWrapper<Movie> queryWrapper=new QueryWrapper();
queryWrapper.like("name",name);
Map<String, Object> movieList = movieService.getMap(queryWrapper);
System.out.println(movieList);
}
}