專案結構

報錯
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
applicationContext.xml
<!-- 1. 配置資料庫連接資訊 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="https://bbs.csdn.net/topics/oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="https://bbs.csdn.net/topics/jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="https://bbs.csdn.net/topics/scott"/>
<property name="password" value="https://bbs.csdn.net/topics/tiger"/>
</bean>
<!-- 2.事務管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3.配置會話工廠 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="https://bbs.csdn.net/topics/classpath:mybatis-config.xml"/>
</bean>
<!-- 自動掃描Competent -->
<context:component-scan base-package="com.service,com.dao"/>
UserController
package com.controller;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value = "Login", produces = "text/html;charset=utf-8",method = RequestMethod.POST)
public String Login(Model model, String name, String pwd) {
int ans = userService.userLogin(name, pwd);
// int ans=0;
String tmp = null;
switch (ans) {
case 0:
tmp = "請輸入用戶名和密碼";
break;
case 1:
tmp = "用戶名不存在";
break;
case 2:
tmp = "密碼錯誤";
break;
case 3:
tmp = "登錄成功+name";
model.addAttribute("name", name);
break;
}
model.addAttribute("tmp", tmp);
if (ans < 3)
return "wrong";
else
return "welcome";
}
@RequestMapping(value = "page/Register", produces = "text/html;charset=utf-8",method = RequestMethod.POST)
public String Register(HttpServletRequest request, HttpServletResponse response, String name, String pwd) throws IOException {
boolean flag=userService.userRegister(name,pwd);
// boolean flag=true;
String tmp=flag?"注冊成功":"注冊失敗";
HttpSession session= request.getSession();
session.setAttribute("tmp",tmp);
if(flag)
return "redirect:../index.jsp";
else
return "wrong";
}
}
UserService
package com.service;
import com.dao.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.po.User;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class UserService{
@Resource
public UserMapper userMapper;
@Transactional
public boolean userRegister(String name,String pwd)
{
Map<String,String> map=new HashMap<>();
map.put("name",name);
map.put("pwd",pwd);
int ans=userMapper.addUser(map);
// int ans=1;
return ans>0;
}
@Transactional
public int userLogin(String name, String pwd) {
if (name == null || pwd == null)
return 0;
User user = userMapper.getUserByName(name);
// User user=null;
if (user == null)
return 1;
else if (user.getPwd().equals(pwd))
return 3;
else
return 2;
}
}
pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpBatis3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpBatis3 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis.version>3.5.4</mybatis.version>
<mybatis.spring.version>2.0.1</mybatis.spring.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--mybatis.jar,mybatis-spring.jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--tags,jstl-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!--odbc-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.0.0.0.0</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars.bower/echarts -->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>echarts</artifactId>
<version>4.7.0</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>maven_artifact</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
uj5u.com熱心網友回復:
mybatis mapper沒有注入到容器MapperScannerConfigurer這玩意配置一下就行了
uj5u.com熱心網友回復:
我在applicationContext里寫了啊
<!-- 自動掃描Competent -->
<context:component-scan base-package="com.service,com.dao"/>
而且如果寫成這樣
<!-- 自動掃描Competent -->
<context:component-scan base-package="com.service"/>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="https://bbs.csdn.net/topics/com.dao" />
</bean>
還是不行
No qualifying bean of type 'com.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate.
uj5u.com熱心網友回復:
改成這樣<context:component-scan base-package="com.service,com"/>
uj5u.com熱心網友回復:
報錯:
No qualifying bean of type 'com.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
uj5u.com熱心網友回復:
重新用maven建了一遍專案,選擇最麻煩的方式治好了<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<!-- aop -->
<!--<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- context容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring測驗 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 事務 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jdbc模板技術 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 單元測驗 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!--odbc-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.0.0.0.0</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- EL JL TL運算式 -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!-- mybatis相關 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
應該是我spring-boot+mybatis缺包了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225638.html
標籤:Web 開發
上一篇:關于辦公自動化的問題,求大神解答
下一篇:Java求助
