项目的目录结构
demo数据库中user的表结构
首先在项目中新建四个包,分别为Controller,dao(mapper),po,service,注意项目的DemoApplication与他们同层,否则会报404错误。
Controller层
package com.example.demo.controller;
import com.example.demo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.userServiceImpl;
import java.util.List;
import java.util.Map;
@RestController
public class userController {
@Autowired
private userServiceImpl userServiceImpl;
@RequestMapping("/getUser")
@ResponseBody
public List<User> findAll(){
List<User> list = userServiceImpl.findAll();
return list;
}
}
service层
package com.example.demo.service;
import com.example.demo.po.User;
import java.util.List;
public interface userService {
public List<User> findAll();
}
serviceImpl
package com.example.demo.service;
import com.example.demo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.example.demo.mapper.userMapper;
@Service
public class userServiceImpl implements userService{
@Autowired
private userMapper userMapper;
@Override
public List<User> findAll(){
return userMapper.findAll();
}
}
dao层
package com.example.demo.mapper;
import com.example.demo.po.User;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface userMapper {
public List<User> findAll();
}
po层
package com.example.demo.po;
public class User {
public int getUserid() {
return userid;
}
public User(int userid, String username) {
this.userid = userid;
this.username = username;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private int userid;
private String username;
}
在 DemoApplication添加映射扫描的注解
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在application.properties进行相关配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=8080
mybatis.type-aliases-package=com.example.demo.po
mybatis.mapper-locations=classpath:mapper/*.xml
在resource资源下,新建mapper.xml,进行sql语句的编写
<?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.example.demo.mapper.userMapper">
<select id="findAll" resultType="User">
select * from user
</select>
</mapper>
在pom.xml文件中添加mysql和jdbc依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
具体代码的下载地址:https://github.com/sstlyy/demo/tree/main