1。UserServiceImpl必须以@Service或@Component注解才行。
2。自动写入的时候把接口写成实现类了
@Autowired
private UserDaoImpl userDaoImpl;
应该是
@Autowired
private UserDao ;
3.在UserDao 类上加上@Repository注解
==================================================
applicationContext.xml的配置细节
<!--扫包-->
<context:component-scan base-package="dao.impl"></context:component-scan>
<context:component-scan base-package="service.impl"></context:component-scan>
<!--加载根目录下的配置文件dataSource.properties-->
<context:property-placeholder location="classpath:dataSource.properties" />
<!-- 数据源配置 -->
<!--Spring提供的数据源配置-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${driverClassName}"></property> <property name="url"
value="${url}"></property> <property name="username"
value="${dataBaseUserName}"></property> <property name="password"
value="${password}"></property> </bean>
<!-- 2.第三方提供的DataSource配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${dataBaseUserName}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxWait" value="${maxWait}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
</bean>
<!--3.jndi配置 -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jndi/DataSource"></property>
</bean>
PS:使用jndi配置即要在tomcat/conf/context.xml中配置也要在安装目录locationTomcat(自己定义的Tomcat安装目录名)下的/conf/context.xml文件中同时配置相同的信息
<Resource
name="mybatis"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/spring"
username="root"
password="root"
maxActive="1"
/>
=============================
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
PS: dao的实现类继承了JdbcDaoSupport则不需要在IOC容器中配置JdbcTemplate了
1.dataSource-->jdbcTemplate--->dao---->Service--->action
2.dataSource--->dao(dao需要继承JdbcDaoSupport)---->Service--->action
<!-- 事务配置方式一 选择spring自带的jar包 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务配置方式二 -->
<!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="add*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
<tx:method name="query*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* service.impl.*Impl.*(..))" id="pc"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pc"/>
</aop:config> -->