11- IOC案例实现
11.1 项目目录
11.2客户实体类
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;
}
11.3 结果集映射
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;
}
}
11.4 客户dao实现类
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;
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Customer> findAllCustomers() {
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;
}
}
11.5 客户service实现类
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("customerService")
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public List<Customer> findAllCustomers() {
return customerDao.findAllCustomers();
}
}
11.6 配置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">
<context:component-scan base-package="cn.guardwhy"></context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</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://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>
11.7客户表现层(Controller)
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) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
CustomerService customerService = (CustomerService) context.getBean("customerService");
List<Customer> list = customerService.findAllCustomers();
for(Customer customer : list){
System.out.println(customer);
}
}
}