Python 官方文档:入门教程 => 点击学习
目录1.准备工作1.1 创建数据库表1.2 创建boot项目1.3 创建实体类(映射数据库表)2.使用mybatisPlus(操作数据库)2.1 添加mybatisPlus依赖2.2
创建表
CREATE TABLE `login`(
`id` INT(4) primary key auto_increment,
`login_id` VARCHAR(50) UNIQUE,
`city` VARCHAR(50) DEFAULT '富平',
`passWord` VARCHAR(50)
)
<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> {
}
@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;
@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可以设置需要查询的列
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);
}
<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的文章就介绍到这了,更多相关boot+MybatisPlus搭建内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 零基础搭建boot+MybatisPlus的详细教程
本文链接: https://lsjlt.com/news/143129.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0