四、启动类 NacosConsumerApplication,@ComponentScan注解可以启动时扫描到相应的类或接口
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @ComponentScan("com.example.nacosprovider.*") public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } }
====加入实体类,@Data插件如果生效的话,就不要这些get set方法
@Data @Api("") public class User { @ApiModelProperty("") private Long id; @ApiModelProperty("") private String username; @ApiModelProperty("") private Integer merchantId; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getMerchantId() { return merchantId; } public void setMerchantId(Integer merchantId) { this.merchantId = merchantId; } }
@Data public class UserSaveOrUpdateReq { @ApiModelProperty("") private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
五、Controller
@RestController public class UserController { @Resource private UserService userService; @ApiOperation("添加或修改") @PostMapping("/user/saveOrUpdate") public Boolean saveOrUpdate(@RequestBody UserSaveOrUpdateReq userSaveOrUpdateReq) { return userService.saveOrUpdate(userSaveOrUpdateReq); } }
六、Service层
public interface UserService extends IService<User> { /** * * @param userSaveOrUpdateReq * @return */ Boolean saveOrUpdate(UserSaveOrUpdateReq userSaveOrUpdateReq); }
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Resource private UserMapper userMapper; @Override public Boolean saveOrUpdate(UserSaveOrUpdateReq userSaveOrUpdateReq) { User user = new User(); user.setUsername(userSaveOrUpdateReq.getUsername()); user.setMerchantId(1); int insert = userMapper.insert(user); return (insert > 0 ? true : false); } }
七、Mapper层
@Mapper public interface UserMapper extends BaseMapper<User> { }
八、配置类 StudyConfiguration.java
Spring 抛除XML使用注解
<context:Component-Scan base-package=“com.example.nacosprovider.mapper”> </context:Component-Scan>
@Configuration @ComponentScan(basePackages = "com.example.nacosprovider.mapper") public class StudyConfiguration { }
最后 测试通过
数据库增加的数据