这是controller的代码,报错注入的bookservice为空
package com.dd.controller;
import com.dd.domain.Book;
import com.dd.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
public BookService bookService;
@PostMapping
public boolean add(@RequestBody Book book) {
bookService.add(book);
return true;
}
@PutMapping
public boolean update(@RequestBody Book book) {
bookService.update(book);
return true;
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
bookService.delete(id);
return true;
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping
public List<Book> getAll() {
return bookService.getAll();
}
}
============================
这是service层的接口和实现类
package com.dd.service;
import com.dd.domain.Book;
import java.util.List;
public interface BookService {
/**
* 增加
* @param book
*/
public boolean add(Book book);
/**
* 修改
* @param book
*/
public boolean update(Book book);
/**
* 删除
* @param id
*/
public boolean delete(Integer id);
/**
* 查询指定用户
* @param id
* @return
*/
public Book getById(Integer id);
/**
* 查询全部用户
* @return
*/
public List<Book> getAll();
}
package com.dd.service;
import com.dd.dao.BookDao;
import com.dd.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService{
@Autowired
@Qualifier("bookDao")
public BookDao bookDao;
@Override
public boolean add(Book book) {
bookDao.add(book);
return true;
}
@Override
public boolean update(Book book) {
bookDao.update(book);
return true;
}
@Override
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
@Override
public Book getById(Integer id) {
bookDao.getById(id);
return null;
}
@Override
public List<Book> getAll() {
bookDao.getAll();
return null;
}
}
下边是各个配置类
==============================
package com.dd.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driver);
return dataSource;
}
}
===========================
package com.dd.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.dd.domain");
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.dd.dao");
return msc;
}
}
===========================
package com.dd.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringmvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
============================
package com.dd.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan("com.dd.controller")
public class SpringmvcConfig {
}
=================================
package com.dd.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.dd.service")
@PropertySource("ClassPath:Jdbc.properties")
@Import({MyBatisConfig.class, JdbcConfig.class})
public class SpringConfig {
}
============================
然后是dao
package com.dd.dao;
import com.dd.domain.Book;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface BookDao {
// @Insert("insert into book values(null, #{type}, #{name}, #{description})")
@Insert("insert into book (type, name, description) values(#{type}, #{name}, #{description})")
public boolean add(Book book);
@Update("update book set type=#{type}, name=#{name}, description=#{description} where id=#{id}")
public boolean update(Book book);
@Delete("delete from book where id =#{id}")
public boolean delete(Integer id);
@Select("select * from book where id= #{id}")
public Book getById(Integer id);
@Select("select * from book")
public List<Book> getAll();
}
================================
实体类如下
package com.dd.domain;
public class Book {
private Integer id;
private String type;
private String name;
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
controller注入的bookservice为空
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
dxs32 2024-02-16 19:20关注在单元测试中无法依赖注入Mapper接口的原因是因为Mapper接口是由Mybatis框架在运行时动态生成的代理类,而在单元测试中并没有启动整个Spring容器,也就无法使用Mybatis框架生成的代理类。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报