项目结构
从入口TestCustomerService开始
public class TestCustomerService {
@Test
public void testDemo() {
ApplicationContext context =
new ClassPathXmlApplicationContext("Spring-JdbcTemplate.xml");
CustomerService service = (CustomerService) context.getBean("CustomerService");
// service.insert(new Customer(5,"fuck",20));
Customer findCustomer = service.findByCustomerId(4);
System.out.println(findCustomer.getId());
System.out.println(findCustomer.getName());
System.out.println(findCustomer.getAge());
}
}
Spring的核心配置文件
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入属性值 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" ></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring_jdbc"></property>
<property name="user" value="root"></property>
<property name="password" value="shengri"></property>
</bean>
<!-- 创建Jdbctemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把dataSource传递到jdbctemplate中 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="CustomerDao" class="dao.JdbcTemplateCustomerDao">
<!-- 注入jdbctemplate对象 -->
<property name="jdbcTemplate" ref="jdbcTemplate">
</property>
</bean>
<bean id="CustomerService" class="Service.CustomerService">
<property name="CustomerDao" ref="CustomerDao">
</property>
</bean>
</beans>
比较简单,就是各种属性的注入,c3p0连接池需要学习,另外注解方式!?需要实现
CustomerService业务层
public class CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void insert(Customer customer) {
customerDao.insert(customer);
System.out.println("Insert done");
}
public Customer findByCustomerId(int custId) {
return customerDao.findByCustomerId(custId);
}
}
这里不涉及对于Dao层的代码,全是核心代码,即插入,寻找
CustomerDao层
- 接口
public interface CustomerDao {
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
- 实现
public class JdbcTemplateCustomerDao implements CustomerDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void insert(Customer customer) {
String sql = "insert into CUSTOMER (CUST_ID, NAME, AGE) values(?,?,?)";
jdbcTemplate.update(sql,customer.getId(),customer.getName(),customer.getAge());
}
@Override
public Customer findByCustomerId(int custId) {
String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
Customer customer = jdbcTemplate.queryForObject(sql, new MyRowMapper(), custId);
return customer;
}
}
从这里可以看出jdbctemplate和JDBC的表面上的区别,即对于sql的封装预编译有所不同,Jdbctemplate更加简洁,此处还需深究
工具类
import java.sql.ResultSet;
import java.sql.SQLException;
public class MyRowMapper implements RowMapper<Customer> {
@Override
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
int CustId = rs.getInt("CUST_ID");
String Name= rs.getString("NAME");
int Age = rs.getInt("AGE");
Customer customer = new Customer(CustId,Name,Age);
return customer;
}
}
关于这个工具类也需要深究,还有结果集的问题
印象中hibernate不需要这一步,即对于对象的封装这一步也用hibernate帮我们完成,这一步就是hibernate 持久层(即hibernate会探知对象的变化)???这部分也需要在思考~
问题:部署在JUNIT是可以的
但是部署在TOMCAT上面就会报错
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:spring_jdbctemplate’ did not find a matching property.
查了许多资料,觉得这篇文章比较靠谱,意思就是
tomcat服务器启动,当其加载一个web应用时,会先去eclipse的workspace下的Servers工程下的server.xml文件寻找元素,再查找conf\Catalina\localhost下的.xml文件的元素,那么就会因为出现重复的Context元素而报警告。但是他的解决方法依旧没有解决这个问题。未完待续