Spring JDBC framework

JDBC framework overview

When using an ordinary JDBC database, it will be troublesome to write unnecessary code to handle exceptions, open and close database connections, etc. But the Spring JDBC framework is responsible for all the low-level details, from opening the connection at the beginning, preparing and executing SQL statements, handling exceptions, handling transactions, and closing the connection at the end.

Insert picture description here
Examples:
1.1 File directory
Insert picture description here1.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
Insert picture description hereExperimental results of database tables
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/s001125/article/details/114644276