博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springCloud分布式事务实战(六)编写第二个微服务
阅读量:7104 次
发布时间:2019-06-28

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

(1)创建工程

springCloud分布式事务实战(六)编写第二个微服务

(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 参数

#1 register server #服务名spring.application.name =themeMicroService#服务端口server.port =8021#注册中心地址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/forum2?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 Theme {private  Integer id;private  String  tName;private  String  tDescription;get set }

Dao 和映射

@Mapperpublic interface ThemeDao {    /**     * 查询     *      * @return     */    @Select(value = "select *  from theme")    public List
getThemeList(); /** * 插入 * * @param bname * @param bDescription * @return */ @Insert(value = "insert into theme(tName,tDescription,blockId)" + " values(#{tName},#{tDescription}," + "#{blockId})") public int saveTheme(@Param("tName") String tName, @Param("tDescription") String tDescription, @Param("blockId") Integer blockId);}

(5)编写服务层

服务接口

public interface ThemeService {

 
List
getThemeList();int saveTheme(String tName, String tDescription , Integer blockId);

}

服务实现:

package com.jh.service.impl;@Servicepublic class ThemeServiceImpl implements ThemeService {    @Autowired    private ThemeDao themeDao;    @Override    public List
getThemeList() { return themeDao.getThemeList(); } @Override public int saveTheme(String tName, String tDescription, Integer blockId) { // TODO Auto-generated method stub int rs1 = themeDao.saveTheme(tName, tDescription, blockId);// 保存1 return rs1; }}

(6)编写控制层

@RestControllerpublic class ThemeController {    @Autowired    private ThemeService themeService;// 块服务,第一个服务    // 1接受请求    @RequestMapping(value = "/getThemeList", method = RequestMethod.GET)    public List
getThemeList() { List
ThemeList = themeService.getThemeList(); return ThemeList; } @RequestMapping(value = "/saveTheme", method = RequestMethod.GET) public int saveTheme() { Integer result = themeService.saveTheme("jwg2", "jwg2", 1); return result }}

(7) 编写主程序

开启springboot应用程序,注册中心客户端,mybatis扫描和定义一个数据源
package com.jh;

@SpringBootApplication  //spring boot应用程序@EnableEurekaClient@MapperScan("com.jh.dao")public class ThemeMicroService {    public static void main(String[] args) {        SpringApplication.run(ThemeMicroService.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)测试

启动注册中心,启动微服务
然后启动浏览器
springCloud分布式事务实战(六)编写第二个微服务

转载于:https://blog.51cto.com/14048134/2311965

你可能感兴趣的文章
CSS块级元素、内联元素概念
查看>>
Ubuntu关闭进入screensaver模式
查看>>
android体系结构
查看>>
Ubuntu安装 和 python开发
查看>>
「洛谷P3931」 SAC E#1 - 一道难题 Tree
查看>>
C#压缩库SharpZipLib的应用
查看>>
C# Type
查看>>
我的博客地址和github地址
查看>>
最大公约数(gcd)还有最小公倍数(lcm)的共通之处
查看>>
2018.10.30-dtoj-4009-秀秀的森林(forest)
查看>>
[LeetCode] Strobogrammatic Number II
查看>>
《C语言》-(static和extern)
查看>>
HBase Snapshot简介
查看>>
//……关于TCP三次握手与四次挥手
查看>>
blocksit
查看>>
renderscript 浅析(一)
查看>>
弹出对话框---MessageBox
查看>>
定义的命令不可用的原因及解决办法
查看>>
libevent(十三)evhttp事件处理流程
查看>>
1004. 西西弗斯式的命运——java
查看>>