學習Spring Data以前,咱們先經過幾種基礎的、常見的方式訪問數據庫,而後再經過Spring Data的方式訪問,能夠分別其中的優劣,對它們有一個更好的瞭解。java
有一個學生表,屬性有id/name/age,id自增,經過spring-jdbc的方式查詢全部數據,添加數據。mysql
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
create database spring_data; create table student( id int not null auto_increment, name varchar(20) not null, age int not null, primary key(id) ); insert into student (name, age) values("zhangsan", 20); insert into student (name, age) values("lisi", 21); insert into student (name, age) values("wangwu", 22);
功能:獲取Connection,關閉Connection、Statement、ResultSetspring
JDBCUtil.javasql
package com.dotleo.util; import java.sql.*; /** * 數據庫工具類 * @author LiuFei * @create 2017-11-05 21:13 */ public class JDBCUtil { /** * 獲取Connection * @return Connection 獲取到的jdbcConnection */ public static Connection getConnection() throws ClassNotFoundException, SQLException { /** * 不建議你們把配置硬編碼到代碼中 * * 最佳實踐: 配置性的建議寫到.properties配置文件中 */ String driverClass = "com.mysql.jdbc.Driver"; //驅動類 String url = "jdbc:mysql://localhost:3306/spring_data"; //數據庫url String username = "root"; //數據庫用戶 String password = "liufei"; //密碼 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, username, password); return connection; } /** * 釋放db相關的資源 * @param resultSet * @param statement * @param connection */ public static void release(ResultSet resultSet, Statement statement, Connection connection) { if(resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Student.java數據庫
package com.dotleo.entity; /** * Student 實體類 * @author LiuFei * @create 2017-11-05 21:35 */ public class Student { private int id; //主鍵 private String name; //姓名 private int age; //年齡 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
StudentDAO.javamaven
package com.dotleo.dao; import com.dotleo.entity.Student; import java.util.List; /** * 學生類DAO * @author LiuFei * @create 2017-11-05 21:38 */ public interface StudentDAO { /** * 查詢全部學生 * @return 全部學生 */ public List<Student> query(); /** * 添加一個學生 * @param student 學生 */ public int save(Student student); }
StudentDAOImpl.java工具
package com.dotleo.dao; import com.dotleo.entity.Student; import com.dotleo.util.JDBCUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * Student類DAO實現類,經過最原始的jdbc方式操做 * @author LiuFei * @create 2017-11-05 21:41 */ public class StudentDAOImpl implements StudentDAO{ public List<Student> query() { List<Student> students = new ArrayList<Student>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "select id, name , age from student"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); Student student = new Student(); student.setId(id); student.setName(name); student.setAge(age); students.add(student); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } return students; } public int save(Student student) { int result = 0; Connection connection = null; PreparedStatement preparedStatement = null; String sql = "insert into student (name, age) values (?, ?)"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, student.getName()); preparedStatement.setInt(2, student.getAge()); result = preparedStatement.executeUpdate(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtil.release(null, preparedStatement, connection); } return result; } }
JDBCUtilTest.java學習
package com.dotleo.util; import org.junit.Assert; import org.junit.Test; import java.sql.Connection; import java.sql.SQLException; /** * @author LiuFei * @create 2017-11-05 21:21 */ public class JDBCUtilTest { @Test public void testGetConnection() throws SQLException, ClassNotFoundException { Connection connection = JDBCUtil.getConnection(); Assert.assertNotNull(connection); } }
StudentDAOTest測試
package com.dotleo.dao; import com.dotleo.entity.Student; import org.junit.Test; import java.util.List; /** * @author LiuFei * @create 2017-11-05 21:57 */ public class StudentDAOTest { @Test public void testQuery() { StudentDAO studentDAO = new StudentDAOImpl(); List<Student> studentList = studentDAO.query(); for (Student student : studentList ) { System.out.println("id: " + student.getId() + "name: " + student.getName() + "age: " + student.getAge()); } } @Test public void testSave() { StudentDAO studentDAO = new StudentDAOImpl(); Student student = new Student(); student.setName("maliu"); student.setAge(23); int result = studentDAO.save(student); System.out.println(result); } }