mybatisplus连接sqlserver数据源
目录
一、目录结构
二、pom文件
- 增加mybatis插件
- 增加sqlserver驱动
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--SqlServer 驱动-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
三、application.yml
- 增加datasource数据源
server:
port: 8080
spring:
application:
name: rapid-demo
datasource:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://127.0.0.1:1433;databaseName=rapid_demo
username: sa
password: 123456
四、Application添加MapperScan扫描
@MapperScan("com.cloud.rapid.screen.mapper")
@SpringBootApplication
public class RapidDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RapidDemoApplication.class, args);
}
}
五、添加entity类
/**
* 用户表
*/
@Data
public class SysUser {
@TableId(type = IdType.AUTO)
private String id;
/**
* 姓名
*/
private String name;
}
六、添加Mapper接口
/**
* 用户信息
*/
public interface SysUserMapper extends BaseMapper<SysUser> {
}
七、添加service接口
1、新建SysUserService接口
/**
* 用户信息
*/
public interface SysUserService extends IService<SysUser> {
/**
* 列表查詢
*
* @param name 姓名
* @return
*/
List<SysUserDTO> listByQuery(String name);
}
2、impl下新建实现类
/**
* 用户信息
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
/**
* 列表查詢
*
* @param name 姓名
* @return
*/
@Override
public List<SysUserDTO> listByQuery(String name) {
LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
wrapper.like(!StringUtils.isEmpty(name), SysUser::getName, name);
List<SysUser> data = this.list(wrapper);
return BeanUtils.convertProperties(data, SysUserDTO.class);
}
}
八、controller调用
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
SysUserService userService;
@GetMapping("/listByQuery")
public ApiResponse<List<SysUserDTO>> listByQuery(String name) {
List<SysUserDTO> data = userService.listByQuery(name);
return ApiResponse.ok(data);
}
}