Springboot整合3.整合mybatis
Posted tea_year
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot整合3.整合mybatis相关的知识,希望对你有一定的参考价值。
3.整合mybatis
1.在pom添加ja依赖
<!-- 今天加上mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
2.启动测试,发现报错:
escription:
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
在SpringBoot里面,配置这些数据库源之后,需要提供相应的配置文件
3.创建mapper接口和配置文件
/*需要指明该类是一个Mapper,加mapper文件*/
@Mapper
public interface StockMapper
//CRUD操作;
int addStock(Stock stock);
int deleteStockById(Integer id);
int updateStockById(Stock stock);
Stock getStockById(Integer id);
List<Stock> getAllstocks();
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--指定命名空间namespace,就是该xml对应的dao接口-->
<mapper namespace="com.aaa.mapper.StockMapper">
<insert id="addStock" parameterType="com.aaa.entity.Stock">
insert stock(name,num,desp)values(#name,#num,#desp)
</insert>
</mapper>
4.创建service类
@Service
public class StockService
@Autowired
StockMapper stockMapper;
public int addStock(Stock stock)
stockMapper.addStock(stock);
int deleteStockById(Integer id)
return stockMapper.deleteStockById(id);
int updateStockById(Stock stock) //?
return stockMapper.updateStockById(stock);
Stock getStockById(Integer id)
return stockMapper.getStockById(id);
List<Stock> getAllstocks()
return stockMapper.getAllstocks();
5.创建Controller类
package com.aaa.controller;
import com.aaa.entity.Stock;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
//复习:遍历一下模拟数据,暂时不用连接数据库。
@RestController
public class StockController
@RequestMapping("/getallstocks")
public ModelAndView getAllStocks()
//先不加数据库的测试 ;
Stock stock1=new Stock(1,"可乐",22,"老美的可乐");
Stock stock2=new Stock(3,"雪碧",223,"中国的可乐");
List<Stock> stockList=new ArrayList<>();
stockList.add(stock1);
stockList.add(stock2);
ModelAndView mv=new ModelAndView();
mv.addObject("stocklist",stockList);
mv.setViewName("stocklist");
return mv;
//复习的时候,争取把json数据给获取出来
@RequestMapping("/getallstocks2")
public List<Stock> getAllStocks2()
//先不加数据库的测试 ;
Stock stock1=new Stock(1,"可乐",22,"老美的可乐");
Stock stock2=new Stock(3,"雪碧",223,"中国的可乐");
List<Stock> stockList=new ArrayList<>();
stockList.add(stock1);
stockList.add(stock2);
return stockList;
6.templates下创建页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
测试...默认在template下的网页,静态网页...
大家做下测试...这次没作业,作业还是上次的Intent交互...
</body>
</html>
7.启动测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BVy5aXwH-1650073977871)(asseits/image-20220416095111689.png)]
测试…默认在template下的网页,静态网页…
大家做下测试…这次没作业,作业还是上次的Intent交互…
7.启动测试
[外链图片转存中…(img-BVy5aXwH-1650073977871)]
以上是关于Springboot整合3.整合mybatis的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot | 3.2 整合MyBatis #yyds干货盘点#