1.1 Project directory
1.2 Code example
Customer entity class
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;
}
Result set mapping
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;
}
}
Persistence layer dao
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.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* 客户dao实现类
*/
public class CustomerDaoImpl implements CustomerDao {
// 定义JdbcTemplate
private JdbcTemplate jdbcTemplate;
// 设置方法
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.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;
}
}
Business layer Service
CustomerService
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 java.util.List;
/**
* 客户service实现类
*/
public class CustomerServiceImpl implements CustomerService {
// 定义客户dao
private CustomerDao customerDao;
// 设置方法
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
/**
* 查询全部客户的列表
*/
@Override
public List<Customer> findAllCustomers() {
return customerDao.findAllCustomers();
}
}
Configure 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置客户dao-->
<bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl">
<!--注入jdbcTemplate-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置客户service-->
<bean id="customerService" class="cn.guardwhy.service.impl.CustomerServiceImpl">
<!--注入客户dao-->
<property name="customerDao" ref="customerDao"></property>
</bean>
<!--配置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>
Presentation layer (Controller)
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);
}
}
}