这篇文章主要介绍如何搭建boot+MybatisPlus,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创建表
CREATE TABLE `login`(
`id` INT(4) primary key auto_increment,
`login_id` VARCHAR(50) UNIQUE,
`city` VARCHAR(50) DEFAULT '富平',
`password` VARCHAR(50)
)
在可视化工具中添加数据(我不太会写sql)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
spring: datasource: url: jdbc:mysql://localhost:3306/test0314?characterEncoding=utf-8&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver
该接口中提供了常用的crud方法,我们只需要从容器中获取mapper操作数据即可
package com.hand.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hand.demo.entity.User;
/**
* 用户数据访问层接口
* */
public interface UserMapper extends BaseMapper<User> {
}
在启动类中配置我们的mapper在哪个包
两种方法:@Mapper注解(麻烦);@MapperScan(在主启动类上进行配置)
@SpringBootApplication
@MapperScan("com.hand.demo.mapper")
public class Demo0318Application {
public static void main(String[] args) {
SpringApplication.run(Demo0318Application.class, args);
}
}
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
在test包下
package com.hand.demo;
import com.hand.demo.entity.User;
import com.hand.demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class Demo0318ApplicationTests {
@Autowired
private UserMapper userMapper;
/**
* 获取UserMapper实现类对象(mybatisPlus容器会使用动态代理生成该接口的实现类对象,并注入到spring容器中
* 所以我们只需要在这定义一个成员变量,通过注解自动注入即可)
* */
@Test
public void testQueryAll() {
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
}
设置表前缀配置
@TableId(type = IdType.AUTO)
private Long id;
mybatis-plus:
global-config:
db-config:
table-prefix:
id-type: auto
mybatis-plus:
global-config:
db-config:
table-prefix:
id-type: auto
configuration:
map-underscore-to-camel-case: false
mybatis-plus:
global-config:
db-config:
table-prefix:
id-type: auto
configuration:
map-underscore-to-camel-case: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Wrapper AbstractWrapper QueryWrapper UpdateWrapper
QueryWrapper的select可以设置需要查询的列
不需要手动注入该泛型内的mapper
如果需要别的mapper手动注入就行
package com.hand.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hand.demo.entity.User;
public interface UserService extends IService<User> {
}
package com.hand.demo.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hand.demo.entity.User;
import com.hand.demo.mapper.UserMapper;
import com.hand.demo.service.UserService;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@Autowired
private UserService userService;
@Test
public void testService() {
List<User> list = userService.list();
System.out.println(list);
}
也有自己的批量操作等(batch)
自定义方法(多表关联)
每个接口都在继承相同的BaseMapper,IService(代码冗余,繁琐)
MybatisPlus提供的代码生成器,一键生成mvc三层所有代码
如何使用,引入下边的包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
以上是“如何搭建boot+MybatisPlus”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。