美文网首页
Spring-Jdbc注解版

Spring-Jdbc注解版

作者: 紫荆秋雪_文 | 来源:发表于2019-10-06 23:19 被阅读0次

数据库配置

因为使用的是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);
        }
    }
}

相关文章

  • Spring-Jdbc注解版

    数据库配置 因为使用的是jdbc注解版,但是无法在相应的dataSource贴标签,所以依然需要XML来配置 do...

  • spring-jdbc

    spring-jdbc 导包:spring-tx spring-jdbc 一、xml配置方式 1、配置xml,...

  • spring-jdbc 的实现原理

    前言 本篇文章将回答以下几个问题 spring-jdbc 的出现是为了解决什么问题 spring-jdbc 如何解...

  • 简单的Java注解机制

    一、什么是注解 注解也叫元数据,例如我们常见的@Override和@Deprecated,注解是JDK1.5版本开...

  • Java面试题之Spring(二)

    Spring常用的注解? spring 在2.5版本以后开始支持注解的方式来配置依赖注入,可以用注解的方式...

  • 注解机制及其原理

    什么是注解 注解也叫元数据,例如我们常见的@Override和@Deprecated,注解是JDK1.5版本开始引...

  • Java注解总结(一)

    Java注解总结(一) 什么是注解 Java注解又称Java标注,是Java语言5.0版本开始支持加入源代码的特殊...

  • Hibernate4复习之Hibernate4 CRUD体验

    今日目录: 1、HibernateUtil封装 2、XML版CRUD实现 3、注解版 CRUD 实现 4、...

  • Spring注解—@AliasFor介绍

    一、@AliasFor注解概览 @AliasFor是Spring Framework中的一个注解,从4.2版本开始...

  • Spring扩展------基于`@Conditional`注解

     @Conditional注解是spring在4.0版本提供的一个注解,作用是贴有这个注解或间接贴有的bean在满...

网友评论

      本文标题:Spring-Jdbc注解版

      本文链接:https://www.haomeiwen.com/subject/wttmuctx.html