急急急有没有人能告诉我代码哪错了,不要让我找,对java不熟练
环境配置applicationContext.xml
<!--主要是针对业务层和持久层-->
<!--1.注解扫描-->
<context:component-scan base-package="com.xja.cvs.service.impl"/>
<!--2.引入属性文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--3.数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driverClassName}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
</bean>
<!--4.SqlSessionFactoryBean -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.xja.cvs.pojo"/>
<property name="mapperLocations" value="classpath:com.xja.cvs.mapper/*.xml"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--5.配置mapper扫描bean-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"/>
<property name="basePackage" value="com.xja.ssm.mapper"/>
</bean>
<!--6.事务管理-->
<!--6.1配置事务Bean-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--6.2配置事务方法(业务层)-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"></tx:method>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"></tx:method>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception"></tx:method>
</tx:attributes>
</tx:advice>
<!--6.3配置事务切面-->
<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* com.xja.cvs.service.impl.*.*(..)) "/>
<aop:advisor pointcut-ref="myPointcut" advice-ref="advice"/>
</aop:config>
</beans>
db.properties
# ?????(???+???)
mysql.driverClassName=com.mysql.cj.jdbc.Driver
# ?????URL??
mysql.url=jdbc:mysql://localhost:3306/test_db?serverTimezone=Asia/Shanghai&useSSL=false
# ?? / ???
mysql.username=root
# ??
mysql.password=123456
# ?????(???+???)
#driverClassName = oracle.jdbc.OracleDriver
# ?????URL??
#url = jdbc:oracle:thin:@localhost:1521:XE
# ?? / ???
#username =scott
# ??
#password =123456
#??????????0?????
maxActive=50
#??????????0?????
maxIdle=20
#??????????-1?????
maxWait=60000
log4j.properties
log4j.rootLogger=info,stdout,D,E
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
log4j.appender.D=org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.Append=true
log4j.appender.D.Threshold=INFO
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[ %t:%r ] - [ %p ]%m%n
log4j.appender.E=org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.Append=true
log4j.appender.E.Threshold=ERROR
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[ %t:%r ] - [ %p ]%m%n
mybatis-config.xml
<configuration>
<!--设置打印日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--关闭一级缓存 localCacheScope-->
<setting name="localCacheScope" value="STATEMENT"/>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--增加分页插件 pageHelper-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
spring-mvc.xml
<!--1.注解扫描-->
<context:component-scan base-package="com.xja.cvs.web.controller"/>
<!--2.视图解析器-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>-->
<!--3.注解驱动-->
<mvc:annotation-driven/>
<!--4.静态资源放行-->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!--不释放statics无法正常显示页面-->
<!--5.配置多文件/部件文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxInMemorySize" value="543543434"/>
</bean>
</beans>
UserMapper
package com.xja.cvs.mapper;
import com.xja.cvs.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
public User selectUserByAccountAndPassword(
@Param("account") String account,
@Param("password") String password)throws Exception;
}
UserMapper.xml
<!--namespace 命名空间 -->
<mapper namespace="com.xja.cvs.mapper.UserMapper">
<!--1.根据账号和密码,查询用户信息-->
<select id="selectUserByAccountAndPassword" resultType="User">
<!-- 编写SQL语句 -->
select * from t_sys_user
<where>
<if test="account !=null and account != '' ">
account = #{account}
</if>
<if test="password != null and password !='' ">
and password= #{password}
</if>
</where>
</select>
</mapper>
UserServiceImpl
package com.xja.cvs.service.impl;
import com.xja.cvs.mapper.UserMapper;
import com.xja.cvs.pojo.User;
import com.xja.cvs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//将业务层接口转化为实例化对象的实现类
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;//报错位置就在这userMapper有红色下划线报错信息是Could not autowire. No beans of 'UserMapper' type found.
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public User queryUserByAccoundAndPassword(String account, String password) {
User user = null;
if (account != null && !"".equals(account)) {
if (password != null && !"".equals(password)) {
try {
user = userMapper.selectUserByAccountAndPassword(account, password);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return user;
}
}
报错地方在源代码UserServiceImpl实现类里面求指教应该将哪里修改才能解决问题,对java不熟悉请见谅,谢谢
看起来问题出在`UserMapper`没有被Spring管理。要解决这个问题,您需要确保以下几点:
1. **UserMapper接口的配置**:确认MyBatis能够扫描到`UserMapper`接口。通常可以在Spring配置中添加MyBatis的扫描路径。
2. **配置@MapperScan注解**:在主类或者配置类上使用`@MapperScan`注解,指定`mapper`接口所在的包路径。
例如,假设`UserMapper`接口在`com.xja.cvs.mapper`包下:
// 在配置类或者Spring Boot应用入口类中
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.xja.cvs.mapper")
public class MyBatisConfig {
// 其他配置
}
3. **检查MyBatis和Spring的依赖**:确保你的`pom.xml`或者`build.gradle`中包含mybatis-spring相关依赖。如果是Spring Boot项目,可以直接添加:
<!-- MyBatis Spring Boot Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version> <!-- 检查版本号是否最新 -->
</dependency>
4. **applicationContext.xml配置**:如果没有配置MyBatis的扫描,可以尝试下面的配置:
<!-- MyBatis的bean配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/xja/cvs/mapper/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xja.cvs.mapper" />
</bean>
请根据实际情况调整包路径和XML配置,确保所有配置文件的一致性。配置完成后,重新启动项目,看是否解决问题。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 本帖最后由 Filthy-ice 于 2024-12-4 14:27 编辑
如果是SpringBoot项目, 现在最简单的处理方法就是在Mapper接口上加上@Mapper注解(配置5删掉)就行.ssm不熟悉, 但是应该也可以删掉配置五, 然后在配置类上使用@MapperScan("com.xja.**.mapper")
你要注意你的mapper扫描路径配置:
<!--5.配置mapper扫描bean-->.... value='com.xja.ssm.mapper'
而你的UserMapper
package com.xja.cvs.mapper;扫描不到的,好像xml不支持com.xja.**.mapper这种包含式写法.
还有你这个配置
<!--4.SqlSessionFactoryBean -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.xja.cvs.pojo"/>
<property name="mapperLocations" value="classpath:com.xja.cvs.mapper/*.xml"/>-> 你应该是改过, value="classpath:mapper/*.xml"所有的xxMapper.xml文件都放入./src/main/resources/mapper,就能够识别
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
页:
[1]