1 搭建项目结构
1.1 新建一个空的maven项目
1.2 配置maven设置
在Settings中搜索maven,将maven版本、maven配置文件和仓库路径改为自己的
1.3 配置编译环境
在Settings中搜索java complier,将Target bytecode version版本改为对应的JDK版本
以及在Project Structure中修改Language level
1.4 创建目录结构
1.5 导入项目依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
</parent>
<dependencies>
<!--SpringBoot通用依赖模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!--Mysql数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2 配置Springboot
在resource文件夹下新建application.yml,配置端口及数据源
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis:
mapper-locations:
- classpath:mapper/*.xml
type-aliases-package: com.qin.mall.pojo //mybatis别名
configuration:
map-underscore-to-camel-case: true #开启驼峰映射,如createTime => create_time
在com.qin.mall包下新建Application类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
3 创建数据库
创建一个mall的数据库,在mall数据库下创建user表
CREATE TABLE `mall`.`user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(64) NOT NULL,
`password` VARCHAR(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci;
INSERT INTO `user`(`username`,`password`)
VALUES ('user1','123456'),('user2','123456'),('user3','123456');
4 在config包下新增MybatisConfig类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.qin.mall.dao")
public class MybatisConfig {
}
5 写User实体类
import lombok.Data;
@Data
public class User {
private Integer id;
private String username;
private String password;
}
6 dao层实现
6.1 在dao包下新建UserMapper接口
import com.qin.mall.pojo.User;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public interface UserMapper {
List<User> getAllUsers();
}
6.2 在resources的mapper目录下新建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">
<mapper namespace="com.qin.mall.dao.UserMapper">
<select id="getAllUsers" resultType="User">
select * from USER
</select>
</mapper>
7 在Service包下新建UserService接口
import com.qin.mall.pojo.User;
import java.util.List;
public interface UserService {
List<User> getAllUsers();
}
8 在Service包下实现UserService接口,新建UserServiceImpl
import com.qin.mall.dao.UserMapper;
import com.qin.mall.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public List<User> getAllUsers() {
List<User> users = userMapper.getAllUsers();
return users;
}
}
9 在Controller包下新建UserController
import com.qin.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
UserService userService;
@GetMapping(value = "/getAllUsers")
@ResponseBody
public void getAllUsers(){
System.out.println(userService.getAllUsers());
}
}
10 运行Application,访问http://localhost:8080/user/getAllUsers接口,成功输出所有用户信息