第二天内容:想来想去玩个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(二)的更多相关文章

  1. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  2. 微服务架构 SpringBoot(一)

    spring Boot:官网地址 https://spring.io/ 由来: 随着spring组件功能的强大,配置文件也越来越复杂繁琐,背离了spring公司的简洁快速开发原理,2015年就推出Sp ...

  3. Spring Cloud构建微服务架构(二)分布式配置中心

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  4. 微服务架构 - SpringBoot整合Jooq和Flyway

    在一次学习分布式跟踪系统zipkin中,发现了jooq这个组件,当时不知这个组件是干嘛的,后来抽空学习了一下,感觉这个组件还挺用的.它主要有以下作用: 通过DSL(Domain Specific La ...

  5. 《Spring Cloud构建微服务架构》系列博文示例

    SpringCloud-Learning   源码下载地址:http://download.csdn.net/detail/k21325/9650968     本项目内容为Spring Cloud教 ...

  6. Net分布式系统之五:微服务架构

    因工作较忙,抽时间将框架遇到的问题和框架升级设计进行记录. 一.背景&问题 之前框架是一个基于SOA思想设计的分布式框架.各应用通过服务方式提供使用,服务之间通信是RPC方式调用,具体实现基于 ...

  7. Spring Cloud构建微服务架构

    Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...

  8. Spring Cloud构建微服务架构(三)消息总线

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  9. SpringBoot微服务架构下的MVC模型总结

    SpringBoot微服务架构下的MVC模型产生的原因: 微服务概念改变着软件开发领域,传统的开源框架结构开发,由于其繁琐的配置流程 , 复杂的设置行为,为项目的开发增加了繁重的工作量,微服务致力于解 ...

随机推荐

  1. ubuntu 16.04源码编译OpenCV教程 | compile opencv on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/15f5c3e8/,欢迎阅读! compile opencv on ubuntu 16.04 Series Part 1: comp ...

  2. 学习PHP框架只停留在会用层面,职业生涯肯定走不远!

    工作这么多年,也面试过很多PHP工程师,我发现很多PHP工程师只停留在使用框架的层面,然而对框架底层根本没有深入去了解,那么这就会给自己的职业生涯带来一定的瓶颈,当遇到问题的时候你就无从下手,不知道如 ...

  3. 程序员的算法课(3)-递归(recursion)算法

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  4. css多行文本溢出显示省略号(兼容ie)

    在日常编写页面中,我们经常遇到内容行数过多时,需要出现 “...” 来处理.但是又要考虑IE浏览器或IE内核浏览器的兼容性. 普通实现方法: display: -webkit-box; -webkit ...

  5. Composer安装和使用

    Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "packages" ...

  6. OBS带你玩转图片

    [摘要] 图片处理特性(Image Processing)是对象存储服务(Object Storage Service,OBS)为用户提供稳定.安全.高效.易用.低成本的图片处理服务,包括:图片剪切. ...

  7. 用生动的案例一步步带你学会python多线程模块

    鱼和熊掌不可兼得 鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也. 从6月开始写公众号,连着四个月一直尽量保证一周五更,结果整天熬夜搞的身体素质骤降.十一休假决定暂时将公众号放放,好好 ...

  8. 转:Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2

    先说结论 建议优先使用logback 或 log4j2.log4j2 不建议和 slf4j 配合使用,因为格式转换会浪费性能. 名词:jcl 和 jul 标题中的 jcl 是 apache Jakar ...

  9. NCPC 2016 Fleecing the Raffle

    Description A tremendously exciting raffle is being held, with some tremendously exciting prizes bei ...

  10. Python之数据分析工具包介绍以及安装【入门必学】

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 首先我们来看 Mac版 按照需求大家依次安装,如果你还没学到数据分析,建议你 ...