简单记录一下踩过的坑。
1.配置准备:
(1)首先下载程序所需的jar包:commons-pool.jar; commons-dbcp.jar;
mysql-connector-java.jar ;spring-jdbc-5.1.0.RELEASE.jar;
spring-tx- 5.1.0.RELEASE.jar.
(2)进行beans的配置:
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="studentDao" class="student.set.cla.StudentManage">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="studentService" class="student.get.cla.StudentService">
<property name="studentDao" ref="studentDao"></property>
</bean>
</beans>
(3)进行jdbc.properties的配置:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
注意:根据自己的mysql-connector-java.jar的版本选择合适的驱动名称,并且mysql版本对jdbc.url的值有一定的影响。
(4)代码编写:
先编写StudentDao接口,写入方法。创建StudentManage类引入接口:写入增删改查方法。
public class StudentManage implements StudentImp{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public int addStudent(Student student) {
// TODO Auto-generated method stub
String sql="insert into tablestu values(null,?,?)";
Object []params=new Object[]{student.getName(),student.getAge()};
return jdbcTemplate.update(sql, params);
}
@Override
public int updateStudent(Student student) {
// TODO Auto-generated method stub
String sql="update tablestu set name=?,age=? where id=?";
Object []params=new Object[]{student.getName(),student.getAge(),student.getId()};
return jdbcTemplate.update(sql, params);
}
@Override
public int deleteStudent(int id) {
// TODO Auto-generated method stub
String sql="delete from tablestu where id=?";
Object []params=new Object[]{id};
return jdbcTemplate.update(sql, params);
}
@Override
public List<Student> findStudent() {
// TODO Auto-generated method stub
String sql="select * from tablestu";
final List<Student> students=new ArrayList<Student>();
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Student student=new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
students.add(student);
}
});
return students;
}
}
(注意以上操作中的查询操作与其他有所不同,采用JdbcTemplate的query操作。而不是update)
然后写StudentServiceIml接口,方法同上,建立StudentService类写入:
public class StudentService implements StudentServiceImp{
private StudentImp studentDao;
public void setStudentDao(StudentImp studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
// TODO Auto-generated method stub
return studentDao.addStudent(student);
}
@Override
public int updateStudent(Student student) {
// TODO Auto-generated method stub
return studentDao.updateStudent(student);
}
@Override
public int deleteStudent(int id) {
// TODO Auto-generated method stub
return studentDao.deleteStudent(id);
}
@Override
public List<Student> findStudent() {
// TODO Auto-generated method stub
return studentDao.findStudent();
}
}
最后,写Student类写入你所建立数据库中对应的参数如:id,name,age等,我所写程序建立的数据库参数设置均可为空值。
(5)附上源码以及所需jar包:
(https://download.csdn.net/download/weixin_43324905/10718727)