spring boot(三) 之 使用JdbcTemplate访问MySQL数据库

spring boot(三) 之 使用JdbcTemplate访问MySQL数据库

1. 使用JdbcTemplate之前的准备
为了能使用JdbcTemplate,添加JDBC starter 依赖。

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

对于开发和测试来说,嵌入式的数据库就足够了,Spring Boot提供自动配置的嵌入式数据库有H2、HSQL、Derby,你不需要提供任何连接配置就能使用。比如使用H2嵌入数据库。

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
 </dependency>

连接生产数据源,一般用于生产环境,以MySQL为例,先引入MySQl依赖包

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
 </dependency>

然后再在 src/main/resources/application.properties 或者src/main/resources/application.yml 路径下配置数据源信息

spring.datasource.url=jdbc:mysql://localhost:3306/数据库名
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

注意:因为Spring Boot 2.1.x默认使用了MySQL 8.0的驱动,所以这里用com.mysql.cj.jdbc.Driver,而不是com.mysql.jdbc.Driver。

2. 使用JdbcTempla操作数据库

通过举一个例子来介绍吧。看完这个例子,使用JdbcTempla操作数据库对你来说就just so so了
1.首先数据库的准备
ingredients表,
在这里插入图片描述在这里插入图片描述
dumping表,主键自动递增
在这里插入图片描述
和dumping_ingredients表(将dumping与之相关的配料映射在一起)
在这里插入图片描述
外键约束
在这里插入图片描述

2.编写领域对象

@Data
@RequiredArgsConstructor
public class Ingredients {
    private final String id;
    private final String name;
    private final String type;


}

@Data
public class Dumping {
    private Long id;
    private String name;
    private String place;
    private List<Ingredients> ingredients;
}

3.定义JDBC repository

  1. 明确要完成操作,例如完成以下操作
  • 查询所有配料信息;
  • 根据id查询单个Ingredients;
  • 保存Ingredient对象
  1. 声明对应接口
public interface IngredientsRepository {

    // 查询所有的配料信息,将它们放到一个Ingredients对象集合中
    List<Ingredients> findAll();

    // 根据id查询单个ingredients
    Ingredients findOne(String id);

    // 保存Ingredients对象
    Ingredients save(Ingredients ingredients);

}
  1. 开始使用JdbcTemplate实现接口
@Repository         //spring的组件扫描到,并会将其初始化spring应用上下文的bean
public class JdbcIngredientsRepository implements IngredientsRepository{


    private JdbcTemplate jdbc;    

    @Autowired			//通过@Autowired标注的构造器将JdbcTemplate注入进来
    public JdbcIngredientsRepository(JdbcTemplate jdbc){
        this.jdbc = jdbc;
    }
}

下面就是实现查找所有和查找id的方法

 @Override
    public List<Ingredients> findAll() {
       return jdbc.query("select * from Ingredients",this::mapRowToIngredients);
    }

    @Override
    public Ingredients findOne(String id) {
        Ingredients  ingredients = jdbc.queryForObject("select * from Ingredients where id = ?",
                this::mapRowToIngredients,id);
        return ingredients;
    }
 private Ingredients mapRowToIngredients(ResultSet rs, int rowNum) throws SQLException {
        return new Ingredients(
                rs.getString("id"),
                rs.getString("name"),
                rs.getString("type"));

    }

接下来就先讲解一下上面的方法。findAll()方法放回一个对象集合,使用JdbcTemplate的query()方法,接受要执行的SQL语句以及Spring RowMapper的一个实现类(用来将查询的结果集的每行数据映射为一个对象)。:: 是java8的新特性——方法引用。简单介绍一下:当用lambda表达式去创建一个函数式接口对象时,当lambda体已经有实现的方法了,就可以用。方法引用
findOne()使用了queryForObject()方法,返回一个对象,id会替换查询中的“?”
mapRowToIngredients()方法就是将结果集rs中的第rowNum行生成一个Ingredients对象,rowNum会从0开始到结果集的最后一行。

再看看保存(插入)数据的代码

@Override
    public Ingredients save(Ingredients ingredients) {
        jdbc.update(
                "insert into Ingredients(id,name,type) values (?,?,?)",
                ingredients.getId(),
                ingredients.getName(),
                ingredients.getType()
                );
        return ingredients;
    }

JdbcTemplate的update()方法用来执行向数据库中写入或更新数据的查询语句。
它只需要一个包含带执行SQL,和每个查询参数对应的值即可。

  1. 在控制器注入和使用repository
@Controller
@RequestMapping("/query")
public class DumpController {

    private final IngredientsRepository ingredientsRepo;
   
    @Autowired
    private DumpController(IngredientsRepository ingredientsRepo){
        this.ingredientsRepo = ingredientsRepo;
       
    }

    @GetMapping
    @ResponseBody
    public Ingredients findOne(@RequestParam("id")String id){
        return ingredientsRepo.findOne(id);
    }

    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/all")
    @ResponseBody
    public List<Ingredients> findAll(){
        return ingredientsRepo.findAll();
    }
}

@GetMapping标注在方法上表示该方法用来处理get请求,@ResponseBody表示该方法的结果直接写入Http响应体中。@RequestParam(“id”)表示请求的参数“id”的数据映射到后面的参数id上。
因为有时前端的应用程序将会运行在与API相互独立的主机或者端口上,web浏览器会阻止消费该API,可以在服务器添加CORS(Cross-Origin Resource Sharing,跨域资源共享)。

展示请求和结果
在这里插入图片描述
在这里插入图片描述
篇幅太长了,分两次写吧。
未完待续

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以通过在Spring Boot使用JdbcTemplate或MyBatis等数据访问框架来执行SQL语句,从而向MySQL数据库插入数据。以下是一个使用JdbcTemplate的示例: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 然后,在application.properties文件中配置MySQL数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 接着,创建一个Java类,使用JdbcTemplate执行SQL语句: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class DataInsertion { @Autowired private JdbcTemplate jdbcTemplate; public void insertData() { String sql = "INSERT INTO user(name, age) VALUES (?, ?)"; Object[] params = new Object[]{"Tom", 20}; jdbcTemplate.update(sql, params); } } ``` 4. 最后,在Spring Boot的启动类中调用insertData方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { @Autowired private DataInsertion dataInsertion; public static void main(String[] args) { SpringApplication.run(Application.class, args); } public void run(String... args) throws Exception { dataInsertion.insertData(); } } ``` 这样,当运行Spring Boot应用程序时,就会执行insertData方法,向MySQL数据库插入一条数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子时不睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值