微服务架构 SpringBoot(二)
第二天内容:想来想去玩个ssm小demo吧
1.创建表

2..引入相关mybatis 数据库jar:
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
3.在application.properties文件中 配置数据库驱动
spring.datasource.url=jdbc:mysql://localhost/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

4.编写三层 bean

beans
package com.chinasoft.bean;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
dao
package com.chinasoft.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import com.chinasoft.bean.User; @Mapper //集成mybatis
public interface UserDao {
@Select("select id ,name from user_tba") //也可以写配置文件形式,通过配置读取sql,我这里就不多讲了
public List<User> getUser(); @Update("UPDATE user_tba SET name = #{name} WHERE id =#{id}")
int updateById(User user); @Insert("insert INTO user_tba (name) values(#{name})")
void insert(User user); @Delete("DELETE FROM user_tba WHERE id = #{id} ")
int delete(Long id); }
Service
package com.chinasoft.service;
import java.util.List;
import com.chinasoft.bean.User;
public interface UserService {
public List<User> getUser();
int updateById(User user);
void insert(User user);
int delete(Long id);
}
ServiceImpl
package com.chinasoft.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.chinasoft.bean.User;
import com.chinasoft.dao.UserDao;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userdao; @Override
public List<User> getUser() {
return userdao.queryAll();
} @Override
public int updateById(User user) {
return userdao.updateById(user);
} @Override
public void insert(User user) {
userdao.insert(user);
} @Override
public int delete(Long id) {
return userdao.delete(id);
} }
Contorller
package com.chinasoft.controller; import java.util.List; 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.ResponseBody; import com.chinasoft.bean.User;
import com.chinasoft.service.UserService; @Controller
//@RestClientTest
public class HelloSpringBootContorller {
@Autowired
private UserService userService; @RequestMapping(value ={"/show"})
@ResponseBody
public List<User> getUser(){
List<User> user = userService.getUser();
return user;
}
@RequestMapping(value ={"/update"})
public void updateUser(){
User user = new User();
user.setId(1);
user.setName("杨幂");
userService.updateById(user);
} @RequestMapping(value ={"/insert"})
public void insertUser(){
User user = new User();
user.setName("赵丽颖");
userService.insert(user);
} @RequestMapping(value ={"/delete"})
public void deleteUser(){ userService.delete(4L);
} }
运行配置:
package com.chinasoft.springboot; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan(basePackages = "com.chinasoft.*" )
@MapperScan(basePackages = "com.chinasoft.dao") //需注意 此为扫描数据库资源文件
public class SpringbootApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
启动:

添加监控运维:(查看请求及jvm的运行信息)
<!--监控运维 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

监控运维作用:
当请求访问时可以访问监控,看到用户的请求,操作信息,及jvm的运行状态。。。信息。
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/
搜索actuator查看相关属性

例如访问:http://localhost:8082/beans 显示的监控信息。。。

细节概要:
1.mybatis怎么和实体类映射的呢?可访问mybatis官网(http://blog.mybatis.org/mybatis-3/java-api.html)
A:@Mapper 自动扫描字段与实体类相对性给予映射。若不同则为空,eg:将数据库中的NAME字段改为address,运行结果如下,

如果手动映射如下:


2.关键字
sql中存在关键字 需要在关键字前后加反引号 (键盘英文格式下 ctrl +alt +~ )

总结:
Spring Boot集成mybatis时的一些注意的地方,也可以看出来SpringBoot确实简介方便,以前用mybatis还要封装查询query,而现在直接@select便可以直接查询,还是比较方便的开发的,这也应该是一种趋势,
技术交流群,海量学习资料免费获取:Q群:289683917
微服务架构 SpringBoot(二)的更多相关文章
- Spring Cloud构建微服务架构(二)服务消费者
Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...
- 微服务架构 SpringBoot(一)
spring Boot:官网地址 https://spring.io/ 由来: 随着spring组件功能的强大,配置文件也越来越复杂繁琐,背离了spring公司的简洁快速开发原理,2015年就推出Sp ...
- Spring Cloud构建微服务架构(二)分布式配置中心
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- 微服务架构 - SpringBoot整合Jooq和Flyway
在一次学习分布式跟踪系统zipkin中,发现了jooq这个组件,当时不知这个组件是干嘛的,后来抽空学习了一下,感觉这个组件还挺用的.它主要有以下作用: 通过DSL(Domain Specific La ...
- 《Spring Cloud构建微服务架构》系列博文示例
SpringCloud-Learning 源码下载地址:http://download.csdn.net/detail/k21325/9650968 本项目内容为Spring Cloud教 ...
- Net分布式系统之五:微服务架构
因工作较忙,抽时间将框架遇到的问题和框架升级设计进行记录. 一.背景&问题 之前框架是一个基于SOA思想设计的分布式框架.各应用通过服务方式提供使用,服务之间通信是RPC方式调用,具体实现基于 ...
- Spring Cloud构建微服务架构
Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...
- Spring Cloud构建微服务架构(三)消息总线
注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...
- SpringBoot微服务架构下的MVC模型总结
SpringBoot微服务架构下的MVC模型产生的原因: 微服务概念改变着软件开发领域,传统的开源框架结构开发,由于其繁琐的配置流程 , 复杂的设置行为,为项目的开发增加了繁重的工作量,微服务致力于解 ...
随机推荐
- JSON——IT技术人员都必须要了解的一种数据交换格式
JSON作为目前Web主流的数据交换格式,是每个IT技术人员都必须要了解的一种数据交换格式.尤其是在Ajax和REST技术的大行其道的当今,JSON无疑成为了数据交换格式的首选! 今天大家就和猪哥一起 ...
- HTTP 协议漫谈
转载出处:HTTP 协议漫谈 简介 网络上已经有不少介绍 HTTP 的好文章,对HTTP的一些细节介绍的比较好,所以本篇文章不会对 HTTP 的细节进行深究,而是从够高和更结构化的角度将 HTTP 协 ...
- odoo12 修行基础篇之 添加明细字段 (二)
前一篇介绍了如何在视图和表单中添加字段.本节内容,我们讨论下如何在明细中加字段. 我想在销售页面明细中增加税额字段,这在表sale.order.line中已经存在,在此仅用来演示. odoo的明细一般 ...
- python3 之 判断闰年小实例
一.方法1: while True: try: year = int(input('请输入一个年份:')) if (year % 4) == 0 and (year % 100) != 0 or (y ...
- JavaScript笔记十
1.正则表达式 - 语法: - 量词 {n} 正好n次 {m,n} m-n次 {m,} 至少m次 + 至少1次 {1,} ? 0次或1次 {0,1} * 0次或多次 {0,} - 转义字符 \ 在正则 ...
- JavaScript笔记七
1.函数 - 返回值,就是函数执行的结果. - 使用return 来设置函数的返回值. - 语法:return 值; - 该值就会成为函数的返回值,可以通过一个变量来接收返回值 - return后边的 ...
- 北冥'sfish
北冥咸鱼,其名为鲲.鲲之大,long long存不下.化而为鸟,其名为鹏.鹏之背,高精被卡废.怒而颓,其码若怪诞之吟.是咸鱼,颓废则将遇上cz.cz谁,大佬也.<大佬说>者,志奆者也.&l ...
- System.out.printf使用以及注意点
一.System.out.printf格式化输出 1.常用控制符 控制符 说明 %d 按十进制整型数据的实际长度输出. %ld 输出长整型数据. %md m 为指定的输出字段的宽度.如果数据的位数小于 ...
- re实战记录
re实战记录 针对网页中的空格符 一般使用的.,但是它不能匹配\n,所以使用[\s\S]或者[\d\D]匹配所有字符 import re l1=r''' <div class="thu ...
- RestTemplate 中文乱码
@Configuration public class RestTemplateWithoutLoadBalance { @Bean("normalRestTemplate") p ...