栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis-Plus基础入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis-Plus基础入门

*★如果文章对你有帮助【关注点赞❤️收藏⭐】一起努力!★* 
六‍ 个人主页:花棉袄

 官方公众号:Java设计
 本章内容:【MyBatis-Plus】 学习视频推荐 ️配套代码官方文档
✍ 版权: 本文由【花棉袄】原创在CSDN首发需要转载请联系博主

MyBatis-Plus简介

 MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为 简化开发、提高效率而生。
磻磻磻官方地址

框架部署 创建SpringBoot工程

 李添加依赖


    
        org.springframework.boot
        spring-boot-starter
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        com.baomidou
        mybatis-plus-boot-starter
        3.5.1
    
    
        org.projectlombok
        lombok
        true
    
    
        mysql
        mysql-connector-java
        runtime
    

 李配置application.yml

 李驱动类driver-class-name
 李spring boot 2.0(内置jdbc5驱动),驱动类使用:driver-class-name: com.mysql.jdbc.Driver
 李spring boot 2.1及以上(内置jdbc8驱动),驱动类使用:driver-class-name: com.mysql.cj.jdbc.Driver

 李MySQL5.7版本的url:jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
 李MySQL8.0版本的url:jdbc:mysql://localhost:3306/mybatis_plus? serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
 李否则运行测试用例报告如下错误:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&useSSL=false
    username: root
    password: root
启动类
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
创建数据库

 创建user表

CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 添加数据

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
添加实体
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

}
>添加mapper
@Repository
public interface UserMapper extends BaseMapper {
}
>测试
@SpringBootTest
public class MybatisPlusTest {
	@Autowired
	private UserMapper userMapper;
	@Test
	public void testSelectList(){
		//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
		userMapper.selectList(null).forEach(System.out::println);
}
}

添加日志
# 配置MyBatis日志
mybatis-plus:
   configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

*★如果文章对你有帮助【关注点赞❤️收藏⭐】一起努力!★* 

!建在这里插入图片描述](https://img-blog来csdnimg直cn/896967de53c9473aaad1459088346b28接png#pic_center)
![在这里插入图片描述]上https://传(imblog.csdnimg.cn/cf6a5N1461b1a2497badcbeb.gif#pic_center00671)(CUer| 往期回顾| s�amAppDataRoamingTyporatypora-user-imagesimage-20220514192758923.png)]

磻磻�地址癩 |
|:--------:--------
| ❤️Java学习路线❤️| ❤️❤️❤️❤️❤️❤️ |
| 癩S.ringBoot参考手册癩| 癩癩癩癩癩癩 |
| SpringClou.d参考手册| 0_46914264/article/details/124760158
|

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885971.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号