springBoot+Maven完全开发手册(项目入门搭建04-整合myBatis)

SpringBoot整合MyBatis

1.数据库,实体类等

这里就不再一一叙述,建表和创建实体类原则看ssm部分

2.在pom.xml中加入相关依赖(坐标)。

<!-- 配置MyBatis启动器 -->
<dependency>
  	<groupId>org.mybatis.spring.boot</groupId>
  	<artifactId>mybatis-spring-boot-starter</artifactId>
  	<version>1.3.0</version>
</dependency>
<!-- 配置mysql驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 配置c3p0连接池 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

3.配置数据源(在application.properties或application.yml中配置一些依赖所需要的参数)

spring: 
   datasource: 
      url: jdbc:mysql://localhost:3306/maven_ssm_fenmokuai_easyui_day29zuoye 
      driverClassName: com.mysql.jdbc.Driver 
      username: root 
      password: root 
      type: com.mchange.v2.c3p0.ComboPooledDataSource 
server:
   port: 8888 
   context-path: /simpleproduct 

4.实现查询功能

controller层:
package com.xiaoxin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoxin.bean.Product;
import com.xiaoxin.service.IProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	IProductService productService;
	
	
	@ResponseBody
	@RequestMapping(path="/getAllProduct",produces={"application/json;charset=utf-8"})
	public List<Product> getOneProduct() {
		return productService.getAllProduct();
	}
	
}

service层:
package com.xiaoxin.service;

import java.util.List;

import com.xiaoxin.bean.Product;

public interface IProductService {
	
	/**
	 * 获得所有的商品
	 * @return List<Product>
	 * */
	public List<Product> getAllProduct();
	
}

serviceImpl层:
package com.xiaoxin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoxin.bean.Product;
import com.xiaoxin.mapper.ProductMapper;
import com.xiaoxin.service.IProductService;

@Service
public class ProductServiceImpl implements IProductService {
	
	@Autowired
	ProductMapper productMapper;

	@Override
	public List<Product> getAllProduct() {
		return productMapper.getAllProduct();
	}
	
}

dao层
package com.xiaoxin.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.xiaoxin.bean.Product;

@Repository
public interface ProductMapper {
	
	/**
	 * 获得所有的商品
	 * @return List<Product>
	 * */
	@Select("select * from product")
	public List<Product> getAllProduct();
	
	
}

5.在SpringBoot启动类配置@MapperScan

启动类放在要加载包的父路径上面,当然你也可以用componentScan,spring的单元测试中的class是指定启动类的.class文件

package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // 代表为SpringBoot应用的运行主类
@MapperScan(basePackages = {"com.xiaoxin.mapper"})
public class SpringBootApplicationRun {
	
	/** 运行SpringBoot应用 */
	 public static void main(String[] args) {
//		 第一个参数和本类名一致
		 SpringApplication.run(SpringBootApplicationRun.class, args);
		
	}

}

6.跑一跑

在这里插入图片描述

成功!

7.启动类中@MapperScan范围过大出现的bug

如果在SpringBoot的启动类中的mapper扫描范围过大,则会出现bug,建议该扫描范围仅仅只有mapper类

错误演示:

项目文件夹路径:
在这里插入图片描述

我们来看看出现的错误:

Field productService in com.xiaoxin.controller.ProductController required a single bean, but 2 were found:
	- productServiceImpl: defined in file [D:\专业系统学习\java_workplace\xxx\java学习第二阶段\SpringBoot_Meven\springbootzongjiejar\target\classes\com\xiaoxin\service\impl\ProductServiceImpl.class]
	
        
	- IProductService: defined in file [D:\专业系统学习\java_workplace\xxx\java学习第二阶段\SpringBoot_Meven\springbootzongjiejar\target\classes\com\xiaoxin\service\IProductService.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

可以看到:命名IproductService在这个项目中只有一个实现类,所以controller层中的自动注入@Autowired不加 @Qualifier指定要注入的子类是没问题的,但是它这里启动都报了错,原因是重复扫描:springBoot里面帮启动类扫描了所有的实体(componentScan),而这里的mapperScan是专门用来扫mapper类的,而因为范围太大扫到了serviceImpl层,所以重复扫了

进一步解释:

在这里插入图片描述

之后停掉服务器,再修改一下启动类

在这里插入图片描述

说了这么多,解决办法是mapperScan的包扫描最好只扫mapper类

当然上述问题也可以通过给@service(“tt”),然后controller层@Qualifier(“tt”)解决,但不推荐这样做

该小范围后又能跑了.
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值