SpringBoot---整合mysql資料庫
阿新 • • 發佈:2018-12-19
SpringBoot 連線mysql資料庫,與Spring MVC 相比較而言,省去了配置檔案,直接將配置通過字串的方式寫到application.properties 檔案中,配置如下:
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jackson.time-zone=Asia/Chongqing
專案大致結構如下:
dao 介面層:
package com.nyist.sql.dao; import com.nyist.sql.model.RoncooUser; public interface RoncooUserDao { int insert(RoncooUser roncooUser); int deleteById(int id); int updateById(RoncooUser roncooUser); RoncooUser selectById(int id); }
dao Impl 實現層:
package com.nyist.sql.dao.Impl; import com.nyist.sql.dao.RoncooUserDao; import com.nyist.sql.model.RoncooUser; import com.nyist.sql.util.base.JdbcDaoImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; @Repository public class RoncooUserDaoImpl extends JdbcDaoImpl implements RoncooUserDao { //JdbcTemplate 是 SpringBoot 特有的一個包 支援對資料庫的原生開發 @Autowired private JdbcTemplate jdbcTemplate; @Override public int insert(RoncooUser roncooUser) { String sql = "insert into roncoo_user(name, create_time) values (?, ?)"; return jdbcTemplate.update(sql, roncooUser.getName(),roncooUser.getCreateTime()); } @Override public int deleteById(int id) { String sql = "delete from roncoo_user where id=?"; return jdbcTemplate.update(sql, id); } @Override public int updateById(RoncooUser roncooUser) { String sql = "update roncoo_user set name=?, create_time=? where id=?"; return jdbcTemplate.update(sql, roncooUser.getName(), roncooUser.getCreateTime(), roncooUser.getId()); } @Override public RoncooUser selectById(int id) { String sql = "select * from roncoo_user where id=?"; /*RoncooUser roncooUser = jdbcTemplate.queryForObject(sql, new RowMapper<RoncooUser>() { @Override public RoncooUser mapRow(ResultSet rs, int rowNum) throws SQLException { RoncooUser roncooUser = new RoncooUser(); roncooUser.setId(rs.getInt("id")); roncooUser.setName(rs.getString("name")); roncooUser.setCreateTime(rs.getDate("create_time")); return roncooUser; } }, id);*/ return queryForObject(sql,RoncooUser.class,id); } }
JdbcDaoImpl 封裝類
/** * 2015-2016 龍果學院 (www.roncoo.com) */ package com.nyist.sql.util.base; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; /** * spirng jdbc 基本支撐類 * * @author wujing */ public abstract class JdbcDaoImpl { @Autowired protected JdbcTemplate jdbcTemplate; /** * 獲取當前事務最後一次更新的主鍵值 */ public Long getLastId() { return jdbcTemplate.queryForObject("select last_insert_id() as id", Long.class); } /** * 獲取物件資訊 */ public <T> T queryForObject(String sql, Class<T> clazz, Object... args) { Assert.hasText(sql, "sql 語句不能為空"); return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<T>(clazz), args); } /** * 獲取物件集合資訊 */ public <T> List<T> queryForObjectList(String sql, Class<T> clazz, Object... args) { Assert.hasText(sql, "sql 語句不能為空"); return jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<T>(clazz)); } /** * 分頁,jdbcTemplate 不支援like */ public Page<Map<String, Object>> queryForPage(String sql, int pageCurrent, int pageSize, Object... args) { Assert.hasText(sql, "sql 語句不能為空"); Assert.isTrue(pageCurrent >= 1, "pageNo 必須大於等於1"); String sqlCount = Sql.countSql(sql); int count = jdbcTemplate.queryForObject(sqlCount, Integer.class, args); pageCurrent = Sql.checkPageCurrent(count, pageSize, pageCurrent); pageSize = Sql.checkPageSize(pageSize); int totalPage = Sql.countTotalPage(count, pageSize); String sqlList = sql + Sql.limitSql(count, pageCurrent, pageSize); List<Map<String, Object>> list = jdbcTemplate.queryForList(sqlList, args); return new Page<Map<String, Object>>(count, totalPage, pageCurrent, pageSize, list); } /** * 分頁,jdbcTemplate 不支援like */ public <T> Page<T> queryForPage(String sql, int pageCurrent, int pageSize, Class<T> clazz, Object... args) { Assert.hasText(sql, "sql 語句不能為空"); Assert.isTrue(pageCurrent >= 1, "pageNo 必須大於等於1"); Assert.isTrue(clazz != null, "clazz 不能為空"); String sqlCount = Sql.countSql(sql); int count = jdbcTemplate.queryForObject(sqlCount, Integer.class, args); pageCurrent = Sql.checkPageCurrent(count, pageSize, pageCurrent); pageSize = Sql.checkPageSize(pageSize); int totalPage = Sql.countTotalPage(count, pageSize); String sqlList = sql + Sql.limitSql(count, pageCurrent, pageSize); List<T> list = jdbcTemplate.query(sqlList, new BeanPropertyRowMapper<T>(clazz), args); return new Page<T>(count, totalPage, pageCurrent, pageSize, list); } }
application.properties 配置:
#開發環境 server.port=8888 spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jackson.time-zone=Asia/Chongqing
SpringBootTest 測試類:
package com.nyist.sql; import com.nyist.sql.dao.RoncooUserDao; import com.nyist.sql.model.RoncooUser; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest public class SqlApplicationTests { @Autowired private RoncooUserDao roncooUserDao; @Test public void insert() { RoncooUser roncooUser = new RoncooUser(); roncooUser.setName("測試"); roncooUser.setCreateTime(new Date()); int result = roncooUserDao.insert(roncooUser); System.out.println(result); } @Test public void delete() { int result = roncooUserDao.deleteById(1); System.out.println(result); } @Test public void update() { RoncooUser roncooUser = new RoncooUser(); roncooUser.setId(2); roncooUser.setName("測試 2"); roncooUser.setCreateTime(new Date()); int result = roncooUserDao.updateById(roncooUser); System.out.println(result); } @Test public void select() { RoncooUser result = roncooUserDao.selectById(2); System.out.println(result); } }
執行結果:
通過Navicate 資料庫視覺化工具可以看到 插入yi't一條記錄成功。