目录结构:
1.依赖引入
<?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>SpringBoot1</groupId>
<artifactId>SpringBoot1</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 添加SpringMVC启动器依赖:spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.5</version>
</dependency>
<!--mybatis的启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2.TestApplication
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Auther: iteration.0
* @Date: 25/3/2021 - 03 - 25 - 10:51 PM
* @Description: com
* @version: 1.0
*/
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
}
3.UesrContrellor
package com.msb.controller;
import com.msb.pojo.User;
import com.msb.service.UserService;
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 java.util.List;
/**
* @Auther: iteration.0
* @Date: 2021-11-07 - 11 - 07 - 5:43 PM
* @Description: com.msb.controller
* @version: 1.0
*/
@Controller
@RequestMapping("/user")
public class UesrContrellor {
@Autowired
UserService userService;
@RequestMapping("findAll")
@ResponseBody
public List<User> findAll(){
return userService.findAll();
}
}
4.UserMapper
package com.msb.mapper;
import com.msb.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @Auther: iteration.0
* @Date: 2021-11-07 - 11 - 07 - 5:49 PM
* @Description: com.msb.mapper
* @version: 1.0
*/
@Mapper
public interface UserMapper {
List<User> selectAll();
}
5.User
package com.msb.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @Auther: iteration.0
* @Date: 2021-11-07 - 11 - 07 - 5:38 PM
* @Description: com.msb.pojo
* @version: 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
private Integer id;
private String userName;
private String sex;
private String stills;
}
6.UserService
package com.msb.service;
import com.msb.pojo.User;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Auther: iteration.0
* @Date: 2021-11-07 - 11 - 07 - 5:47 PM
* @Description: com.msb.service
* @version: 1.0
*/
public interface UserService {
List<User> findAll();
}
7.UserServiceImpl
package com.msb.service.impl;
import com.msb.mapper.UserMapper;
import com.msb.pojo.User;
import com.msb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Auther: iteration.0
* @Date: 2021-11-07 - 11 - 07 - 5:48 PM
* @Description: com.msb.service.impl
* @version: 1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAll() {
return userMapper.selectAll();
}
}
8.UserMapper.xml
<?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" >
<!--namespace必须是mapper的全路径 -->
<mapper namespace="com.msb.mapper.UserMapper">
<select id="selectAll" resultType="user">
SELECT * from user;
</select>
</mapper>
9.application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
# driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123
mybatis:
type-aliases-package: com.msb.pojo
mapper-locations: classpath:com.msb.mapper/*.xml
10.结果
报错;mapper 接口已经加了@Mapper注解,UserServiceImpl仍然报错
解决办法:
idea 按快捷键 Ctrl+shift+Alt ,将代码校验严格程度跳到中间即可: