CREATE TABLE `user` ( `id` bigint(20) NOT NULL COMMENT '主键ID', `name` varchar(30) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', `email` varchar(50) DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
建表语句,创建后随便添加几条数据
创建Spring Initializr项目
选择Eureka Discovery Client
配置maven
刷新jar包
配置文件application.properties配置如下
server.port=10001 spring.application.name=eureka-user #注册的eureka.Service信息 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/ #注册成ip #spring cloud不同版本的配置可能不同,比较老的版本是eureka.instance.preferIpAddress=true #不行的话需要配置eureka.instance.instanceId eureka.instance.preferIpAddress=true spring.cloud.client.ipAddress=127.0.0.1 eureka.instance.instanceId=${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port} #################mysql################# #指定时区和编码 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lianxi?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver #mybatis-plus 开启SQL语句打印 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
项目路径如下
pom.xml文件
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>provided</scope> </dependency>
启动类
@EnableEurekaClient @SpringBootApplication public class EurekaUserApplication { public static void main(String[] args) { SpringApplication.run(EurekaUserApplication.class, args); } }
UserMapper
@Mapper public interface UserMapper extends BaseMapper<User> { }
User
@Data public class User implements Serializable { /** */ private Integer id; /** */ private String name; /** */ private String age; private String email; }
UserAction
@RestController @RequestMapping("/user") public class UserAction { @Value("${server.port}") String port; @Resource private UserMapper userMapper; @RequestMapping("/hi") public String hi(@RequestParam String name) { System.out.println(("----- selectAll method test ------")); List<User> userList = userMapper.selectList(null); userList.forEach(System.out::println); return "hi " + name + ",i am from port:" + port; } }
启动项目访问
http://127.0.0.1:10001/user/hi?name=ok
启动项目可能会报lombok找不到,这时需要把maven里的<localRepository>配置注释掉。idea2020.01无法识别自行设置的localRepository ,所以启动的时候去默认.m2去找jar包,maven下载的包在配置的目录下导致找不到。