SpringBoot3(springboot_jdbctemplate以及MyBatis和Dubbo整合)

一、JDBCTemplate

  1、导入依赖

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

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

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

  2、配置application文件

    

  3、创建启动类

package com.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

  4、创建实体层

package com.boot.entity;
public class Grade {

    private Integer gradeId;
    private String gradeName;

    public Grade() {

    }

    public Grade(String gradeName) {
        this.gradeName = gradeName;
    }

    public Grade(Integer gradeId, String gradeName) {
        this.gradeId = gradeId;
        this.gradeName = gradeName;
    }

    public Integer getGradeId() {
        return gradeId;
    }

    public void setGradeId(Integer gradeId) {
        this.gradeId = gradeId;
    }

    public String getGradeName() {
        return gradeName;
    }

    public void setGradeName(String gradeName) {
        this.gradeName = gradeName;
    }
}

  5、创建Dao层

package com.boot.dao;

import com.boot.entity.Grade;

import java.util.List;

public interface IGradeDao {
    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer id);
    public List<Grade> getAll();
}

  6、创建Dao层实现类

package com.boot.dao.impl;

import com.boot.dao.IGradeDao;
import com.boot.entity.Grade;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class IGradeDaoImpl implements IGradeDao {

    //导入JDBCTemplate模板
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insertGrade(Grade grade) {
        return jdbcTemplate.update("insert into Grade(GradeName) values (?)",grade.getGradeName());
    }

    @Override
    public int updateGrade(Grade grade) {
        return jdbcTemplate.update("update Grade set GradeName=? where GradeId=?",grade.getGradeName(),grade.getGradeId());
    }

    @Override
    public int deleteGrade(Integer id) {
        return jdbcTemplate.update("delete from Grade where GradeId=?",id);
    }

    @Override
    public List<Grade> getAll() {
        //fengxhaung封装行数据映射
        RowMapper<Grade> rowMapper=new RowMapper<Grade>() {
            @Override
            public Grade mapRow(ResultSet rs, int i) throws SQLException {
                Grade grade=new Grade(rs.getInt("GradeId"),rs.getString("GradeName"));
                return grade;
            }
        };
        return jdbcTemplate.query("select * from Grade",rowMapper);
    }
}

  7、创建Service层

package com.boot.service;

import com.boot.entity.Grade;

import java.util.List;

public interface IGradeService {
    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer id);
    public List<Grade> getAll();
}

  8、创建Service实现类

package com.boot.service.impl;

import com.boot.dao.IGradeDao;
import com.boot.entity.Grade;
import com.boot.service.IGradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("iGradeService")
public class IGradeServiceImpl implements IGradeService {
    @Resource
    //植入Dao层对象
    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> getAll() {
        return iGradeDao.getAll();
    }
}

  9、创建Controller层

package com.boot.controller;

import com.boot.entity.Grade;
import com.boot.service.IGradeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class IGradeController {
    //导入Service层
    @Resource
    private IGradeService iGradeService;

    @RequestMapping("/insertGrade")
    public int insertGrade(Grade grade){
        return iGradeService.insertGrade(new Grade("S1"));
    }

    @RequestMapping("/updateGrade")
    public int updateGrade(Grade grade){
        return iGradeService.updateGrade(new Grade(2,"S1"));
    }

    @RequestMapping("/deleteGrade")
    public int deleteGrade(){
        return iGradeService.deleteGrade(6);
    }

    @RequestMapping("/getAll")
    public List<Grade> getAll(){
        return iGradeService.getAll();
    }
}

  10、实现效果如下

    

二、MyBatis整合

  1、导入依赖

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

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

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

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

  2、配置application文件

    

  3、创建entity层

package com.boot.entity;

import java.io.Serializable;

public class Grade implements Serializable {

    private Integer gradeId;    private String gradeName;

    public Grade() {

    }

    public Grade(String gradeName) {        this.gradeName = gradeName;    }

    public Grade(Integer gradeId, String gradeName) {        this.gradeId = gradeId;        this.gradeName = gradeName;    }

    public Integer getGradeId() {        return gradeId;    }

    public void setGradeId(Integer gradeId) {        this.gradeId = gradeId;    }

    public String getGradeName() {        return gradeName;    }

    public void setGradeName(String gradeName) {        this.gradeName = gradeName;    }}

  4、创建Dao层

package com.boot.dao;

import com.boot.entity.Grade;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IGradeDao {
    public int insertGrade(Grade grade);

    public int updateGrade(Grade grade);

    public int deleteGrade(Integer id);

    public List<Grade> findAll();
}

  5、创建Dao层小配置文件(在resources目录下创建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(GradeName) values(#{gradeName})
    </insert>

    <insert id="updateGrade">
         update set GradeName=#{gradeName} from Grade where GradeId=#{gradeId}
    </insert>

    <insert id="deleteGrade">
        delete from Grade where GradeId=#{gradeId}
    </insert>

    <select id="findAll" resultType="com.boot.entity.Grade">
       select * from Grade
    </select>
</mapper>

  6、创建Service层

package com.boot.service;

import com.boot.entity.Grade;

import java.util.List;

public interface IGradeService {
    public int insertGrade(Grade grade);
    public int updateGrade(Grade grade);
    public int deleteGrade(Integer id);
    public List<Grade> findAll();
}

  7、创建Service实现层

package com.boot.service.impl;

import com.boot.dao.IGradeDao;
import com.boot.entity.Grade;
import com.boot.service.IGradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@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层

package com.boot.controller;

import com.boot.entity.Grade;
import com.boot.service.IGradeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MybatisController {

    @Resource(name = "iGradeService")
    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();
    }
}

  9、实现效果如下

    

三、Dubbo整合

  1、导入依赖

 <!--dubbo依赖-->
    <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>

    <!--MySQL-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

  2、创建消费者springboot-dubbo-consumer

    (1)配置application文件

      

    (2)创建启动类

package com.boot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

    (3)创建Service层

package com.boot.service;

public interface IDoSomeService {
    public String sayHi();
}

    (4)创建Controller层

package com.boot.controller;

import com.boot.service.IDoSomeService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboController {
    @Reference
    private IDoSomeService iDoSomeService;

    @RequestMapping("/dubbo")
    public String dubbo(){
        String returnValue=iDoSomeService.sayHi();
        return returnValue;
    }
}

  3、创建生产者springboot-dubbo-provider

    (1)配置文件

        

    (2)创建DubboProvider层

package com.boot.com.boot.service;

import org.springframework.boot.SpringApplication;

public class DubboProvider {
    public static void main(String[] args){
        SpringApplication.run(DubboProvider.class,args);
        System.out.println("Dubbo接口暴露成功!");
    }
}

    (3)创建IDoSomeService层

package com.boot.com.boot.service;

public interface IDoSomeService {
    public String sayHi();
}

    (4)创建IDoSomeService实现层

package com.boot.com.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.boot.com.boot.service.IDoSomeService;
import org.springframework.stereotype.Component;

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

  4、实现效果如下

    

原文地址:https://www.cnblogs.com/tinghao/p/12040235.html

时间: 2024-11-09 03:06:05

SpringBoot3(springboot_jdbctemplate以及MyBatis和Dubbo整合)的相关文章

Mybatis与Spring整合

Mybatis与Spring整合无外乎要将数据源,以及事物管理的相关配置交给spring来管理,mybatis配置负责sqlmapper的相关配置也就是dao层到sql映射的相关配置. 一下以手机管理系统dao层实现所用到的Spring与MyBatis整合为例. 1.spring中beans.xml相关配置 <!--加载数据源基本配置文件--><context:property-placeholder location="classpath:conf/jdbc.properti

Spring+mybatis+struts框架整合的配置具体解释

学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以下. 1.导入对应的jar包 由于我们建造的是maven的web项目,全部我们在pom.xml中须要导入这些包. pom.xml 具体凝视 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q

mybatis与spring整合时读取properties问题的解决

在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: 1 <context:property-placeholder location="classpath:db.properties" /> 2 <bean id="dataSource" 3 class="org.springframework.jdbc.datasource.DriverManage

Mybatis 与 spring 整合

这篇文章我们来学习一下Mybatis 与 spring 的整合MapperFactoryBean 我们知道在Mybatis的所有操作都是基于一个SqlSession的,而SqlSession是由SqlSessionFactory来产生的,SqlSessionFactory又是由SqlSessionFactoryBuilder来生成的.但是Mybatis-Spring是基于SqlSessionFactoryBean的.在使用Mybatis-Spring的时候,我们也需要SqlSession,而且这

Spring MVC入门第2天--Spring、SpringMVC与MyBatis三大框架整合

文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.07.01 lutianfei none springmvc和mybatis整合 需求: 使用springmvc和mybatis完成商品列表查询. 整合思路 spring+springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在spring中进行注册. 第二步:整合service层

转:spring与mybatis三种整合方法

哎,csdn没转载功能,只能复制了.. 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接http://code.google.com/p/mybatis/下载到. 1.采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数.  (1)Spring配置文件: <!-- 引入jdbc配置文件 -->     <context:property

mybatis与spring整合(基于配置文件)(转)

mybatis与spring整合(基于配置文件) 本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDao.java) package com.mybatis; publicinterface UserDao { publicint countAll(); } 2.编写数据访问接口映射文件(UserDaoMapper.xml) <?xml versio

Spring+SpringMVC+MyBatis+Maven框架整合

本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点 一.Maven需要引入的jar包 二.Spring与SpringMVC的配置分离 三.Spring与MyBatis的整合 一.Maven需要引入的jar包 本文默认读者已经掌握Maven的使用,Maven配置片段如下 Xml代码   <!-- 引入spring-webmvc与spring-jdbc --> <dependency> <groupId>org.sprin

mybatis与spring整合(基于配置文件)

本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDao.java) package com.mybatis;public interface UserDao { public int countAll();} 2.编写数据访问接口映射文件(UserDaoMapper.xml) <?xml version="1.0" encoding=