Caso Spring-IOC (integración de anotaciones xml)

1.1 Directorio de proyectos

1.2 Ejemplo de código

Clase de entidad de cliente

package cn.guardwhy.domain;
/**
 * 客户实体类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
    
    
    // 成员变量
    private Long custId;
    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;
    private String custAddress;
    private String custPhone;
}

Mapeo del conjunto de resultados

package cn.guardwhy.resources;

import cn.guardwhy.domain.Customer;
import org.springframework.jdbc.core.RowMapper;

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


public class CustomerRowMapper implements RowMapper<Customer> {
    
    
    /**
     * 结果集映射的方法:
     *      结果集中的每一行记录,都会调用一次该方法
     */
    public Customer mapRow(ResultSet rs, int index) throws SQLException{
    
    
        // 创建客户对象
        Customer customer = new Customer();
        customer.setCustId(rs.getLong("cust_id"));
        customer.setCustName(rs.getString("cust_name"));
        customer.setCustSource(rs.getString("cust_source"));
        customer.setCustIndustry(rs.getString("cust_industry"));
        customer.setCustLevel(rs.getString("cust_level"));
        customer.setCustAddress(rs.getString("cust_address"));
        customer.setCustPhone(rs.getString("cust_phone"));
        return customer;
    }
}

Dao de la capa de persistencia

CustomerDao

package cn.guardwhy.dao;

import cn.guardwhy.domain.Customer;

import java.util.List;

public interface CustomerDao {
    
    
    /**
     * 查询全部客户
     */
    List<Customer> findAllCustomers();
}

CustomerDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.domain.Customer;
import cn.guardwhy.resources.CustomerRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * 客户dao实现类
 */
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
    
    
    // 定义JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * 查询全部客户列表
     */
    @Override
    public List<Customer> findAllCustomers() {
    
    
        // 定义sql
        String sql = "select t.cust_id, t.cust_name, t.cust_source, "+
                "t.cust_industry, t.cust_level, t.cust_address, t.cust_phone "+
                "from cst_customer t";

        // 执行查询操作
        List<Customer> list = jdbcTemplate.query(sql, new CustomerRowMapper());
        return list;
    }
}

Servicio de capa empresarial

Servicio al Cliente

package cn.guardwhy.service;

import cn.guardwhy.domain.Customer;

import java.util.List;
/**
 * 客户service接口
 */
public interface CustomerService {
    
    
    /**
     * 查询全部客户列表
     */
    List<Customer> findAllCustomers();
}

CustomerServiceImpl

package cn.guardwhy.service.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.domain.Customer;
import cn.guardwhy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 客户service实现类
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
    
    
    // 定义客户dao
    @Autowired
    private CustomerDao customerDao;

    /**
     * 查询全部客户的列表
     */
    @Override
    public List<Customer> findAllCustomers() {
    
    
        return customerDao.findAllCustomers();
    }
}

Configurar bean.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">

    <!--配置包扫描dao/service
        第一步:导入context名称空间和约束
        第二步:通过<context:component-scan>标签配置包扫描,spring框架在
        初始化IOC容器的时候,会扫描指定的包和它的子包
    -->
    <context:component-scan base-package="cn.guardwhy"></context:component-scan>

    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置数据源对象(druid)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入连接数据库-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>

        <!--数据库连接池常用属性-->
        <!--初始化连接数量-->
        <property name="initialSize" value="6" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="3" />
        <!-- 最大并发连接数(最大连接池数量) -->
        <property name="maxActive" value="50" />
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
    </bean>
</beans>

Capa de presentación (controlador)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.domain.Customer;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * 客户表现层
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        // 1.加载spring配置文件,创建spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
        // 2.获取客户service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.查询全部客户列表数据
        List<Customer> list = customerService.findAllCustomers();
        // 4.遍历操作
        for(Customer customer : list){
    
    
            System.out.println(customer);
        }
    }
}

1.3 Resultados de la ejecución

Supongo que te gusta

Origin blog.csdn.net/hxy1625309592/article/details/115008939
Recomendado
Clasificación