SpringBoot整合使用JdbcTemplate

來源:互聯網
上載者:User

標籤:ica   cat   temp   request   oca   enc   control   roo   內建   

JdbcTemplate是Spring架構內建的對JDBC操作的封裝,目的是提供統一的模板方法使對資料庫的操作更加方便、友好,效率也不錯。

整合使用JdbcTemplate實現對圖書的添加功能小案例

 採用springboot2.0.0版本

1.匯入所需依賴jar包

<!--web應用-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!--單測-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <!--jdbc -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>        </dependency>        <!-- mysql驅動 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>

2.application.properties中的配置

1 spring.datasource.url=jdbc:mysql://localhost:3306/bookshop2 spring.datasource.username=root3 spring.datasource.password=1234 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

3.entity層

 1 @Entity(name = "book") 3 public class Book { 4     @Id 5     @GeneratedValue 6     private Integer bookid; 7     @Column 8     private String bookname; 9     @Column10     private Integer bookprice;11    12    get  set方法省略。。13 }

4.service層

 1 @Service 2 public class BookService { 3     @Autowired 4     private JdbcTemplate jdbcTemplate; 5     public void createUser(Integer booid,String bookname,Integer bookprice){ 6         System.out.println("createUser"); 7         jdbcTemplate.update("insert into book values(?,?,?);",booid,bookname,bookprice); 8         System.out.println("圖書添加成功!!"); 9     }10 11 }

5.controller層

 1 @Controller 2 public class BookController { 3     @Autowired 4     private BookService userService; 5  6  7     @RequestMapping("/createUser") 8     public String createUser(Integer booid,String bookname,Integer bookprice){ 9         userService.createUser(booid,bookname,bookprice);10        return "success";11     }12 }

6.success.ftl

 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 3       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> 4 <head> 5     <title>Hello World!</title> 6 </head> 7 <body> 8 <h1>success</h1> 9 </body>10 </html>

 

7.啟動項目

 

控制台列印

 

SpringBoot整合使用JdbcTemplate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.