springboot使用@mapper注解

项目结构:
springboot使用@mapper注解_第1张图片

首先

在启动类XxxxApplication类里面,添加使用@mapper注解的包进行扫描:
@MapperScan(“com.springbootcache.mapper”)
例如:(这里,我开启了缓存)

@MapperScan("com.springbootcache.mapper")
@SpringBootApplication
@EnableCaching
public class SpringbootCacheApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringbootCacheApplication.class, args);
	}

}

接着是mapper接口

这样就不用配置mapper.xml文件,使用@mapper注解,代码会简化很多。

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM tb_user WHERE id=#{id}")
    public User getUserById(Integer id);
}

等同于
注意,这里同样需要在启动类添加注解@MapperScan(“com.springbootcache.mapper”)

@Component
public interface UserDao {
    public User getUserById(Integer id);
}

在resources里面新建mapper文件夹存放mapper.xml映射文件




	

还有在application.properties配置文件里面

#映射表
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.springbootcache.domain

下面是缺少的代码

domain包下(pojo):

@Data
public class User implements Serializable {
    private Integer id;
    private String name;
    private int age;
}

controller包下(负责与用户进行交互):

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id") Integer id){
        return userService.getUserById(id);
    }
}

config包下(这里配置了swagger接口文档):
有兴趣可以了解一下swagger2技术,很简单的,有以注解的方式。

package com.springbootcache.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage("com.springbootcache.controller"),配置要扫描的包
                //any(),扫描全部
                //none(),什么都不扫描
                //withMethodAnnotation(GetMapping.class),扫描方法上的注解,[GetMapping/RequestMapping/PostMapping]
                //withClassAnnotation(),扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.springbootcache.controller"))
                //paths(),过滤什么路径
               // .paths(PathSelectors.ant("/springbootcache/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact contact=new Contact("lee","https://blog.csdn.net/weixin_43849543","[email protected]");
        return new ApiInfo(
                "lee的API文档",
                "相逢太短,不等茶凉。",
                "1.0",
                "https://blog.csdn.net/weixin_43849543",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

你可能感兴趣的:(springboot,spring,boot)