一.导入mysql包
mysql-connector-java-5.0.8-bin.jar
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/606466af3154ab9e974506d299a29cb2.png)
二.配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--提供注释支持-->
<context:component-scan base-package="net.zrgj"/>
<!--配置数据源-->
<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/java_day19?useSSL=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--创建JDBCTemplate对象注入dataSource-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
三.定义实体类(Student类)
package net.zrgj.bean;
import org.springframework.stereotype.Component;
@Component
public class Student {
private int id;
private String name;
private String sex;
private int age;
public Student() {
}
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Student(int id, String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
this.id = id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
四.Dao层(IStudentDao接口)
package net.zrgj.dao;
import net.zrgj.bean.Student;
import java.util.List;
public interface IStudentDao {
//添加
boolean addStu(Student student);
//删除
boolean delStu(int stuId);
//修改
boolean updStu(Student student);
//查找单个
Student findById(int stuId);
//查找全部
List<Student> findAll();
}
五.DaoImpl(dao层实现)
package net.zrgj.dao.impl;
import net.zrgj.bean.Student;
import net.zrgj.dao.IStudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class StudentDaoImpl implements IStudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate=jdbcTemplate;
}
@Override
public boolean addStu(Student student) {
boolean flag = false;
String sql = "insert into tb_student values(null,?,?,?)";
int row = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge());
if (row > 0){
flag = true;
}
return flag;
}
@Override
public boolean delStu(int stuId) {
boolean flag = false;
String sql = "delete from tb_student where id = ?";
int row = jdbcTemplate.update(sql,stuId);
if (row > 0){
flag = true;
}
return flag;
}
@Override
public boolean updStu(Student student) {
boolean flag = false;
String sql = "update tb_student set name = ?,sex = ?,age = ? where id = ?";
int row = jdbcTemplate.update(sql,new Object[]{student.getName(),student.getSex(),student.getAge(),student.getId()});
if (row > 0){
flag = true;
}
return flag;
}
@Override
public Student findById(int stuId) {
String sql ="select * from tb_student where id = ?";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql,new Object[]{stuId}, rowMapper);
return student;
}
@Override
public List<Student> findAll() {
String sql ="select * from tb_student";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper(Student.class);
List<Student> students = jdbcTemplate.query(sql,rowMapper);
return students;
}
}
六.测试(Test)
package test;
import net.zrgj.bean.Student;
import net.zrgj.service.impl.StudentServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class TestSpring {
//加载配置文件applicationContext.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
@Test
public void addStu() {
//创建对象
Student student = new Student("亚索", "男", 19);
//获取自动创建的对象 --IOC
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
boolean b = ssi.addStu(student);
if (b) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
@Test
public void delStu() {
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
boolean b = ssi.delStu(1);
if (b) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
@Test
public void updStu() {
Student student = new Student(2, "亚索", "男", 20);
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
boolean b = ssi.updStu(student);
if (b) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
}
@Test
public void findAll() {
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
List<Student> students = ssi.findAll();
for (Student student : students) {
System.out.println("\n" + student);
}
}
@Test
public void findById() {
StudentServiceImpl ssi = (StudentServiceImpl) ac.getBean("studentServiceImpl");
Student student = ssi.findById(2);
System.out.println("\n" + student);
}
}