Spring JDBC框架

JDBC 框架概述

在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等。但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。

在这里插入图片描述
实例:
1.1文件目录
在这里插入图片描述1.2、CityDao

package sc.dao;

import sc.domain.City;

import java.util.List;

public interface CityDao {
    
    
    public List<City> findAll();

//    public void update(String name,int id);
    public void update(City city);
}

1.3、CityDaoImpl

package sc.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import sc.dao.CityDao;
import sc.domain.City;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CityDaoImpl implements CityDao{
    
    

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
    
    
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    
    
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<City> findAll() {
    
    
        return jdbcTemplate.query("select * from city", new RowMapper<City>() {
    
    
            @Nullable
            @Override
            public City mapRow(ResultSet rs, int num) throws SQLException {
    
    
                City city = new City();
                city.setCid(rs.getInt(1));
                city.setCname(rs.getString(2));
                city.setPid(rs.getInt(3));
                return city;
            }
        });
    }

    @Override
//    public void update(String name,int id) {
    
    
//        jdbcTemplate.update("update city set cname=? where cid=?",name,id);
//    }
    public void update(City city) {
    
    
        jdbcTemplate.update("update city set cname=? where cid=?",city.getCname(),city.getCid());
    }
}

1.4、City

package sc.domain;

public class City {
    
    
    private int cid;
    private String cname;
    private int pid;

    public City() {
    
    
    }

    public City(int cid, String cname, int pid) {
    
    

        this.cid = cid;
        this.cname = cname;
        this.pid = pid;
    }

    public int getCid() {
    
    

        return cid;
    }

    public void setCid(int cid) {
    
    
        this.cid = cid;
    }

    public String getCname() {
    
    
        return cname;
    }

    public void setCname(String cname) {
    
    
        this.cname = cname;
    }

    public int getPid() {
    
    
        return pid;
    }

    public void setPid(int pid) {
    
    
        this.pid = pid;
    }
}

1.5、CityService

package sc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sc.dao.CityDao;
import sc.domain.City;

import java.util.List;

@Service
public class CityService {
    
    

    @Autowired
    private CityDao cityDao;

    public CityDao getCityDao() {
    
    
        return cityDao;
    }

    public void setCityDao(CityDao cityDao) {
    
    
        this.cityDao = cityDao;
    }
    public List<City> findAll(){
    
    
        return cityDao.findAll();
    }

//    public void update(String name,int id){
    
    
//        cityDao.update(name, id);
//    }
    public void update(City city){
    
    
        cityDao.update(city);
    }
}

1.6、Test

package sc.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sc.domain.City;
import sc.service.CityService;

import java.util.List;
import java.util.Scanner;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        CityService cs = ac.getBean("cityService", CityService.class);
        List<City> lists = cs.findAll();
        for (City list : lists) {
    
    
            System.out.println(list.getCname()+"----"+list.getCid());
        }

        System.out.println("请输入需要修改的内容:");
        Scanner sc = new Scanner(System.in);
        String next = sc.next();
        System.out.println("请输入需要修改的编号:");
        int id = sc.nextInt();

        City city = new City();
        city.setCname(next);
        city.setCid(id);
        cs.update(city);



//        System.out.println("请输入需要修改的内容:");
//        Scanner sc = new Scanner(System.in);
//        String next = sc.next();
//        System.out.println("请输入需要修改的编号:");
//
//        int id = sc.nextInt();
//        cs.update(next,id);

    }
}

1.7、spring.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">

    <!--自动扫描上下文-->
    <context:component-scan base-package="sc.*"></context:component-scan>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <!--注意大部分的接口都是3306-->
        <property name="url" value="jdbc:mysql://localhost:3308/db_shopping?characterEncoding = utf8&amp;&amp;useSSL=false&amp;serverTimezone=UTC"></property>
        <!--数据库账号-->
        <property name="username" value="root"></property>
        <!--数据库密码-->
        <property name="password" value="root"></property>
    </bean>

    <bean id="jdbcTemlate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="services" expression="execution(* sc.dao.impl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txadvice" pointcut-ref="services"></aop:advisor>
    </aop:config>
</beans>

1.8数据库表
在这里插入图片描述实验结果
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/s001125/article/details/114644276