代码:https://download.csdn.net/download/qq_43499543/19349626
1.编写pojo层
public class Student {
private int id;
private String name;
private String sex;
//省略了get、set、constructor
}
2.编写dao层
StudentDao.java
package dao;
import pojo.Student;
public interface StudentDao {
void insert(Student student);
}
StudentDaoImp.java
package dao;
import org.springframework.jdbc.core.JdbcTemplate;
import pojo.Student;
public class StudentDaoImp implements StudentDao{
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void insert(Student student) {
String sql = "insert into `t_student`(t_student.`name`,t_student.`sex`) VALUES(?,?);";
this.jdbcTemplate.update(sql,student.getName(),student.getSex());
}
}
3.编写xml配置文件
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/temp"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--配置JDBC模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置注入实体类-->
<bean id="studentDao" class="dao.StudentDaoImp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
4.进行单元测试
package dao;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;
public class StudentDaoImpTest extends TestCase {
@Test
public void testInsert() {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从配置文件中获取配置好的studentDao
StudentDaoImp studentDao = (StudentDaoImp) context.getBean("studentDao");
Student student = new Student("pp","m");
studentDao.insert(student);
}
}
PS:
Maven依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 为了方便,直接引入webmvc包,将会全部导入spring依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- Spring-jdbc 用于配置JdbcTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
数据库