Spring Data 学习 01 :JDBC 访问 MySQL
Spring Data 学习 02 :Spring JdbcTemplate 访问 MySQL
Spring Data 学习 03 :JPA
Spring Data 学习 04:Spring Data JPA
一、依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
二、创建bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
// 创建完StudentDAOSpringJDBCTemplateImpl 后将其添加进来
<bean id="studentDAO" class="com.springboot.dao.StudentDAOSpringJDBCTemplateImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
三、对bean.xml进行单元测试
package com.springboot.utils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class DataSourceTest {
private ApplicationContext ctx = null;
@Before
public void setup(){
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
@After
public void tearDown(){
ctx = null;
}
@Test
public void testDataSource(){
DataSource dataSource = (DataSource)ctx.getBean("dataSource");
Assert.assertNotNull(dataSource);
}
@Test
public void testJdbcTemplate(){
JdbcTemplate jdbcTemplate= (JdbcTemplate)ctx.getBean("jdbcTemplate");
Assert.assertNotNull(jdbcTemplate);
}
}
四、创建 Student
DAO接口的实现类 StudentDAOSpringJDBCTemplateImpl
package com.springboot.dao;
import com.springboot.dao.StudentDAO;
import com.springboot.domain.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAOSpringJDBCTemplateImpl implements StudentDAO {
// 注入JdbcTemplate 生成getter and setter
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 继承接口的查询
@Override
public List<Student> query() {
final ArrayList<Student> students = new ArrayList<>();
String sql = "select id, name, age from student";
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
Student student = new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
students.add(student);
}
});
return students;
}
// 继承接口的 添加
@Override
public void save(Student student) {
String sql = "insert into student(name ,age) values (?,?)";
jdbcTemplate.update(sql,new Object[]{student.getName(),student.getAge()});
}
}
五、对查询、新增 进行测试
package com.springboot.dao;
import com.springboot.domain.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class StudentDAOSpringJdbcTemplateImplTest {
private ApplicationContext ctx = null;
protected StudentDAO studentDAO =null;
@Before
public void setup(){
ctx = new ClassPathXmlApplicationContext("beans.xml");
studentDAO = (StudentDAO)ctx.getBean("studentDAO");
}
@After
public void tearDown(){
ctx = null;
}
// 查询所有记录
@Test
public void testQuery(){
List<Student> students = studentDAO.query();
for (Student student:students){
System.out.println("id"+student.getId()
+"name"+student.getName()
+"age"+student.getAge()
);
}
}
// 新增一个记录
@Test
public void testSave(){
Student student = new Student();
student.setName("小辣椒");
student.setAge(25);
studentDAO.save(student);
}
}
网友评论