一、提供数据的客户端需要连接数据了,因此需要我们使用mybatis了,等下使用idea生成mybaits和web的依赖
二、提供数据的客户端项目
1.创建项目
2.选择idea自动给我们生成的依赖
3. 确认工程名称和模块名称
4.查看项目结构
5.修改pom的依赖
cn.kgc eureka-common-school 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 org.springframework.cloud spring-cloud-starter-netflix-eureka-client mysql mysql-connector-java 5.1.38 org.springframework.boot spring-boot-starter-test test
6.按照如下结构编写代码
7.编写数据访问层接口ClassesMapper
package cn.kgc.mapper;import cn.kgc.vo.Classes;import org.apache.ibatis.annotations.Select;import java.util.List;public interface ClassesMapper { @Select("select * from classes") ListselectClasses();}
8.编写数据访问层接口StudentMapper
package cn.kgc.mapper;import cn.kgc.vo.Student;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;import java.util.Map;public interface StudentMapper { //所有查询 List
9.编写业务层接口ClassesService
package cn.kgc.service;import cn.kgc.vo.Classes;import java.util.List;public interface ClassesService { ListshowOptions();}
10..编写业务层接口StudentService
package cn.kgc.service;import cn.kgc.vo.Student;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Update;import java.util.List;import java.util.Map;public interface StudentService { //所有查询 List
11.编写业务层接口实现类ClassesServiceImpl
package cn.kgc.service;import cn.kgc.mapper.ClassesMapper;import cn.kgc.vo.Classes;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class ClassesServiceImpl implements ClassesService{ @Autowired private ClassesMapper mapper; @Override public ListshowOptions() { return mapper.selectClasses(); }}
12.编写业务层接口实现类StudentServiceImpl
package cn.kgc.service;import cn.kgc.mapper.StudentMapper;import cn.kgc.vo.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import java.util.List;import java.util.Map;@Servicepublic class StudentServiceImpl implements StudentService{ @Autowired private StudentMapper mapper; @Override public List
13.映射xml文件StudentMapper.xml
14.编写属性配置文件application.properties
#eureka的相关配置#使用feign时报错Service id not legal hostname(xx_sss)#原因是feign不支持下划线"_",支持"-",改成xx-sss即可#spring.application.name表示当前微服务注册到Eureka Server中的名字,同事需要制定Eureka Server地址spring.application.name=client-school-providerserver.port=8762eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/#数据源相关配置spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://192.168.117.134:3306/kgcspring.datasource.username=rootspring.datasource.password=ok#mybatis相关配置#映射xml文件映射地址mybatis.mapper-locations=mapper/*.xml#别名配置mybatis.type-aliases-package=cn.kgc.vo
15.编写控制类文件 CenterController
package cn.kgc.controller;import cn.kgc.mapper.ClassesMapper;import cn.kgc.service.ClassesService;import cn.kgc.service.StudentService;import cn.kgc.vo.Classes;import cn.kgc.vo.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestControllerpublic class CenterController { @Autowired private ClassesService classesService; @Autowired private StudentService studentService; @RequestMapping("/options.do") public ListoptionsData() { return classesService.showOptions(); } @RequestMapping("/data.do") public List
16.编写启动类注解和扫描包路径
package cn.kgc;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient@MapperScan("cn.kgc.mapper")@SpringBootApplicationpublic class EurekaClientProviderApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientProviderApplication.class, args); }}
17.启动你的eureka-server服务端,再启动eureka-client客户端,然后现查看服务器端是否收到客户端的注册信息
此帖子为原创
作者:红酒人生
转载请注明出处:https://www.cnblogs.com/holly8/p/11012773.html