33-Spring-JDBC操作数据库

1.db.properties数据库配置

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配置文件

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 1.导入数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 2.开启@Autowired注解 -->
    <context:annotation-config/>
    <!-- 3.扫描Spring Bean组件 -->
    <context:component-scan base-package="com.gzcgxt.erp"/>
    <!-- 4.jdbcTemplate数据库操作组件 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource"/>
    </bean>
    <!-- 5.Druid数据源配置 -->
    <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对象

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.测试代码

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();
    }
}

猜你喜欢

转载自www.cnblogs.com/hua900822/p/11303588.html