繼續跟著上一節的節奏走,簡單快速的整合JDBC
首先的匯入JDBC和mySQL的SpringBoot Jar檔案
<!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
還需要在application.properties中配置如下代碼:
//資料庫連接地址,連接埠號碼,串連名,設定編碼格式spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8//使用者名稱spring.datasource.username=root//密碼spring.datasource.password=root//資料庫驅動spring.datasource.driver-class-name=com.mysql.jdbc.Driver
這是我的檔案位置,名稱截圖:
在建立entity檔案,在其裡面建個StudentEntity測試實體類:
public class StudentEntity { //id private Integer id; //姓名 private String name; //年齡 private Integer age; //性別 private String sex; //住址 private String address; //是否邏輯刪除(0:未刪除,1:已刪除) private Integer isDelete; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getIsDelete() { return isDelete; } public void setIsDelete(Integer isDelete) { this.isDelete = isDelete; }}
下來就直接到service了,建立StudentService介面代碼如下:
public interface StudentService { //寫入資料 int saveStudent(); //查詢資料 List<StudentEntity> queryAllStudent(); //更新資料 int updateStudent(StudentEntity StudentEntity); //刪除資料 int deleteStudent(Integer id);}
還要建立impl檔案,StudentServiceImpl類,代碼如下:
import com.demo.entity.StudentEntity;import com.demo.mapper.StudentMapper;import com.demo.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementSetter;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;@Service("studentServices")//別名public class StudentServiceImpl implements StudentService { @Resource private JdbcTemplate jdbcTemplate; //JDBC 寫入資料 @Override public int saveStudent() { //初始化屬性參數 String name = "張三"; Integer age = 12; //執行寫入 int row = jdbcTemplate.update("INSERT INTO student (name,age)VALUES (?,?);", "李四", 12); //返回結果 return row; } //JDBC 查詢資料 @Override public List<StudentEntity> queryAllStudent() { //SQL String sql = "SELECT * FROM student WHERE is_delete=0"; //結果 List<StudentEntity> list = jdbcTemplate.query(sql, new RowMapper<StudentEntity>() { //映射每行資料 @Override public StudentEntity mapRow(ResultSet rs, int rowNum) throws SQLException { StudentEntity stu = new StudentEntity(); stu.setId(rs.getInt("ID")); stu.setAge(rs.getInt("AGE")); stu.setName(rs.getString("NAME")); stu.setAddress(rs.getString("ADDRESS")); return stu; } }); //返回結果 return list; } //JDBC 更新資料 @Override public int updateStudent(StudentEntity studentEntity) { //SQL String sql = "update tudent set name=?,address=? where id=?"; //結果 int row = jdbcTemplate.update(sql, new PreparedStatementSetter() { //映射資料 @Override public void setValues(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setString(1, studentEntity.getName()); preparedStatement.setString(2, studentEntity.getAddress()); preparedStatement.setInt(3, studentEntity.getId()); } }); //反悔結果 return row; } //刪除資料 @Override public int deleteStudent(Integer id) { //SQL+結果 int resRow = jdbcTemplate.update("UPDATE student SET is_delete=1 WHERE id=?", new PreparedStatementSetter() { //映射資料 @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setInt(1, id); } }); //返回結果 return resRow; }}
最後就剩controller,建立StudentControllerImpl:代碼如下
import com.demo.entity.StudentEntity;import com.demo.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.util.ArrayList;import java.util.Iterator;import java.util.List;@RestControllerpublic class StudentControllerImpl { @Autowired private StudentService studentServices; /** *新增資料 * */ @RequestMapping("/save") public String save() { int row = studentServices.saveStudent(); //判斷結果 if(row==-1){ return "新增失敗"; }else{ return "新增成功"; } } /** * 查詢資料 * */ @RequestMapping("/query") public String query(){ //查尋資料 List list = studentServices.queryAllStudent(); //組裝資料 List newlist = new ArrayList(); //迴圈取出結果 for (int i = 0; i <list.size() ; i++) { //建立學生對象 StudentEntity stu = (StudentEntity) list.get(i); //填充資料 newlist.add(stu.getId()); newlist.add(stu.getName()); newlist.add(stu.getAge()); newlist.add(stu.getSex()); newlist.add(stu.getAddress()); } //返回資料 return newlist.toString(); } /** * 更新資料 * */ @RequestMapping("/update") public String update(){ //建立對象傳遞資料 StudentEntity stu = new StudentEntity(); stu.setId(2); stu.setName("尼古拉斯"); stu.setAddress("東北"); //執行更新操作 int row = studentServices.updateStudent(stu); //判斷結果 if(row==-1){ return "更新失敗"; }else { return "更新成功"; } } /** * 刪除資料 * */ @RequestMapping("/delete") public String delete(){ //初始化資料 Integer id = 3; //執行刪除 int row = studentServices.deleteStudent(id); //判斷結果 if(row==-1){ return "刪除失敗"; }else{ return "刪除成功"; } }}
這就完事了,啟動運行我的URL:localhost:8080/save, 請各位路過的大神多多指教,如有問題或者疑問請留言,