SpringBoot 整合JdbcTemplate

1.创建一个springboot_jdbc项目

 2.导入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

3.创建一个entity实体类

public class Grade {
private Integer grade_id;
private String grade_name;
}

4.创建一个dao层实体

IGradeDao
public interface IGradeDao {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}
IGradeDaoImpl
@Repository
public class IGradeDaoImpl implements IGradeDao {
//导入JDBCTemplate模板
@Resource
private JdbcTemplate jdbcTemplate; @Override
public int insertGrade(Grade grade) {
return jdbcTemplate.update("insert into Grade(grade_name) values(?)",grade.getGrade_name());
} @Override
public int updateGrade(Grade grade) {
return jdbcTemplate.update("update grade set grade_name=? where grade_id=?",grade.getGrade_name(),grade.getGrade_id());
} @Override
public int deleteGrade(Integer id) {
return jdbcTemplate.update("delete from grade where grade_id=?",id);
} @Override
public List<Grade> findAll() {
//封装行数据映射
RowMapper<Grade> rowMapper=new RowMapper<Grade>() {
@Override
public Grade mapRow(ResultSet rs, int rowNum) throws SQLException {
Grade grade=new Grade(rs.getInt("grade_id"),rs.getString("grade_name"));
return grade;
}
};
return jdbcTemplate.query("select * from grade", rowMapper);
}
}

5.创建service

IGradeService
public interface IGradeService {
public int insertGrade(Grade grade);
public int updateGrade(Grade grade);
public int deleteGrade(Integer id);
public List<Grade> findAll();
}
IGradeServiceImpl
@Service("iGradeService")
public class IGradeServiceImpl implements IGradeService {
@Resource
private IGradeDao iGradeDao; @Override
public int insertGrade(Grade grade) {
return iGradeDao.insertGrade(grade);
} @Override
public int updateGrade(Grade grade) {
return iGradeDao.updateGrade(grade);
} @Override
public int deleteGrade(Integer id) {
return iGradeDao.deleteGrade(id);
} @Override
public List<Grade> findAll() {
return iGradeDao.findAll();
}
}

6.创建controller控制器

@RestController
public class JDBCTemplateController {
@Resource
private IGradeService iGradeService; @RequestMapping("/insertGrade")
public int insertGrade(){
return iGradeService.insertGrade(new Grade("S1"));
}
@RequestMapping("/updateGrade")
public int updateGrade(){
return iGradeService.updateGrade(new Grade(10012,"S2"));
}
@RequestMapping("/deleteGrade")
public int deleteGrade(){
return iGradeService.deleteGrade(10012);
}
@RequestMapping("/findAll")
public List<Grade> findAll(){
return iGradeService.findAll();
}
}

7.编写application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springdatajpa
username: root
password: 123456 ##更改Tomcat端口
server:
port: 8081
##指定当前工程项目访问地址
context-path: /jdbc

8.直接运行

@SpringBootApplication
public class StartSpringBoot {
public static void main(String[] args) {
SpringApplication.run(StartSpringBoot.class,args);
}
}

分别访问http://localhost:8081/jdbc/insertGrade

返回数字1代表添加成功

http://localhost:8081/jdbc/updateGrade

http://localhost:8081/jdbc/deleteGrade

http://localhost:8081/jdbc/findAll

SpringBoot整合Mybatis

1.创建一个springboot_mybatis项目

 2.导入依赖

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

3.创建application.yml文件

##数据源配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springdatajpa
username: root
password: 123456
##myabtis配置
mybatis:
type-aliases-package: com.boot.entity
mapper-locations: classpath:/mapper/*.xml
##开启日志
logging:
level:
com.boot.dao: debug

4.创建entity实体类

public class Grade {

    private Integer grade_id;
private String grade_name;
}

5.创建dao

@Repository
@Mapper
public interface IGradeDao { int insertGrade(Grade grade); int updateGrade(Grade grade); int deleteGrade(Integer id); List<Grade> findAll();
}

6.创建mapper文件

<?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="com.boot.dao.IGradeDao">
<insert id="insertGrade">
insert into grade(grade_name) values(#{grade_name})
</insert>
<update id="updateGrade">
update grade set grade_name=#{grade_name} where grade_id=#{grade_id}
</update>
<delete id="deleteGrade">
delete from grade where grade_id=#{grade_id}
</delete>
<select id="findAll" resultType="com.boot.entity.Grade">
select * from grade
</select>
</mapper>

7.创建service

IGradeService
public interface IGradeService {
int insertGrade(Grade grade);
int updateGrade(Grade grade);
int deleteGrade(Integer id);
List<Grade> findAll();
}
IGradeServiceImpl
@Service("iGradeService")
public class IGradeServiceImpl implements IGradeService {
@Resource
private IGradeDao iGradeDao; @Override
public int insertGrade(Grade grade) {
return iGradeDao.insertGrade(grade);
} @Override
public int updateGrade(Grade grade) {
return iGradeDao.updateGrade(grade);
} @Override
public int deleteGrade(Integer id) {
return iGradeDao.deleteGrade(id);
} @Override
public List<Grade> findAll() {
return iGradeDao.findAll();
}
}

8.创建controller控制器

@RestController
public class MybatisController {
@Resource
private IGradeService iGradeService; @RequestMapping("/insertGrade")
public int insertGrade(){
return iGradeService.insertGrade(new Grade("S1"));
}
@RequestMapping("/updateGrade")
public int updateGrade(){
return iGradeService.updateGrade(new Grade(2,"S2"));
}
@RequestMapping("/deleteGrade")
public int deleteGrade(){
return iGradeService.deleteGrade(2);
}
@RequestMapping("/findAll")
public List<Grade> findAll(){
return iGradeService.findAll();
}
}

9.运行

@SpringBootApplication
@MapperScan("com.boot.dao")
public class StartSpringBoot {
public static void main(String[] args) {
SpringApplication.run(StartSpringBoot.class,args);
}
}

效果如上所示

SpringBoot整合Dubbo

1.创建springboot_dubbo_provider项目

2.导入依赖

<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

3.创建application.yml

spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880 ##com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springdatajpa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456

4.创建service

public interface IDoSomeService {
public String sayHi();
}
IDoSomeServiceImpl
//利用Dubbo暴露出一个接口
@Service(interfaceClass=IDoSomeService.class)
@Component
public class IDoSomeServiceImpl implements IDoSomeService {
@Override
public String sayHi() {
System.out.println("生产者生产的IDoSomeService服务,中的sayHi方法");
return "SpringBoot Dubbo";
}
}

5.运行

启动本地的zookeeper

启动项目

可以看到service已经暴露成功了

下面创建springboot_dubbo_consumer

创建application.properties

spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 server.port=8081

创建service

public interface IDoSomeService {
public String sayHi();
}

创建controller控制器

@RestController
public class DubboController {
@Reference
private IDoSomeService iDoSomeService; @RequestMapping("/dubbo")
public String dubbo(){
String returnValue = iDoSomeService.sayHi();
return returnValue;
}
}

直接启动

springboot(三)的更多相关文章

  1. spring-boot (三) spring data jpa

    学习文章来自:http://www.ityouknow.com/spring-boot.html spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence ...

  2. SpringBoot 三种方式配置 Druid(包括纯配置文件配置)

    记录一下在项目中用纯 YML(application.yml 或者 application.properties)文件.Java 代码配置 Bean 和注解三种方式配置 Alibaba Druid 用 ...

  3. SpringBoot(三) -- SpringBoot与日志

    一.日志的起源 现在假设一个开发人员在开发一个大型系统,由于这个系统过于庞大没在很多的地方将关键的数据使用System.out.println()打印,但是当我们在项目正式上线时又需要去除,在项目bu ...

  4. SpringBoot(三)SpringBoot自动配置

    我们都知道SpringBoot帮助我们集成了许多组件和配置,那么SpringBoot是如何集成这些配置并在启动是自动进行配置呢.说到这就不得又需要回过头来看一下@SpringBootApplicati ...

  5. Java开发学习(三十六)----SpringBoot三种配置文件解析

    一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...

  6. SpringBoot(三) - Slf4j+logback 日志,异步请求,定时任务

    1.Slf4j+logback 日志 SpringBoot框架的默认日志实现:slf4j + logback: 默认日志级别:info,对应了实际生产环境日志级别: 1.1 日志级别 # 常见的日志框 ...

  7. springboot(三):Spring boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  8. spring-boot(三) HowTo

    Spring Boot How To 1. 简介 本章节将回答一些常见的"我该怎么做"类型的问题,这些问题在我们使用spring Boot时经常遇到.这绝不是一个详尽的列表,但它覆 ...

  9. SpringBoot三种配置Dubbo的方式

    *必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...

  10. SpringBoot(三) Core Features: External Configuration(配置文件)

    码云: external-configuration 可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置 一.属性值可以直接注入到bean 系统属性值不可以 // application ...

随机推荐

  1. js中常见的创建对象的方法(1)

    工厂模式:抽象了创建具体对象的过程 function createPerson(name, age, job){ var obj = new Object(); obj.name = name; ob ...

  2. 深度学习-CNN+RNN笔记

    以下叙述只是简单的叙述,CNN+RNN(LSTM,GRU)的应用相关文章还很多,而且研究的方向不仅仅是下文提到的1. CNN 特征提取,用于RNN语句生成图片标注.2. RNN特征提取用于CNN内容分 ...

  3. arm-linux-系列工具,ld,ar,as,objcopy

    ref :http://www.360doc.com/content/14/0509/09/17268421_376009916.shtml 一.编译器相关知识学习 GNU GCC简介: GNU GC ...

  4. docker查看日志记录

    命令格式: $ docker logs [OPTIONS] CONTAINER Options: --details 显示更多的信息 -f, --follow 跟踪实时日志 --since strin ...

  5. 论文笔记: Deep Learning based Recommender System: A Survey and New Perspectives

    (聊两句,突然记起来以前一个学长说的看论文要能够把论文的亮点挖掘出来,合理的进行概括23333) 传统的推荐系统方法获取的user-item关系并不能获取其中非线性以及非平凡的信息,获取非线性以及非平 ...

  6. Java 阿拉伯数字转换为中文大写数字

    Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...

  7. Matlab模板模式

    在模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板.它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行.本文以数据库SQL语法为例来阐述模板模 ...

  8. 嵌入式 vlc从接收到数据流到播放视频的过程分析(经典)

    个人整理: Vlc流播放流程 vlc源码目录树: 目录名称 说明 bindings Java, CIL 和Python绑定 doc 帮助文档 (不是更新的) extras 另叙. include VL ...

  9. Spring 重定向(Redirect)指南

    原文:Hacking the IntegerCache in Java 9? 链接:https://dzone.com/articles/hacking-the-integercache-in-jav ...

  10. 10 分钟上手 Vue 组件 Vue-Draggable

    Vue 综合了 Angualr 和 React 的优点,因其易上手,轻量级,受到了广泛应用.成为了是时下火热的前端框架,吸引着越来越多的前端开发者! 本文将通过一个最简单的拖拽例子带领大家快速上手 V ...