一、提供数据的客户端需要连接数据了,因此需要我们使用mybatis了,等下使用idea生成mybaits和web的依赖
二、提供数据的客户端项目
1.创建项目
2.选择idea自动给我们生成的依赖
3. 确认工程名称和模块名称
4.查看项目结构
5.修改pom的依赖
<dependencies> <!--因为等下要用到数据访问层,因此我们可以应用我们刚才的公共组件--> <dependency> <groupId>cn.kgc</groupId> <artifactId>eureka-common-school</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--修改生成的版本号--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
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<Classes> 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<Map<String,Object>> 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<Classes> 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<Map<String,Object>> showData(); Map<String,Object> showInfo(Integer key); Map<String,Object> 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; @Service public class ClassesServiceImpl implements ClassesService{ @Autowired private ClassesMapper mapper; @Override public List<Classes> 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; @Service public class StudentServiceImpl implements StudentService{ @Autowired private StudentMapper mapper; @Override public List<Map<String, Object>> showData() { return mapper.selectStudent(new Student()); } @Override public Map<String, Object> showInfo(Integer key) { Student student=new Student(key,null,null,null,null,null); Map<String, Object> map=mapper.selectStudent(student).get(0); return map; } @Override public Map<String, Object> login(String sname, String password) { Student student=new Student(null,sname,password,null,null,null); Map<String, Object> 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
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.kgc.mapper.StudentMapper"> <select id="selectStudent" parameterType="Student" resultType="map"> SELECT s.*,c.cname from student s,classes c where s.sid=c.cid <if test="sid!=null"> and s.sid=#{sid} </if> <if test="sname!=null and password!=null"> and s.sname=#{sname} and s.password=#{password} </if> </select> </mapper>
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-provider
server.port=8762
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#数据源相关配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.117.134:3306/kgc
spring.datasource.username=root
spring.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; @RestController public class CenterController { @Autowired private ClassesService classesService; @Autowired private StudentService studentService; @RequestMapping("/options.do") public List<Classes> optionsData() { return classesService.showOptions(); } @RequestMapping("/data.do") public List<Map<String, Object>> stuData() { return studentService.showData(); } @RequestMapping("/info.do") public Map<String, Object> infoData(Integer key) { return studentService.showInfo(key); } @RequestMapping("/login.do") public Map<String, Object> 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") @SpringBootApplication public 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
springcloud(五):Eureka提供数据的客户端连接Docker的mysql
原文:https://www.cnblogs.com/holly8/p/11012773.html