Spring整合JdbcTemplate实现增删改查--基于xml配置

33 篇文章 0 订阅
19 篇文章 0 订阅

总结:该项目没有使用mybatis框架,仅仅使用了spring,比较容易理解

可以改进一下,使用springmvc和mybatis。将其改进为web项目

 

1,导入需要引入的依赖:Spring ioc + JdbcTemplate+druid+ mysql。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>com.day02Jdbc</groupId>
    <artifactId>SpringJdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
 
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- jdbc模板 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <!--druid数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
    </dependencies>
 
</project>

2、创建业务层接口及实现类

bean层

实体类:Customer.java

package com.day02Jdbc.domain;
 
import java.io.Serializable;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:00 2018/11/8
 */
public class Customer implements Serializable {
    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_address;
    private String cust_phone;
 
    public Long getCust_id() {
        return cust_id;
    }
 
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
 
    public String getCust_name() {
        return cust_name;
    }
 
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
 
    public String getCust_source() {
        return cust_source;
    }
 
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
 
    public String getCust_industry() {
        return cust_industry;
    }
 
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
 
    public String getCust_level() {
        return cust_level;
    }
 
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
 
    public String getCust_address() {
        return cust_address;
    }
 
    public void setCust_address(String cust_address) {
        this.cust_address = cust_address;
    }
 
    public String getCust_phone() {
        return cust_phone;
    }
 
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
 
    @Override
    public String toString() {
        return "Customer{" +
                "cust_id=" + cust_id +
                ", cust_name='" + cust_name + '\'' +
                ", cust_source='" + cust_source + '\'' +
                ", cust_industry='" + cust_industry + '\'' +
                ", cust_level='" + cust_level + '\'' +
                ", cust_address='" + cust_address + '\'' +
                ", cust_phone='" + cust_phone + '\'' +
                '}';
    }
}

 service 层

创建CustomerService.java接口和CustomerServiceImpl.java实现类

package com.day02Jdbc.service;
 
import com.day02Jdbc.domain.Customer;
 
import java.util.List;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:01 2018/11/8
 */
public interface CustomerService {
    /**
     * 业务层:查询所有客户
     * @return
     */
    List<Customer> findAllCustomer();
}
//impl

package com.day02Jdbc.service.Impl;
 
import com.day02Jdbc.dao.CustomerDao;
import com.day02Jdbc.domain.Customer;
import com.day02Jdbc.service.CustomerService;
 
import java.util.List;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:06 2018/11/8
 */
public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao ;
 
    public void setCustomerDao(CustomerDao customerDao){
        this.customerDao = customerDao;
    }
 
    @Override
    public List<Customer> findAllCustomer() {
        List<Customer> list = customerDao.findAll();
        return list;
 
    }
}

3、创建dao层接口及实现类

在这一层可以使用mybatis自动生成dao的实现类,比如CustomerDaoImpl

CustomerDao.java接口

package com.day02Jdbc.dao;
 
import com.day02Jdbc.domain.Customer;
 
import java.util.List;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:08 2018/11/8
 */
public interface CustomerDao {
    List<Customer> findAll();
}
//impl
package com.day02Jdbc.dao.Impl;
 
import com.day02Jdbc.dao.CustomerDao;
import com.day02Jdbc.domain.Customer;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.util.List;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:09 2018/11/8
 */
public class CustomerDaoImpl implements CustomerDao {
    private JdbcTemplate jdbcTemplate;//不需要实例化,通过spring依赖注入进来,采用xml配置,需要提供set方法
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    @Override
    public List<Customer> findAll() {
        List<Customer> list = jdbcTemplate.query("select * from cst_customer",new BeanPropertyRowMapper<Customer>(Customer.class));
        return list;
    }
}

4、编写Spring配置文件

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:context="http://www.springframework.org/schema/context"
       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">
 
    <bean id="customerService" class="com.day02Jdbc.service.Impl.CustomerServiceImpl">
        <!--给业务层注入dao-->
        <!--对象属性用ref指定需要注入的bean的id-->
        <property name="customerDao" ref="customerDao"></property>
    </bean>
 
    <bean id="customerDao" class="com.day02Jdbc.dao.Impl.CustomerDaoImpl">
        <!--给dao层注入jdbcTemplate-->
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--给jdbcTemplate层注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置数据源 用的是德鲁伊 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>
        <property name="username" value="root"></property>
        <property name="password" value="sswqzx"></property>
    </bean>
</beans>

 5、测试类

package com.day02Jdbc.test;
 
import com.day02Jdbc.domain.Customer;
import com.day02Jdbc.service.CustomerService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.util.List;
 
/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 17:15 2018/11/8
 */
public class TestJdbc {
    @Test
    public void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerService customerService = (CustomerService) ac.getBean("customerService");
        List<Customer> list = customerService.findAllCustomer();
        for (Customer customer : list) {
            System.out.println("customer = " + customer);
        }
    }
}

 

这一个案例还是加深我对ssm的理解,对jdbcTemplate的使用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值