一、SpringData简介
SpringData提供一致的,大家都熟悉的编程模型,为了简化数据库的访问。
子项目:
Spring Data JPA:减少数据层的开发量
Spring Data Mongo DB:基于分布式数据层的数据库,在大数据层用的比较多
Spring Data Redis:开源,由C语言编写的,支持网络、内存,而且可以持久化的,提供非常多的语言支持
Spring Data Solr:高性能 搜索功能 对查询性能优化
二、传统方式访问数据库
1、JDBC
- Connection
- Statement
- ResultSet
- TestCase
项目准备
- 新建maven 项目
- 选择maven-archetype-quickstart
- 设置GAV
- 添加依赖:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </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("刘备", 40); insert into student(name, age) values("关羽", 30); insert into student(name, age) values("张飞", 20);
开发JDBCUtil工具类
获取Connection,关闭Connection,Statement,ResultSet
db.properties
jdbc.driverClass = com.mysql.cj.jdbc.Driver
jdbc.username = root
jdbc.password = 123456
jdbc.url = jdbc:mysql://127.0.0.1:3306/data
package com.mouday.util; import java.io.InputStream; import java.sql.*; import java.util.Properties; /** * JDBC工具类 * 1. 获取Connection * 2. 释放资源 */ public class JDBCUtil { // 配置型内容建议放在配置文件中 private static final String driverClass = "com.mysql.cj.jdbc.Driver"; private static final String username = "root"; private static final String password = "123456"; private static final String url = "jdbc:mysql://127.0.0.1:3306/data"; /** * 获取Connection * * @return */ public static Connection getConnection() throws Exception { // 读取配置文件 InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); String driverClass = properties.getProperty("jdbc.driverClass"); String username = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); String url = properties.getProperty("jdbc.url"); Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, username, password); return connection; } /** * 关闭链接 * * @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(); } } } }
注意事项:
配置的属性放在配置文件中
实体类
package com.mouday.domain; /** * 学生实体类 */ public class Student { private Integer id; private String name; private Integer age; 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; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
接口类
package com.mouday.dao; import com.mouday.domain.Student; import java.util.List; /** * 学生访问接口 */ public interface StudentDao { /** * 获取所有学生 * @return */ List<Student> query(); /** * 插入数据 * @param student * @return */ Integer insert(Student student); }
实现类
package com.mouday.dao.impl; import com.mouday.dao.StudentDao; import com.mouday.domain.Student; import com.mouday.util.JDBCUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; /** * 学生访问接口实现类 */ public class StudentDaoImpl implements StudentDao { @Override public List<Student> query() { List<Student> students = new ArrayList<>(); Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String sql = "select id, name, age from student"; Student student; try { connection = JDBCUtil.getConnection(); statement = connection.prepareStatement(sql); resultSet = statement.executeQuery(); while (resultSet.next()) { Integer id = resultSet.getInt("id"); String name = resultSet.getString("name"); Integer age = resultSet.getInt("age"); student = new Student(); student.setId(id); student.setName(name); student.setAge(age); students.add(student); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, statement, connection); } return students; } @Override public Integer insert(Student student) { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; String sql = "insert into student (name, age) values (?, ?)"; Integer id = null; try { connection = JDBCUtil.getConnection(); statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); statement.setString(1, student.getName()); statement.setInt(2, student.getAge()); statement.executeUpdate(); // 获取自增id resultSet = statement.getGeneratedKeys(); if (resultSet.next()) { id = resultSet.getInt(1); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, statement, connection); } return id; } }
测试类
package com.mouday.util; import org.junit.Assert; import org.junit.Test; import java.sql.Connection; public class JDBCUtilTest { @Test public void testGetConnection() throws Exception{ Connection connection = JDBCUtil.getConnection(); Assert.assertNotNull(connection); } } package com.mouday.dao.impl; import com.mouday.dao.StudentDao; import com.mouday.domain.Student; import org.junit.Test; import java.util.List; public class StudentDaoImplTest { @Test public void testQuery() { StudentDao studentDao = new StudentDaoImpl(); List<Student> students = studentDao.query(); System.out.println(students); } @Test public void testInsert() { StudentDao studentDao = new StudentDaoImpl(); Student student = new Student(); student.setName("曹操"); student.setAge(50); Integer id = studentDao.insert(student); System.out.println(id); } }