1、在springboot项目的pom文件中添加依赖
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>你的版本号</version>
</dependency>
<!-- SQL Server JDBC 驱动 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>你的版本号</version>
</dependency>
2、配置数据源
spring:
datasource:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://你的服务器地址:端口号;databaseName=你的数据库名
username: 你的用户名
password: 你的密码
3、添加实体类
这里添加TableField是为了防止mybatis-plus默认把驼峰式命名格式改为下划线格式,导致查询异常
@Data
@TableName("Users")
public class UsersEntity {
@TableField("ID")
private Long id;
@TableField("Name")
private String name;
@TableField("Active")
private Boolean active;
@TableField("Password")
private String password;
}
4、为实体类添加mapper层和service层的代码
示例核心代码
queryWrapper.eq("Name", userName);
5、在controller层中添加CURD访问入口
示例
@GetMapping("/pageList")
public Result pageList(String userName) {
IPage<UsersEntity> iPage = usersService.pageList(userName);
return Result.success(new BootTablePagEntity(iPage));
}
总结:在使用SpingBoot时,主要还是配置文件是否配置正确。