使用spring-jdbc完成最简单的数据库操作

1 表关系
使用spring-jdbc完成最简单的数据库操作_第1张图片
使用spring-jdbc完成最简单的数据库操作_第2张图片
2 使用Maven方式创建工程
pom.xml文件JAR包的坐标

<?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>com.zxf</groupId>
    <artifactId>Spring_tx2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!--通过Maven传递依赖,导入依赖jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- Spring test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <!-- aop联盟 -->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

3 Spring的核心配置文件
applicationContext_jdbc.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!--配置连接池 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.0.148:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="abc123"/>
    </bean>
<!-- JDBC模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

4 编写测试类;通过注解方式导入spring配置文件

package com.test;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext_jdbc.xml")
public class MyTest1 {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void test1(){
        for(int i=0;i<10;i++) {
            jdbcTemplate.update("insert into t1(tname) values('可以写入数据了')");
        }
    }
  @Test
    public void test2(){
        jdbcTemplate.update("update t1 set tname='我玩你看' where tid=5");
    }
    @Test
    public void test3(){
        jdbcTemplate.update("delete from t1 where tid=6 ");
    }
    @Test
    public void test4(){
        T1 t1 = jdbcTemplate.queryForObject("select * from t1 where tid=?", new BeanMapper(),10);
        System.out.println(t1.getTname());
    }
    @Test
    public void test5(){
        List query = jdbcTemplate.query("select * from t1 ", new BeanMapper());
        for(T1 t1:query){
            System.out.println(t1.getTname());
        }
    }
}


package com.zxf.pojo;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class BeanMapper implements RowMapper<T1> {
    public T1 mapRow(ResultSet resultSet, int i) throws SQLException {
        T1 t=new T1();
        t.setTid(resultSet.getInt("tid"));
        t.setTname(resultSet.getString("tname"));
        return t;
    }
}

你可能感兴趣的:(Spring)