jdbc课程管理的增删改查-后端测试

1.我们先建立一个实体类Course,定义以下属性值,并生成get(),set(),toString()方法

  并建立数据库(我这里配置的数据库为Mysql8.0),要和这里的值保持一致,id自增,成为主键。

 private Integer id;
    private String curriculum;
    private String tp;
    private Integer credit;

2.在Dao层建立CourseDao接口文件,增删改查方法

1 public interface CourseDao {
2     public int addCourse(Course course);
3     public int updateCourse(Course course);
4     public int deletecourse(int id);
5     public Course findCourseById(int id);
6     public Course findCourseByName(String curriculum);
7     public List<Course> findAllCourse();
8 }

 

3.根据接口文件生成CourseDao的CourseDaoImpl.java文件,用sql语句实现对Mysql的增删改查

 1 @Repository("courseDao")
 2 public class CourseDaoImpl implements CourseDao {
 3 
 4     private JdbcTemplate jdbcTemplate;
 5     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 6         this.jdbcTemplate = jdbcTemplate;
 7     }
 8     
 9     @Override
10     public int updateCourse(Course course) {
11         String sql="update course set curriculum=?,tp=?,credit=? where id=?";
12         Object[] params=new Object[] {
13                 course.getCurriculum(),
14                 course.getTp(),    
15                 course.getCredit(),
16                 course.getId()
17         };
18         int num = this.jdbcTemplate.update(sql,params);
19         System.out.println("修改了"+num+"条数据");
20         return num;
21     }
22 
23     
24     @Override
25     public int addCourse(Course course) {
26         String sql="insert into course(curriculum,tp,credit) values(?,?,?)";
27         Object[] obj=new Object[] {
28                 course.getCurriculum(),
29                 course.getTp(),    
30                 course.getCredit()        
31         };
32         int num = this.jdbcTemplate.update(sql,obj);
33         System.out.println("增加了"+num+"条数据");
34         return num;
35     }
36     @Override
37     public int deletecourse(int id) {
38         String sql="delete from course where id=?";
39         int num=this.jdbcTemplate.update(sql,id);
40         System.out.println("删除了"+num+"条数据");
41         return num;
42     }
43     @Override
44     public Course findCourseById(int id) {
45         String sql="select * from course where id = ?";
46         RowMapper<Course> rowMapper =new BeanPropertyRowMapper<Course>(Course.class);
47         System.out.println(this.jdbcTemplate.queryForObject(sql, rowMapper,id));
48         return this.jdbcTemplate.queryForObject(sql, rowMapper,id);
49     }
50     @Override
51     public List<Course> findAllCourse() {
52         String sql="select * from course";
53         RowMapper<Course> rowMapper =new BeanPropertyRowMapper<Course>(Course.class);
54         List<Course> all=this.jdbcTemplate.query(sql,rowMapper);
55         for (Course cou : all) {
56             System.out.println(cou);
57         }
58         return this.jdbcTemplate.query(sql,rowMapper);
59     }
60     @Override
61     public Course findCourseByName(String curriculum) {
62         String sql="select * from course where curriculum = ?";
63         RowMapper<Course> rowMapper =new BeanPropertyRowMapper<Course>(Course.class);
64         System.out.println(this.jdbcTemplate.queryForObject(sql, rowMapper,curriculum));
65         return this.jdbcTemplate.queryForObject(sql, rowMapper,curriculum);
66     }

 

4.applicationContext.xml配置数据库,及注解注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
         <context:component-scan base-package="com.lcu.wl"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="url" value="jdbc:mysql://localhost:3306/edusystem?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT"></property>
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
        </bean>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <bean id="courseDao" class="com.lcu.wl.dao.CourseDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        
</beans>

 

5.业务层Service

5.1CourseService

1 public interface CourseService {
2     boolean exist(Course course);
3     public void deleteCourse(int id);
4     public void findCourseAll();
5     public void findCourseById(int id);
6     public void addCourse(Course course);
7     public void updateCourse(Course course);
8 }

5.2CourseServiceImpl

 1 @Service("courseService")
 2 public class CourseServiceImpl implements CourseService {
 3     
 4     @Autowired
 5     private CourseDao courseDao;
 6 
 7     @Override
 8     public void deleteCourse(int id) {
 9         courseDao.deletecourse(id);
10     }
11 
12     @Override
13     public void findCourseAll() {
14         courseDao.findAllCourse();
15     }
16 
17     @Override
18     public void findCourseById(int id) {
19         courseDao.findCourseById(id);
20     }
21 
22     @Override
23     public void addCourse(Course course) {
24         courseDao.addCourse(course);
25     }
26 
27     @Override
28     public void updateCourse(Course course) {
29         courseDao.updateCourse(course);
30     }
31 
32     @Override
33     public boolean exist(Course course) {
34         Course cou=courseDao.findCourseByName(course.getCurriculum());
35         if(cou!=null) {
36             System.out.println("已存在");
37             return false;
38         }
39         return true;
40         
41     }
42 }

6.控制层Controller

 

 1 @Controller("courseController")
 2 public class CourseController {
 3     
 4     @Autowired
 5     private CourseService coureseService;
 6     
 7     public void delete(int id) {
 8         this.coureseService.deleteCourse(id);
 9     }
10     
11     public void findAll() {
12         this.coureseService.findCourseAll();
13     }
14     
15     public void findById(int id) {
16         this.coureseService.findCourseById(id);
17     }
18     
19     public void add(Course course) {
20         course.setCurriculum("java");
21         course.setTp("必修");
22         course.setCredit(4);
23         this.coureseService.addCourse(course);;
24     }
25     
26     public void update(Course course) {
27         course.setCurriculum("web");
28         course.setTp("必修");
29         course.setCredit(4);
30         course.setId(8);
31         this.coureseService.updateCourse(course);
32     }
33     
34     public void ex(Course course) {
35         course.setCurriculum("web");
36         this.coureseService.exist(course);
37     }
38 }

7.测试类

 1 public class Test2 {
 2     @Test
 3     public void addTest() {
 4         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
 5         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
 6         Course course=new Course();
 7         courseController.add(course);
 8     }
 9     @Test
10     public void updateTest() {
11         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
12         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
13         Course course=new Course();
14         courseController.update(course);
15     }
16     @Test
17     public void deletetTest() {
18         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
19         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
20         courseController.delete(6);
21         
22     }
23     @Test
24     public void findStudentByIdTest() {
25         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
26         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
27         courseController.findById(4);
28     }
29 
30     @Test
31     public void TestfindAllAccount() {
32         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
33         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
34         courseController.findAll();
35     }
36     @Test
37     public void Testexist() {
38         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
39         CourseController courseController=(CourseController) applicationContext.getBean("courseController");
40         Course course=new Course();
41         courseController.ex(course);
42     }
43 
44 }

我是一个java初学者,第一次写博客有很多不懂,上述有什么不妥的欢迎大家指正。

 

posted @ 2020-03-19 22:02  Carry灭霸  阅读(336)  评论(0)    收藏  举报