本文共 6431 字,大约阅读时间需要 21 分钟。
1)数据库准备
在数据库中mysql 分别创建2个数据库forum1和forum2 forum1 下创建表blockforum2下创建表Themesql代码如下:1 数据库1Forum1:CREATE DATABASE IF NOT EXISTS `forum1`;USE `forum1`;DROP TABLE IF EXISTS `block`;CREATE TABLE `block` ( `id` int(11) NOT NULL AUTO_INCREMENT, `bname` varchar(45) DEFAULT NULL, `bDescription` varchar(45) DEFAULT NULL COMMENT '板块描述\n', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='板块';LOCK TABLES `block` WRITE;INSERT INTO `block` VALUES (1,'java','讨论java'),(2,'C++','讨论c++方面问题');UNLOCK TABLES;
2 数据库forum2
CREATE DATABASE IF NOT EXISTS `forum2` ;USE `forum2`;DROP TABLE IF EXISTS `theme`;CREATE TABLE `theme` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tName` varchar(50) DEFAULT NULL COMMENT '主题名', `tDescription` varchar(200) DEFAULT NULL COMMENT '主题描述', `blockid` int(11) DEFAULT NULL COMMENT '区id', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='主题';LOCK TABLES `theme` WRITE;INSERT INTO `theme` VALUES (1,'java基础','java 基础',1),(2,'javaWeb','java web',1);UNLOCK TABLES;
2)编写第一个微服务BlockMicroService
(1)创建工程(2)添加 jar pom.xml
添加:springboot 父, mysql连接,(mybatis, spring-mybatis springboot ,阿里连接池) ,服务中心客户端。4.0.0 com.jh BlockMicroService 0.0.1-SNAPSHOT jar BlockMicroService http://maven.apache.org org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 UTF-8 1.7 1.7 1.7 Dalston.SR1 4.1.0 mysql mysql-connector-java 5.1.43 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.springframework.cloud spring-cloud-starter-eureka com.alibaba druid 1.0.19 org.springframework.cloud spring-cloud-dependencies Dalston.SR3 pom import
(3)编写配置文件Application.properties
配置发布服务名,端口;配置中心地址;连接mysql 参数
#服务名spring.application.name =blockMicroService#服务端口server.port =8020 #注册中心地址eureka.client.service-url.defaultZone=http://127.0.0.1:8001/eurekaspring.datasource.driver-class-name =com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/forum1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=falsespring.datasource.username= rootspring.datasource.password=spring.datasource.initialize =trueinit-db= truelogging.level.com.codingapi=debug
(4)编写实体,dao和映射。
实体:public class Block { private int id; private String bname; private String bDescription; get set …}
Dao 和映射
package com.jh.dao;@Mapperpublic interface BlockDao { /** * 查询 * * @return */ @Select(value = "select * from block") public ListgetBlockList(); /** * 插入 * * @param bname * @param bDescription * @return */ @Insert(value = "insert into block(bname,bDescription)" + " values(#{bname},#{bDescription})") public int saveBlock(@Param("bname") String bname, @Param("bDescription") String bDescription);}
(5)编写服务层
服务接口
public interface BlockService { ListgetBlockList(); int saveBlock(String bname, String bDescription);}
服务实现:
@Servicepublic class BlockServiceImpl implements BlockService {@Autowired private BlockDao blockDao;@Overridepublic ListgetBlockList() { return blockDao.getBlockList();}*/@Overridepublic int saveBlock(String bName, String bDescription){ int rs1 = blockDao.saveBlock("jwg1", "111");// 保存1 return rs1; }}
(6)编写控制层
package com.jh.controller;@RestControllerpublic class BlockController { /* private final Logger logger = Logger.getLogger(getClass()); */ @Autowired private BlockService blockService;// 区块服务,第一个服务 // 1接受请求 @RequestMapping(value = "/getBlockList", method = RequestMethod.GET) public ListgetBlockList() { List blockList = blockService.getBlockList(); return blockList; } // 2调用其他服务@RequestMapping(value = "/saveBlock", method = RequestMethod.GET) public String saveBlock() { //调用服务 Integer result=blockService.saveBlock("cc", "cc"); return ;}}
(7) 编写主程序
开启springboot应用程序,注册中心客户端,mybatis扫描和定义一个数据源package com.jh;import javax.sql.DataSource;@SpringBootApplication //spring boot应用程序@EnableEurekaClient //服务中心客户端@MapperScan("com.jh.dao")//mybatis 扫描public class BlockMicroService { public static void main(String[] args) { SpringApplication.run(BlockMicroService.class, args); } //1环境 @Autowired private Environment env; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(env.getProperty("spring.datasource.url")); dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名 dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码 dataSource.setInitialSize(10); dataSource.setMaxActive(50); dataSource.setMinIdle(1); dataSource.setMaxWait(60000); dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(false); dataSource.setTestWhileIdle(true); dataSource.setPoolPreparedStatements(false); return dataSource; }}
(8)测试
启动注册中心,启动微服务 然后启动浏览器转载于:https://blog.51cto.com/14048134/2310798