目录标题
- 1、工程目录
- `pom.xml`文件
- 2、创建数据表`student`
- 3、在resources目录创建配置文件
- `applicationContext.xml`
- `jdbc.properties`
- 4、创建Bean对象
- `Student`
- 5、创建Dao类
- `UserDao`接口
- 6、插入数据
- 一、直接注入jdbctemplate
- 二、继承jdbcdaosupport类
- 7、运行结果图
1、工程目录
pom.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>chapter09</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.19</version>
</dependency>
</dependencies>
</project>
2、创建数据表student
3、在resources目录创建配置文件
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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入外部 properties 文件 -->
<context:property-placeholder
location="classpath:jdbc.properties" />
<!-- 注册数据源 -->
<bean name="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--注册JdbcTemplate类 -->
<bean name="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="student" class="com.qfedu.bean.Student" />
<bean name="userDao01" class="com.qfedu.dao.UserDaoImpl01">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- <bean name="userDao02" class="com.qfedu.dao.UserDaoImpl02"> <property
name="jdbcTemplate" ref="jdbcTemplate" /> </bean> -->
<bean name="userDao02" class="com.qfedu.dao.UserDaoImpl02">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
jdbc.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/chapter09
jdbc.user=root
jdbc.password=123456
4、创建Bean对象
Student
package com.qfedu.bean;
public class Student {
private int sid;
private String sname;
private String age;
private String course;
public Student() {
super();
}
public Student(int sid, String sname, String age, String course) {
super();
this.sid = sid;
this.sname = sname;
this.age = age;
this.course = course;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", " + "age=" + age + ", " + "course=" + course + "]";
}
}
5、创建Dao类
UserDao
接口
package com.qfedu.dao;
import com.qfedu.bean.Student;
import java.util.List;
public interface UserDao {
public void insert(Student student); // 添加
public void delete(Integer sid); // 删除
public void update(Student role); // 更新
public Student selectOne(Integer sid); // 查询单个
public List<Student> selectAll(); // 查询所有
}
6、插入数据
一、直接注入jdbctemplate
UserDaoImpl01
实现类
package com.qfedu.dao;
import com.qfedu.bean.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class UserDaoImpl01 implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//实现增删改查
@Override
public void insert(Student student) {
String sql = "insert into student(sname,age,course) value (?,?,?) ";
jdbcTemplate.update(sql, student.getSname(), student.getAge(), student.getCourse());
}
@Override
public void delete(Integer sid) {
String sql = "delete from student where sid = ?";
jdbcTemplate.update(sql, sid);
}
@Override
public void update(Student student) {
String sql = "update student set sname = ? ,age = ? ,course = ?";
jdbcTemplate.update(sql, student.getSname(), student.getAge(), student.getCourse());
}
@Override
public Student selectOne(Integer sid) {
String sql = "select * from student where sid = ?";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
return jdbcTemplate.queryForObject(sql, rowMapper, sid);
}
@Override
public List<Student> selectAll() {
String sql = "select * from student";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
return jdbcTemplate.query(sql, rowMapper);
}
}
TestUserDao01
测试类
package com.qfedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qfedu.bean.Student;
import com.qfedu.dao.UserDao;
public class TestUserDao01 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean("userDao01", UserDao.class);
Student student = context.getBean("student", Student.class);
student.setSname("ZhouBa");
student.setAge("20");
student.setCourse("Java");
userDao.insert(student);
}
}
二、继承jdbcdaosupport类
UserDaoImpl02
实现类
package com.qfedu.dao;
import com.qfedu.bean.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class UserDaoImpl02 extends JdbcDaoSupport implements UserDao{
@Override
public void insert(Student student) {
String sql = "insert into student(sname,age,course) value (?,?,?) ";
getJdbcTemplate().update(sql, student.getSname(), student.getAge(), student.getCourse());
}
@Override
public void delete(Integer sid) {
String sql = "delete from student where sid = ?";
getJdbcTemplate().update(sql, sid);
}
@Override
public void update(Student student) {
String sql = "update student set sname = ? ,age = ? ,course = ?";
getJdbcTemplate().update(sql, student.getSname(), student.getAge(), student.getCourse());
}
@Override
public Student selectOne(Integer sid) {
String sql = "select * from student where sid = ?";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
return getJdbcTemplate().queryForObject(sql, rowMapper, sid);
}
@Override
public List<Student> selectAll() {
String sql = "select * from student";
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
return getJdbcTemplate().query(sql, rowMapper);
}
}
TestUserDao02
测试类
package com.qfedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qfedu.bean.Student;
import com.qfedu.dao.UserDao;
public class TestUserDao02 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean("userDao02", UserDao.class);
Student student = context.getBean("student", Student.class);
student.setSname("WuJiu");
student.setAge("19");
student.setCourse("Java");
userDao.insert(student);
}
}
直接注入数据源形式
直接修改applicationContext.xml
将下面的bean注释
<bean name="userDao02" class="com.qfedu.dao.UserDaoImpl02"> <property
name="jdbcTemplate" ref="jdbcTemplate" /> </bean>
添加dataSource注入数据源的bean
<bean name="userDao02" class="com.qfedu.dao.UserDaoImpl02">
<property name="dataSource" ref="dataSource" />
</bean>