Spring Boot入門系列七(SpringBoot 使用JDBC串連Mysql資料庫)

來源:互聯網
上載者:User

標籤:root   rom   connect   border   pom   最簡   顯示   串連mysql   word   

SpringBoot 使用JDBC串連Mysql資料庫

    Spring串連Mysql的方式有很多,例如JDBC,Spring JPA,Hibeirnate,Mybatis等,本文主要介紹使用最簡單、最底層的JDBC方式來串連Mysql資料庫,JDBC串連資料庫,主要是注入JdbcTemplate,使用JdbcTemplate來操作資料庫。

一、在mysql中的test庫中建立user表,並插入兩條資料,為後續做好準備

    

二、在pom.xml中添加依賴
<!--JDBC--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>

    

三、添加設定檔設定資料庫和其他參數

在resource檔案夾下添加application.properties設定檔並輸入資料庫參數,如下:

############################################################## mysql#############################################################spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.max-idle=10spring.datasource.max-wait=10000spring.datasource.min-idle=5spring.datasource.initial-size=5

    

四、新增實體類User.java,屬性與資料庫user表相對應
/** * @author oyc * @Description:使用者實體類 * @date 2018/7/8 22:51 */public class User {    //使用者id    private String id;    //使用者名稱稱    private String name;    //戶年齡    private String age;    //使用者性別    private String sex;    //此處省略getter、setter方法}

  

    

五、建立測試類別串連資料庫
/** * @author oyc * @Description: 使用者控制類 * @date 2018/7/8 22:10 */@Controller@RequestMapping("/jdbc")public class JdbcController {    @Resource    private JdbcTemplate jdbcTemplate;    @RequestMapping("/userlist")    public String getUserList(ModelMap map){        String sql = "SELECT * FROM user";        List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {            User user = null;            @Override            public User mapRow(ResultSet rs, int rowNum) throws SQLException {                user = new User();                user.setId(rs.getString("id"));                user.setName(rs.getString("name"));                user.setSex(rs.getString("sex"));                user.setAge(rs.getString("age"));                return user;            }});        for(User user:userList){            System.out.println(user.getName());        }        map.addAttribute("users", userList);        return "user";    }}

  

六、新增一個thymeleaf模板頁面user.html用於顯示使用者資訊列表

      

七、使用瀏覽器測試

    

 

Spring Boot入門系列七(SpringBoot 使用JDBC串連Mysql資料庫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.