引言
在現(xiàn)代軟件開發(fā)中,我們經(jīng)常需要處理大量的數(shù)據(jù)。為了有效地管理這些數(shù)據(jù),我們需要使用一些強(qiáng)大的框架。其中,Spring Boot和MyBatis-Plus是兩個非常流行的框架。Spring Boot是一個基于Spring的開源Java框架,可以用于創(chuàng)建獨(dú)立的、生產(chǎn)級別的Spring應(yīng)用。MyBatis-Plus是一個MyBatis的增強(qiáng)工具,它在MyBatis的基礎(chǔ)上增加了許多實(shí)用的功能,如自動分頁、自動填充等。本文將詳細(xì)介紹如何使用Spring Boot整合MyBatis-Plus。
Spring Boot整合MyBatis-Plus的基礎(chǔ)配置
-
引入依賴
在pom.xml
中引入MyBatis-Plus和相關(guān)數(shù)據(jù)庫驅(qū)動的依賴:
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- MySQL Connector 數(shù)據(jù)庫驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
-
配置數(shù)據(jù)源
在application.properties
或application.yml
中配置數(shù)據(jù)源信息:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus
username: root
password: root
server:
port: 8181
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
configuration:
# 日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#是否開啟自動駝峰命名規(guī)則(camel case)映射,即從經(jīng)典數(shù)據(jù)庫列名 A_COLUMN(下劃線命名)到經(jīng)典 Java 屬性名 aColumn(駝峰命名) 的類似映射
map-underscore-to-camel-case: true
溫馨提示
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_ #設(shè)置所有表的通用前綴名稱為tbl_
-
配置MyBatis-Plus
在Spring Boot的主應(yīng)用類上添加@MapperScan
注解,指定Mapper接口所在的包路徑:
@SpringBootApplication
@MapperScan(basePackages = "com.dao")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
使用MyBatis-Plus進(jìn)行數(shù)據(jù)訪問
-
數(shù)據(jù)準(zhǔn)備
CREATE TABLE `user` (
`id` bigint(200) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`create_time` date DEFAULT NULL,
`update_time` date DEFAULT NULL,
`version` bigint(10) DEFAULT NULL,
`status` tinyint(20) DEFAULT NULL,
`deleted` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
-
創(chuàng)建實(shí)體類
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
@Version
private Long version;
private Integer status;
private Integer deleted;
}
-
創(chuàng)建Mapper接口
創(chuàng)建Mapper接口,繼承BaseMapper
接口,無需寫具體的SQL語句。
public interface UserDao extends BaseMapper<User> {
}
-
創(chuàng)建Service接口
public interface UserService extends IService<User> {
}
-
創(chuàng)建Service實(shí)現(xiàn)類
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
}
-
創(chuàng)建Controller類
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> list() {
return userService.list();
}
}
測試應(yīng)用
最后,我們可以運(yùn)行我們的應(yīng)用來進(jìn)行測試。如果我們的應(yīng)用能夠正常運(yùn)行,并且能夠正確地從數(shù)據(jù)庫中獲取數(shù)據(jù),那么我們就可以說我們已經(jīng)成功地使用Spring Boot整合了MyBatis-Plus。
結(jié)語
Spring Boot整合MyBatis-Plus為開發(fā)者提供了一個強(qiáng)大而高效的數(shù)據(jù)訪問解決方案。通過簡單的配置,開發(fā)者可以使用MyBatis-Plus提供的便捷功能,減少了傳統(tǒng)MyBatis開發(fā)中的樣板代碼,同時保留了MyBatis的靈活性。整合MyBatis-Plus的代碼生成器更是提高了開發(fā)效率,使得開發(fā)者能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn)。通過本文的介紹,希望讀者能夠深入理解Spring Boot整合MyBatis-Plus的配置和使用方法,從而更加高效地構(gòu)建健壯的數(shù)據(jù)訪問層。文章來源:http://www.zghlxwxcb.cn/news/detail-804091.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-804091.html
到了這里,關(guān)于Spring Boot整合MyBatis-Plus的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!