赞
踩
❝1.配置相关的依赖
2.配置模式❞
3写.mapper、controller、service
4.配置yaml文件 配置mybatis全局配置文件
(这里我使用的是配置模式+注解模式所以需要配置全局文件)
当然也可以在创建springboot的时候勾选对应的功能
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.25</version>
- </dependency>
- <!--mybatis整合springboot起步依赖-->
- <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.3</version>
- </dependency>
在写这个之前话要写基本的pojo
❝❞
pojo相关 这里使用了 lombok
- package com.xbfinal.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class ssmdb {
- private Integer id;
- private String name;
- private String type;
- private String description;
- }
❝话不多说注意代码的注释
❞
- package com.xbfinal.mapper;
-
-
- import com.xbfinal.pojo.ssmdb;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.util.List;
-
- @Mapper
- public interface ssmdbMapper {
-
- //更具id查对应的书用注解模式
- @Select("select * from tbl_book where id=#{id}")
- public ssmdb getById(int id);
-
- //查询所有的书 ,这里我们用配置模式
- //配置模式我个人喜欢用来写复制的sql语句(狗头)
- public List<ssmdb> getAll();
-
- }
❝❞
一般用来实现mapper的 直接看代码
- package com.xbfinal.service;
-
- import com.xbfinal.mapper.ssmdbMapper;
- import com.xbfinal.pojo.ssmdb;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class ser {
- @Autowired
- ssmdbMapper ssmdbMapper;
- public ssmdb getById(int id){
- return ssmdbMapper.getById(id);
- }
- public List<ssmdb> getAll(){
- return ssmdbMapper.getAll();
- }
- }
- package com.xbfinal.controller;
-
- import com.xbfinal.pojo.ssmdb;
- import com.xbfinal.service.ser;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.List;
-
- @RestController
- public class controller01 {
- @Autowired
- ser ser;
- @RequestMapping("/id")
- public String to01(){
-
- final ssmdb byId = ser.getById(1);
- return byId.toString();
- }
- @RequestMapping("/all")
- public String to02(){
- // final ssmdb byId = ser.getById(1);
-
- final List<ssmdb> all = ser.getAll();
- return all.toString();
- }
-
- }
❝1.写mysql的文件。写在application.yaml文件中
❞
- spring:
- datasource:
- password: 0615
- username: root
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
❝2.由于用配置模式+注解模式所以需要配置mybatis全局文件
❞
❝在static文件下创建mybatis文件夹然后创建配置文件如下
❞
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <!--基本的东西都不用配置,因为springboot配好了-->
- </configuration>
❝接着在mapper文件下写mybatis对应的mapper配置
❞
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.xbfinal.mapper.ssmdbMapper">
-
- <!--public List<ssmdb> getAll();-->
- <select id="getAll" resultType="com.xbfinal.pojo.ssmdb">
- select * from tbl_book
- </select>
- </mapper>
❝最后在yaml文件中配好mybatis
❞
在这里插入图片描述
- mybatis:
- config-location: classpath:static/mybatis/mybatis-config.xml
- mapper-locations: classpath:static/mybatis/mapper/*.xml
❝数据库
❞
❝解决方案
❞检查自己的mysql配置是否正确如果正确尝试一下把密码加上""如图
来源:https://blog.csdn.net/weixin_52062043/article/details/124434652
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。