Dao接口不能正确初始化,下面是相关类和配置文件。
javaBean
package com.mars.dal.bean; import java.math.BigDecimal; public class Recharge { private long rechargeID; private BigDecimal amount; private long time; private String dateTime; private int quarter; public long getRechargeID() { return rechargeID; } public void setRechargeID(long rechargeID) { this.rechargeID = rechargeID; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } public String getDateTime() { return dateTime; } public void setDateTime(String dateTime) { this.dateTime = dateTime; } public int getQuarter() { return quarter; } public void setQuarter(int quarter) { this.quarter = quarter; } }
Mybatis Mapper
package com.mars.dal.mapper; import java.util.List; import com.mars.dal.bean.Recharge; public interface RechargeMapper { public int addCount(Recharge count); public List<Recharge> getCountPerMonthByYear(String year); public List<Recharge> getCountPerQuarterByYear(String year); public List<Recharge> getCountYearByYear(String year); }
Mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- properties resource="com/curender/dal/dbconfig/sqlconfig.properties" /--> <typeAliases> <typeAlias alias="Recharge" type="com.mars.dal.bean.Recharge" /> </typeAliases> <mappers> <mapper resource="com/mars/dal/mapper/RechargeMapper.xml" /> </mappers> </configuration>
RehcargeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mars.dal.mapper.RechargeMapper"> <resultMap id="rechargeMap" type="com.mars.dal.bean.Recharge"> <id property="countID" column="count_id" /> <result property="amount" column="amount" /> <result property="time" column="time" /> <result property="dateTime" column="date_time" /> <result property="quarter" column="quarter" /> </resultMap> <insert id="addCount" statementType="PREPARED" parameterType="Recharge"> insert into recharge(amount,time) values(#{amount},#{time}) </insert> <select id="getCountPerMonthByYear" statementType="PREPARED" parameterType="HashMap" resultMap="rechargeMap"> select FROM_UNIXTIME(left(time,10),'%Y-%m') as date_time,sum(amount) as amount from recharge where FROM_UNIXTIME(left(time,10),'%Y')=#{year} group by FROM_UNIXTIME(left(time,10),'%Y%m') </select> <select id="getCountPerQuarterByYear" statementType="PREPARED" parameterType="HashMap" resultMap="rechargeMap"> select FLOOR((FROM_UNIXTIME(left(time,10),'%m' )+2)/3) as quarter,sum(amount) as amount from recharge where FROM_UNIXTIME(left(time,10),'%Y')=#{year} group by FLOOR((FROM_UNIXTIME(left(time,10), '%m')+2)/3); </select> <select id="getCountYearByYear" statementType="PREPARED" parameterType="HashMap" resultMap="rechargeMap"> select FROM_UNIXTIME(left(time,10),'%Y') as date_time,sum(amount) as amount from recharge where FROM_UNIXTIME(left(time,10),'%Y')=#{year} group by FROM_UNIXTIME(left(time,10),'%Y'); </select> </mapper>
RechargeServiceImpl.java
package com.mars.dal.service.impl; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.mars.dal.bean.Recharge; import com.mars.dal.mapper.RechargeMapper; import com.mars.dal.service.RechargeService; public class RechargeServiceImpl implements RechargeService { private RechargeMapper rechargeMapper; public RechargeMapper getRechargeMapper() { return rechargeMapper; } public void setRechargeMapper(RechargeMapper rechargeMapper) { this.rechargeMapper = rechargeMapper; } public void getRechargeDatas(String year) { System.out.println("ddddddddddddd"); System.out.println(this.rechargeMapper); } }
Spring配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <context:component-scan base-package="com.mars.dal" /> <context:annotation-config /> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1/count"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <!-- 声明式事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:com/mars/dal/mapper/Configuration.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="rechargeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mars.dal.mapper.RechargeMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="rechargeService" class="com.mars.dal.service.impl.RechargeServiceImpl"> <property name="rechargeMapper" ref="rechargeMapper"></property> </bean> </beans>
启动服务后service中的this.rechangeMapper始终为null.刚上手学Spring,请问哪边出错了!
<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" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 这部分是Spring配置文件头,提问的时候判断成广告不给发