作者:手机用户2502887197 | 来源:互联网 | 2023-07-15 07:28
这篇文章主要为大家详细介绍了springboot整合mybatisplus的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够
目录
- POM:
- application.yaml:
- POJO:
- mapper接口:
- 包扫描:
- 测试:
- 总结
POM:
com.baomidou
mybatis-plus-boot-starter
3.5.1
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
如下图所示,mybatis-plus-boot-starter
已经引入了jdbc 所以无需在引入 jdbc的依赖
application.yaml:
只需要 填写数据源配置即可 。
spring:
datasource:
url: jdbc:mysql://localhost:3306/kaoyan?serverTimezOne=UTC
password: admin
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
POJO:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName(value = "user") // 指定查询的表名为 user
public class UserPlus {
// 以下为 mybatis-plus测试
private Integer id;
private String name;
private Integer age;
private String email;
}
mapper接口:
特别简洁
package com.hoan.demo03242.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hoan.demo03242.bean.UserPlus;
public interface UserPlusMapper extends BaseMapper {
}
包扫描:
测试:
@SpringBootTest
class Demo03242ApplicationTests {
@Autowired
UserPlusMapper userPlusMapper;
@Test
void contextLoads() {
UserPlus user = userPlusMapper.selectById(1);
System.out.println(user.toString());
}
}
总结
目录
- POM:
- application.yaml:
- POJO:
- mapper接口:
- 包扫描:
- 测试:
- 总结
POM:
com.baomidou
mybatis-plus-boot-starter
3.5.1
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.4
如下图所示,mybatis-plus-boot-starter
已经引入了jdbc 所以无需在引入 jdbc的依赖
application.yaml:
只需要 填写数据源配置即可 。
spring:
datasource:
url: jdbc:mysql://localhost:3306/kaoyan?serverTimezOne=UTC
password: admin
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
POJO:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName(value = "user") // 指定查询的表名为 user
public class UserPlus {
// 以下为 mybatis-plus测试
private Integer id;
private String name;
private Integer age;
private String email;
}
mapper接口:
特别简洁
package com.hoan.demo03242.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hoan.demo03242.bean.UserPlus;
public interface UserPlusMapper extends BaseMapper {
}
包扫描:
测试:
@SpringBootTest
class Demo03242ApplicationTests {
@Autowired
UserPlusMapper userPlusMapper;
@Test
void contextLoads() {
UserPlus user = userPlusMapper.selectById(1);
System.out.println(user.toString());
}
}
总结