spring学习(七)spring整合JDBC

Spring中封装了一个可操作数据库的对象,该对象封装了JDBC技术

使用数据库

 

一、导包(IDEA的maven工程,在pom.xml文件中导入依赖,必须注意依赖,不然会报各种异常)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>demojdbc</groupId>
    <artifactId>demojdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
    </dependencies>
</project>

二、书写与数据库对应的bean类

package dyh.bean;

public class User {
    private String username;
    private String sex;
    private String address;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

三、UserDao接口以及它的实现类

import dyh.bean.User;
import java.util.List;
public interface UserDao {
    void save(User user);
    void delete();
    void update();
    User selectById(Integer id);
    List<User> selectList();
    int getListCount();

}
import dyh.bean.User;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;

public class UserDaoImpl implements UserDao {
    
    //把JdbcTemplate交给spring管理
    private JdbcTemplate jt;
    //JdbcTemplate的set方法,不然不能够依赖注入
    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    //数据库插入
    public void save(User user) {
        String sql = "insert into user(username,sex,address) values(?,?,?)";
        jt.update(sql,user.getUsername(),user.getSex(),user.getAddress());
    }

    public void delete() {

    }

    public void update() {

    }

    public User selectById(Integer id) {
        return null;
    }

    public List<User> selectList() {
        return null;
    }

    public int getListCount() {
        return 0;
    }
}

四、配置文件书写(IDEA的maven工程,applicationContext.xml与jdbc.properties文件,必须放在resources文件夹中)

(1)、jdbc.properties文件

jdbc.jdbcUrl=jdbc:mysql:///school
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=dyhroot

(2)、applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

    <!-- 指定spring读取db.properties配置 -->
    <context:property-placeholder location="classpath:jdbc.properties"  />

    <!-- 1.将连接池放入spring容器 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>
    
    <!-- 2.将JDBCTemplate放入spring容器 -->
    <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <property name="dataSource" ref="dataSource" ></property>
    </bean>

    <!-- 3.将UserDao放入spring容器 -->
    <bean name="userDao" class="dyh.dao.UserDaoImpl" >
         <property name="jt" ref="jdbcTemplate" ></property>
    </bean>
</beans>

五、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Demo {
    @Resource(name = "userDao")
    private UserDao userDao;

    @Test
    public void fun1(){

        User u = new User();
        u.setUsername("hhuan");
        u.setSex("man");
        u.setAddress("American");
        userDao.save(u);
    }
}

测试结果:

数据库添加成功

 

 

posted @ 2018-12-16 15:04  杜大帅-Rebirth  阅读(557)  评论(0编辑  收藏  举报