Simplify JDBC operations with Spring JDBCTemplate

Friends who have been exposed to JAVA WEB development must know the Hibernate framework. Although it does not deny its power, I have never felt it, and always feel that it is not flexible enough and too bloated.

Today, let's talk about an auxiliary class (JDBC Template) about JDBC in Spring, which encapsulates the operation of JDBC and is very convenient to use.

package com.yiibai;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class JDBCTemplate {
@Test
public void demo(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///ibatis");
dataSource.setUsername("root");
dataSource.setPassword("123");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table temp(id int primary key,name varchar(32))");
}
}

mysql> show tables;
+------------------+
| Tables_in_ibatis |
+------------------+
| employee |
| ibatis |
| temp |
| users |
+------------------+
4 rows in set (0.01 sec)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893677&siteId=291194637