Spring-jdbc template

Spring—jdbc

Spring-jdbc jdbc tool is a class of jdbc a simple package, similar to the dbutils. Relative to mybatis, hibernate these frameworks may not be as strong function, but the database is simple CRUD operations or mention.

Create a maven project, dependent on the introduction of

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>

Here Druid introduces a database connection pool, so will use it as a connection pool, of course, also possible to use spring's internal pool, the pool is connected Druid performance is higher.

Create a jdbc configuration class

JdbcConfig.java


@Configuration
public class JdbcConfig {

    @Bean
    DataSource dataSource(){
    	//使用druid数据库连接池
        DruidDataSource build = new DruidDataSource();
        //使用spring自带的数据库连接池
        //DriverManagerDataSource build = new DriverManagerDataSource();
        build.setUsername("root");
        build.setPassword("root");
        build.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC");
        build.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return build;
    }
    @Bean
    JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }
}

Took over for testing:


public class JdbcTest {
    private JdbcTemplate jdbcTemplate;
    @BeforeEach
    public void test(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JdbcConfig.class);
        jdbcTemplate= (JdbcTemplate) context.getBean("jdbcTemplate");
    }

    @Test
    //添加
    public void test1(){
        jdbcTemplate.update("insert  into user (username ,address) value (?,?)","zs","fuzhou");
    }

	//更新
    @Test
    public void update() {
        jdbcTemplate.update("update user set username=? where id=?", "zs", 1);

    }
	//删除
    @Test
    public void delete() {
        jdbcTemplate.update("delete from user where id=?", 1);
    }
    
    //查询所有
    @Test
    public void select4() {
        List<User> list = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<User>(User.class));
        System.out.println(list);
    }
	//查询单个
    @Test
    public void select() {
        User user = jdbcTemplate.queryForObject("select * from user where id=?", new BeanPropertyRowMapper<User>(User.class), 2);
        System.out.println(user);
    }

}

Published 25 original articles · won praise 0 · Views 286

Guess you like

Origin blog.csdn.net/qq_42219004/article/details/105198236