Spring- 5jdbc-template

  //xml配置
    private static final ApplicationContext context = new ClassPathXmlApplicationContext("config/db.xml");
    //jdbc
    private static final JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);

    @Test
    public void test1() throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("config/db.xml");
        HikariConfig config = (HikariConfig) context.getBean("dbConfig");
        HikariDataSource dataSource = context.getBean(HikariDataSource.class);
        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        System.out.println(config);
        System.out.println(dataSource);
        System.out.println(jdbcTemplate);
        //查詢操作
        String sql = "select id from szg8 where `name` = ?";
        Integer id = jdbcTemplate.queryForObject(sql, int.class, "張三");
        System.out.println(id);
    }

    //多條查詢
    @Test
    public void test2() {
        String sql = "select * from szg8 where `id` > ?";
        RowMapper <Szg8> rowMapper = new BeanPropertyRowMapper <>(Szg8.class);
        List <Szg8> szg8s = jdbcTemplate.query(sql, rowMapper, 1);
        System.out.println(szg8s);

    }

    //增
    @Test
    public void add() {
        String sql = "insert INTO szg8 (name) values (?) ";
        int update = jdbcTemplate.update(sql, "陳六");
        System.out.println(update);
    }

    //循環增加
    @Test
    public void forAdd() {
        LocalDateTime startTime = LocalDateTime.now();
        List <Object[]> args = new ArrayList <>(1000);
        String sql = "insert INTO szg8 (name) values (?) ";
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            args.add(new Object[]{i});
        }
//        for (int i = 0; i < 1000; i++) {
//            add();
//        }

        int[] ints = jdbcTemplate.batchUpdate(sql, args);
        System.out.println(ints.length);
        LocalDateTime endTime = LocalDateTime.now();
        System.out.println(Duration.between(startTime, endTime).getSeconds());
    }

    //批量改動
    @Test
    public void batchUpdate() {
        LocalDateTime startTime = LocalDateTime.now();
        List <Object[]> args = new ArrayList <>(1000);
        String sql = "update szg8 set `name`= '任傑' where `id` = ?";
        for (int i = 1; i < Short.MAX_VALUE + 1; i++) {
            args.add(new Object[]{i});
        }
        int[] ints = jdbcTemplate.batchUpdate(sql, args);
        System.out.println(ints.length);
        LocalDateTime endTime = LocalDateTime.now();
        System.out.println(Duration.between(startTime, endTime).getSeconds());
    }

    //刪除
    @Test
    public void delete() {
        String sql = "delete from szg8 where `name` = ?";
        int delete = jdbcTemplate.update(sql, "陳六");
        System.out.println(delete);
    }

    //改
    @Test
    public void update() {
        String sql = "update szg8 set `name`= ? where `id` = ?";
        int update = jdbcTemplate.update(sql, "任傑", 2);
        System.out.println(update);
    }

 //具名參數
    @Test
    public void test4() {
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = context.getBean(NamedParameterJdbcTemplate.class);
        String sql = "select * from szg8 where id=:id";
        Map <String, Object> paraMap = new HashMap <>();
        paraMap.put("id", 23);
        RowMapper <Szg8> rowMapper = new BeanPropertyRowMapper <>(Szg8.class);
        List <Szg8> query = namedParameterJdbcTemplate.query(sql, paraMap, rowMapper);
        System.out.println(query);


    }
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("username", "root");
        properties.setProperty("password", "root");
        properties.setProperty("jdbcUrl", "jdbc:mysql://localhost:3306/szg8?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
        properties.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
        HikariConfig hikariConfig = new HikariConfig(properties);
        HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);

    }

dbconfig.property文件內容


user=root
password=root
jdbcUrl=jdbc:mysql://localhost:3306/szg8?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&rewriteBatchedStatements=true&characterEncoding=utf-8
driverClassName=com.mysql.cj.jdbc.Driver

db.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:P="http://www.springframework.org/schema/c"
       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">

    <context:property-placeholder location="config/dbconfig.property"/>
    <!--    hikar的config配置-->
    <bean id="dbConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
        <property name="jdbcUrl" value="${jdbcUrl}"/>
        <property name="driverClassName" value="${driverClassName}"/>
    </bean>
    <!--    數據源-->
    <bean id="db" class="com.zaxxer.hikari.HikariDataSource">
        <constructor-arg name="configuration" ref="dbConfig"/>
    </bean>
    <!--    spring的jdbc構造-->
    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="db"/>
    </bean>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章