创建spring cloud项目1——创建服务提供者
1、创建module——service
右键——new——module——finish
2、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3、创建启动类以及配置文件
可以下载一个idea的插件JBLSpringBootAppGen,可以帮助我们自动创建启动类
启动类
@SpringBootApplication
@MapperScan("com.xzk.user.mapper")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
配置文件
server:
port: ${port:9091}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=UTC
username: root
password: 123456
4、创建实体类
@Data
@Table(name = "tb_user")
public class User {
// id
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// 用户名
private String userName;
}
5、创建mapper
public interface UserMapper extends Mapper<User> {
}
6、创建service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryById(Long id){
return userMapper.selectByPrimaryKey(id);
}
}
7、创建controller
@RestController
@RequestMapping("/user")
public class UsreController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id){
return userService.queryById(id);
}
}