栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

springboot整合mybatis增删改查(四):完善增删改查及整合swgger2

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis增删改查(四):完善增删改查及整合swgger2

接下来就是完成增删改查的功能了,首先在config包下配置Druid数据连接池,在配置之前先把相关配置在application.preperties中完善

application.preperties
# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=30# 配置获取连接等待超时的时间spring.datasource.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECt 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testonBorrow=false
spring.datasource.testonReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙spring.datasource.filters=stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多个DruidDataSource的监控数据spring.datasource.useGlobalDataSourceStat=true# Druid 监控 Servlet 配置参数spring.datasource.druidRegistrationUrl: /druid
    public List getAllUser();    
    void saveUser(User user);    
    User getById(Integer id);    
    Boolean checkUserName(String userName);    
    void updateUser(User user);    
    void deleteUser(Integer id);    
    void deleteBatchUser(List useridList);
}

UserServiceImpl实现类

@Service@Transactionalpublic class UserServiceImpl implements UserService {    //注入
    @Autowired
    private UserMapper userMapper;    
    @Override
    public List getAllUser() {
        List users = userMapper.selectByExample(null);        return users;
    }    
    @Override
    public User getById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);        return user;
    }    
    @Override
    public void saveUser(User user) {
        userMapper.insertSelective(user);
    }    
    @Override
    public Boolean checkUserName(String userName) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(userName);        long count=userMapper.countByExample(example);        if(count==0){            return true;
        }        return false;
    }    
    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }    
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }    
    @Override
    public void deleteBatchUser(List useridList) {      

    }
}
UserController
@RestController@RequestMapping(value = "/user")public class UserController {    //注入
    @Autowired
    private UserService userService;    
    @ApiOperation(value="获取用户列表")    @RequestMapping(value = "/user",method = RequestMethod.GET)    public List getListAll(){
        List listAll = userService.getAllUser();        return listAll;
    }    
    @ApiOperation(value = "添加用户",notes = "根据user添加用户")    @ApiImplicitParam(name = "user",value = "用户user",required = true,dataType = "User")    @RequestMapping(value = "/users",method = RequestMethod.POST)    public String saveUser(@RequestBody User user){
        userService.saveUser(user);        return "success";
    }    
    @ApiOperation(value = "根据id查询")    @ApiImplicitParam(name = "id",value = "用户id")    @RequestMapping(value = "/{id}",method = RequestMethod.GET)    public User getById(@PathVariable("id") Integer id){
        User user = userService.getById(id);        return user;
    }    
    @ApiOperation(value = "校验用户名")    @ApiImplicitParam(name = "userName",value = "用户名",required = true,dataType = "String")    @RequestMapping(value = "/{username}",method = RequestMethod.POST)    public Boolean checkUserName(@PathVariable("username")String username){
        Boolean aboolean = userService.checkUserName(username);        if (aboolean){            return true;
        }else {            return false;
        }
    }    
    @ApiOperation(value = "修改用户")    @ApiImplicitParam(name = "user",value = "用户",required = true,dataType = "User")    @RequestMapping(value = "/user",method = RequestMethod.PUT)    public String updateUser(@RequestBody User user){
        userService.updateUser(user);        return "success";
    }    
    @ApiOperation(value = "根据id删除用户")    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer")    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)    public String deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);        return "success";
    }
}

controller类中使用了swgger2如下:

springboot中整合swgger2

pom.xml

        
            io.springfox
            springfox-swagger2
            2.2.2
        
        
            io.springfox
            springfox-swagger-ui
            2.2.2
        

springbootmybatis包下创建SwaggerConfig.java

SwaggerConfig
@Configuration@EnableSwagger2public class SwaggerConfig {    @Bean
    public Docket createRestApi() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("使用Swagger2构建RESTful APIs") //标题
                .description("客户端与服务端接口文档") //描述
                .termsOfServiceUrl("http://localost:8080") //域名地址
                .contact("姜飞祥") //作者
                .version("1.0.0") //版本号
                .build();        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.springbootmybatis"))
                .paths(PathSelectors.any())
                .build();
    }

}

以上就算完成了,写的不好请见谅。具体测试请参考下面的springboot整合swgger2,之后访问http://localhost:8080/swagger-ui.html即可,和

备注:
  • springboot整合swgger2参考:https://www.jianshu.com/p/57a4381a2b45

  • MyBatis的Mapper接口以及Example的实例函数及详解:https://blog.csdn.net/biandous/article/details/65630783

  • Mybatis Generator最完整配置详解:https://www.jianshu.com/p/e09d2370b796

原文出处:https://www.cnblogs.com/smfx1314/p/10334335.html 

作者:姜飞祥

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号