数据库配置
因为使用的是jdbc注解版,但是无法在相应的dataSource贴标签,所以依然需要XML来配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 引入属性配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="com.revanwang.jdbc.dao.impl"/>
<context:component-scan base-package="com.revanwang.jdbc.service.impl"/>
<context:component-scan base-package="com.revanwang.jdbc.action"/>
<!--配置缓冲池-->
<bean id="dataBaseDB" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>
<!--配置DAO-->
<bean id="studentDAO" class="com.revanwang.jdbc.dao.impl.StudentDAOImpl">
<property name="dataSource" ref="dataBaseDB"/>
</bean>
</beans>
domain(Student)
package com.revanwang.jdbc.domain;
import lombok.Data;
@Data
public class Student {
private Long id;
private String name;
private Integer age;
}
dao(StudentDAOImpl)
XML中配置DAO的id="studentDAO"要和注解@Repository("studentDAO")相同
package com.revanwang.jdbc.dao.impl;
import com.revanwang.jdbc.dao.IStudentDAO;
import com.revanwang.jdbc.domain.Student;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//studentDAO必须要和XML中的引用dataSource的id名称相同
@Repository("studentDAO")
public class StudentDAOImpl extends JdbcDaoSupport implements IStudentDAO {
@Override
public void save(Student student) {
String sql = "INSERT INTO Student (name, age) VALUES(?, ?)";
super.getJdbcTemplate().update(sql, student.getName(), student.getAge());
}
@Override
public void delete(Student student) {
String sql = "DELETE FROM Student WHERE id = ?";
super.getJdbcTemplate().update(sql, student.getId());
}
@Override
public void update(Student student) {
String sql = "UPDATE Student SET name = ?, age = ? WHERE id = ?";
super.getJdbcTemplate().update(sql, student.getName(), student.getAge(), student.getId());
}
@Override
public Student get(Long id) {
String sql = "SELECT * FROM Student WHERE id = ?";
List<Student> students = super.getJdbcTemplate().query(sql, new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet result, int i) throws SQLException {
Student student = new Student();
student.setId(result.getLong("id"));
student.setName(result.getString("name"));
student.setAge(result.getInt("age"));
return student;
}
}, id);
return students.size() == 1 ? students.get(0) : null;
}
@Override
public List<Student> getList() {
String sql = "SELECT * FROM Student";
List<Student> studentList = super.getJdbcTemplate().query(sql, new RowMapper<Student>() {
/**
* 把数据库中一行数据封装成 Student 对象
* 自动把每一行封装的Student对象存储在List集合中,并返回集合
*/
@Override
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
Student student = new Student();
student.setId(resultSet.getLong("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
return student;
}
});
return studentList;
}
}
service(StudentServiceImpl)
package com.revanwang.jdbc.service.impl;
import com.revanwang.jdbc.dao.IStudentDAO;
import com.revanwang.jdbc.domain.Student;
import com.revanwang.jdbc.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service()
public class StudentServiceImpl implements IStudentService {
@Autowired
private IStudentDAO studentDAOImple;
@Override
public void save(Student student) {
studentDAOImple.save(student);
}
@Override
public void delete(Student student) {
studentDAOImple.delete(student);
}
@Override
public void update(Student student) {
studentDAOImple.update(student);
}
@Override
public Student get(Long id) {
return studentDAOImple.get(id);
}
@Override
public List<Student> getList() {
return studentDAOImple.getList();
}
}
Action(StudentAction)
package com.revanwang.jdbc.action;
import com.revanwang.jdbc.domain.Student;
import com.revanwang.jdbc.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
@Controller("studentAction")
public class StudentAction {
@Autowired
private IStudentService studentService;
public void save(Student student) {
this.studentService.save(student);
}
public void delete(Student student) {
this.studentService.delete(student);
}
public void update(Student student) {
this.studentService.update(student);
}
public Student get(Long id) {
return this.studentService.get(id);
}
public List<Student> getList() {
return this.studentService.getList();
}
}
JdbcTest
package com.revanwang.jdbc.test;
import com.revanwang.jdbc.action.StudentAction;
import com.revanwang.jdbc.domain.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTest {
@Autowired
private ApplicationContext ctx;
@Autowired
private StudentAction stuAction;
@Test
public void testSave() {
Student student = new Student();
student.setName("WRW");
student.setAge(28);
StudentAction action = ctx.getBean("studentAction", StudentAction.class);
action.save(student);
}
@Test
public void testDelete() {
Student student = new Student();
student.setId(1L);
StudentAction action = ctx.getBean("studentAction", StudentAction.class);
action.delete(student);
}
@Test
public void testUpdate() {
Student student = new Student();
student.setId(4L);
student.setName("乔峰");
student.setAge(119);
StudentAction action = ctx.getBean("studentAction", StudentAction.class);
action.update(student);
}
@Test
public void testGet() {
StudentAction action = ctx.getBean("studentAction", StudentAction.class);
Student student = action.get(2L);
System.out.println(student);
}
@Test
public void testGetList() {
StudentAction action = ctx.getBean("studentAction", StudentAction.class);
List<Student> studentList = action.getList();
for (Student student:studentList) {
System.out.println(student);
}
}
}
网友评论