博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springcloud(六):Eureka提供数据的客户端连接Docker的mysql
阅读量:5286 次
发布时间:2019-06-14

本文共 8151 字,大约阅读时间需要 27 分钟。

一、提供数据的客户端需要连接数据了,因此需要我们使用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")    List
selectClasses();}

 

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
> selectStudent(Student student); @Insert("INSERT INTO kgc.student (sname, password, subject, result, cid) VALUES (#{sname},#{password},#{subject},#{result},#{classes.cid})") Integer insertStudent(Student student); @Update("UPDATE kgc.student SET sname =#{sname}, password =#{password}, subject =#{subject}, result =#{result}, cid =#{classes.cid} WHERE sid =#{sid}") Integer updateStudent(Student student); @Delete("delete from sudent where sid=#{sid}") Integer deleteStudent(Integer sid);}

 

9.编写业务层接口ClassesService

package cn.kgc.service;import cn.kgc.vo.Classes;import java.util.List;public interface ClassesService {    List
showOptions();}

 

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
> showData(); Map
showInfo(Integer key); Map
login(String sname,String password); Integer addData(Student student); Integer editData(Student student); Integer delData(Integer key);}

 

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 List
showOptions() { 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
> showData() { return mapper.selectStudent(new Student()); } @Override public Map
showInfo(Integer key) { Student student=new Student(key,null,null,null,null,null); Map
map=mapper.selectStudent(student).get(0); return map; } @Override public Map
login(String sname, String password) { Student student=new Student(null,sname,password,null,null,null); Map
map=mapper.selectStudent(student).get(0); return map; } @Override public Integer addData(Student student) { return mapper.insertStudent(student); } @Override public Integer editData(Student student) { return mapper.updateStudent(student); } @Override public Integer delData(Integer key) { return mapper.deleteStudent(key); }}

 

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 List
optionsData() { return classesService.showOptions(); } @RequestMapping("/data.do") public List
> stuData() { return studentService.showData(); } @RequestMapping("/info.do") public Map
infoData(Integer key) { return studentService.showInfo(key); } @RequestMapping("/login.do") public Map
login(String sname, String password) { return studentService.login(sname,password); } @RequestMapping("/add.do") public Integer addData(Student student) { return studentService.addData(student); } @RequestMapping("/edit.do") public Integer editData(Student student) { return studentService.editData(student); } @RequestMapping("/delete.do") public Integer delData(Integer key) { return studentService.delData(key); }}

 

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

转载于:https://www.cnblogs.com/holly8/p/11012773.html

你可能感兴趣的文章
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
查看>>
如何看待yandex开源clickhouse这个列式文档数据库?
查看>>
使用jdbc操作ClickHouse
查看>>
Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量...
查看>>
LinkedList实现基于LRU算法的缓存
查看>>
Alibaba开源组件-分布式流量控制框架sentinel初探
查看>>
分布式系统
查看>>
一致性算法—Paxos、Raft、ZAB
查看>>
Cobar + MySQL 技术验证(li)
查看>>
一致性hash算法及java实现
查看>>
搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3
查看>>
Python 最常见的 170 道面试题解析:2019 最新
查看>>
机器学习算法GBDT的面试要点总结-上篇
查看>>
nodejs+gulp+webpack基础知识
查看>>
nginx代理 tomcat获得真实用户IP
查看>>
开始写下自己的python的cocos2d, pyglet学习
查看>>
最全最新java面试题系列全家桶(带答案)
查看>>
[多平台]pymo – 手机上的 GalGame 引擎
查看>>
signalfx的中间件监控指标so cool
查看>>
Java面试知识点汇总
查看>>