33-Spring-JDBC database operation

1.db.properties database configuration

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=root

 

2.applicationContext.xml profile

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/jee HTTP: // the WWW .springframework.org / Schema / JEE / the Spring-jee.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
            HTTP: // http://www.springframework.org/schema/tx/spring-tx.xsd www.springframework.org/schema/tx "  <!--> 
    
    ! <- 1. import database configuration file -> 
    < context: Property-placeholder LOCATION = "the CLASSPATH: db.properties" /> 
    2. open @Autowired comment -> < context: annotation-config /> ! <-
    
     3. Spring Bean scanning assembly -> 
    < context: Scan-Component Base-Package = "com.gzcgxt.erp" /> 
    < ! - 4.jdbcTemplate database operations components -> 
    < the bean ID = "the jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > 
        < constructor-Arg REF = "the dataSource" /> 
    </ the bean > 
    <! - 5.Druid data source configuration -> 
    < the bean ID = "dataSource" class = " com.alibaba.druid.pool.DruidDataSource " > 
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans> 

3.User objects

package com.gzcgxt.erp.domain;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String    name;
    private Integer age;
    private Integer flag;
}

4.UserDAO

package com.gzcgxt.erp.dao;

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

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.gzcgxt.erp.domain.User;

@Repository
public class UserDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save() {
        jdbcTemplate.update("INSERT INTO `user` SET name='dddd',age=12,flag=1");
    }

    public void delete() {
        
        List<User> list = jdbcTemplate.query("select * from user order by id asc limit 10",
                new RowMapper<User>() {
                    @Override
                    public User mapRow(ResultSet rs, int rowNum)
                            throws SQLException {
                        User u = new User();
                        u.setId(rs.getInt("id"));
                        u.setName(rs.getString("name"));
                        u.setAge(rs.getInt("age"));
                        u.setFlag(rs.getInt("flag"));
                        return u;
                    }
                });

        User u=list.size()>1 ? list.get(0) : null;
        
        String cmdText="DELETE FROM `user` WHERE id="+u.getId();
        
        jdbcTemplate.execute(cmdText);
        System.out.println(u+"删除成功");
    }
}

6. Test Code

package com.gzcgxt.erp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.gzcgxt.erp.dao.UserDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class App {
    
    @Autowired
    private UserDAO dao;
    
    @Test
    public void testApp() throws Exception {
        dao.delete();
    }
}

 

Guess you like

Origin www.cnblogs.com/hua900822/p/11303588.html