<?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"> 1.配置数据源 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://localhost:3306/crud"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> 2.配置JdbcTemplate,并注入数据源 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> 3.配置dao,并注入JdbcTemplate <bean id="userinfoDao" class="cn.zzsxt.crud.dao.impl.UserinfoDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> 4.配置service,并注入dao <bean id="userinfoService" class="cn.zzsxt.crud.service.impl.UserinfoServiceImpl"> <property name="userinfoDao" ref="userinfoDao"></property> </bean> </beans>
====================================================================================================
1.IOC的综合实例 (斧子问题)---->面试题 2.Spring容器中Bean声明周期 scope="singleton|prototype" a.单例,当容器初始化时 ApplicationContext ac = new ClassPathXmlApplicationContext();创建对象,并且针对所有的用户请求只创建一个对象 b.原型,初始化容器时并不会创建对象,当调用getBean方法时针对每一次用户的请求创建一个新的对象 3.使用静态工程方法和实例工程方法创建对象 <bean id="" class="" factory-method=""/> 4.自动装配(autowire) <bean id="" class="" autowire="byName|byType|constructor">
5.使用spring中的JdbcTemplate实现CRUD操作 JdbcTemplate 查询:query()/queryForObject(..)/queryForList(..)---->RowMapper的实例 增/删/改:update()
a.配置数据源(DruidDataSource) b.配置JdbcTemplate,并注入数据源(dataSource) c.配置DAO,并注入JdbcTemplate d.配置Service,并注入DAO