spring boot 学习--05---mysql,jpa,jdbcTemplate-应用

18 篇文章 1 订阅
12 篇文章 5 订阅

步骤

  1. 修改实体
  2. 增加dao(jpa),service层
  3. 增加controller层
  4. 测试
修改Demo实体
package com.springboot.study.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Demo 模型
 * @author like
 *
 */
@Entity
public class Demo {
     @Id 
     @GeneratedValue
    private long id;


    private String name;
    private String demo;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDemo() {
        return demo;
    }
    public void setDemo(String demo) {
        this.demo = demo;
    }

}
增加dao层(JPA)
package com.springboot.study.dao.jpa;

import org.springframework.data.repository.CrudRepository;

import com.springboot.study.bean.Demo;

/**
 * 这就完成一个Dao层的开发,不需要其他注解和配置,JPA自动配置
 * @author like
 *
 */
public interface DemoDao extends CrudRepository<Demo, Long>{

}
增加service层
package com.springboot.study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.study.bean.Demo;
import com.springboot.study.dao.jpa.DemoDao;

@Service
public class DemoService {
    @Autowired
    private DemoDao demoDao;

    public void save(Demo demo){
        demoDao.save(demo);
    }

}
controller层

package com.springboot.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.study.bean.Demo;
import com.springboot.study.service.DemoService;

/**
 * Demo 控制类
 * @author like
 *
 */
@RestController
@RequestMapping("/demo")
public class DemoController2 {
    @Autowired
    private DemoService demoService;

    @RequestMapping("/saveDemo")
    public String getDemo(){
        Demo demo = new Demo();
        demo.setId(1);
        demo.setDemo("demo");
        demo.setName("testDemo");
        demoService.save(demo);

        return "demo save success";

    }
}
测试

http://localhost:8080/demo/saveDemo
这里写图片描述

jdbcTemplate应用
  1. 新增jdbcdao
  2. 修改Service
  3. 修改controller
  4. 测试
新增jdbcdao

package com.springboot.study.dao.jdbcTemplate;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.springboot.study.bean.Demo;

@Repository
public class DemoJdbcDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public Demo getById(long id){
        String sql = "select * from Demo where id=?";
        RowMapper<Demo> rowMapper = new BeanPropertyRowMapper<Demo>(Demo.class);
        return jdbcTemplate.queryForObject(sql, rowMapper,id);
    }
}
修改Service
package com.springboot.study.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.study.bean.Demo;
import com.springboot.study.dao.jdbcTemplate.DemoJdbcDao;
import com.springboot.study.dao.jpa.DemoDao;

@Service
public class DemoService {
    @Autowired
    private DemoDao demoDao;

    @Autowired
    private DemoJdbcDao demoJdbcDao;

    public void save(Demo demo){
        demoDao.save(demo);
    }

    public Demo getById(long id){
        return demoJdbcDao.getById(id);
    }

}
修改controller
package com.springboot.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.study.bean.Demo;
import com.springboot.study.service.DemoService;

/**
 * Demo 控制类
 * @author like
 *
 */
@RestController
@RequestMapping("/demo")
public class DemoController2 {
    @Autowired
    private DemoService demoService;

    @RequestMapping("/saveDemo")
    public String getDemo(){
        Demo demo = new Demo();
        demo.setId(1);
        demo.setDemo("demo");
        demo.setName("testDemo");
        demoService.save(demo);

        return "demo save success";

    }

    @RequestMapping("/getById")
    public Demo getDemo(long id ){
        return demoService.getById(id);

    }
}
测试

访问 http://localhost:8080/demo/getById?id=1

结果:
成功

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Druid数据源和Spring Boot的多数据源支持来实现动态切换MySQL数据源。具体步骤如下: 1. 添加Druid和Spring Boot多数据源依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2. 配置Druid数据源 在application.properties文件中添加以下配置: ```properties # 数据源1 spring.datasource.druid.datasource-1.url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false spring.datasource.druid.datasource-1.username=root spring.datasource.druid.datasource-1.password=123456 spring.datasource.druid.datasource-1.driver-class-name=com.mysql.cj.jdbc.Driver # 数据源2 spring.datasource.druid.datasource-2.url=jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false spring.datasource.druid.datasource-2.username=root spring.datasource.druid.datasource-2.password=123456 spring.datasource.druid.datasource-2.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 配置多数据源 在application.properties文件中添加以下配置: ```properties # 数据源1对应的JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.datasource.druid.datasource-1.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.datasource-1.initial-size=5 spring.datasource.druid.datasource-1.min-idle=5 spring.datasource.druid.datasource-1.max-active=20 spring.datasource.druid.datasource-1.filters=stat,log4j # 数据源2对应的JPA配置 datasource2.jpa.hibernate.ddl-auto=update datasource2.jpa.show-sql=true datasource2.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.datasource.druid.datasource-2.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.datasource-2.initial-size=5 spring.datasource.druid.datasource-2.min-idle=5 spring.datasource.druid.datasource-2.max-active=20 spring.datasource.druid.datasource-2.filters=stat,log4j # 多数据源配置 spring.datasource.dynamic.primary=datasource-1 spring.datasource.dynamic.datasource-1=druid.datasource-1 spring.datasource.dynamic.datasource-2=druid.datasource-2 ``` 4. 编写动态数据源切换代码 在需要使用多数据源的地方,通过以下代码来动态切换数据源: ```java // 获取数据源 DynamicDataSourceContextHolder.setDataSourceKey("datasource-1"); DataSource dataSource = DynamicDataSource.getInstance().getDataSource(); // 使用数据源 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from user"); // 切换数据源 DynamicDataSourceContextHolder.setDataSourceKey("datasource-2"); dataSource = DynamicDataSource.getInstance().getDataSource(); jdbcTemplate = new JdbcTemplate(dataSource); result = jdbcTemplate.queryForList("select * from user"); ``` 以上就是使用Druid和Spring Boot多数据源支持来实现动态切换MySQL数据源的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值