概述
这里通过设计一个对用户进行增删改查的接口服务,来练习java项目工程化、Spring框架、Mybatis框架的实际应用
本项目目录
上一节初始化项目,已经controller层了,下方新建包:pojo、mapper、service
pojo:所有的实体类都放这里
mapper:所有的mapper接口都放这里(可以理解为数据访问层)
service:具体的业务逻辑处理写这里(curd)
创建实体类
package com.mxf.monkey.pojo;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
/*
* 定义了一个用户实体类
* 包含getter、setter等构造方法
* */
private int user_id;
private String user_name;
private String user_sex;
}
根据实体类创建和初始化数据
定义一个查询用户列表的mapper接口
package com.mxf.monkey.mapper;
import com.mxf.monkey.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UserMapper {
//定义了一个查询用户列表的方法,返回list类型
List<User>queryUserList();
}
写mapper接口对应的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.mxf.monkey.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from monkey.monkey_user
</select>
</mapper>
pom文件中更新mybatis依赖
其他的依赖如sping、mysql等在初始化项目时已经建了
<?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.7.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mxf</groupId>
<artifactId>monkey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>monkey</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--MyBatis-Spring依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--数据库连接依赖-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
mybatis映射配置及Spring数据库配置
上边定义了mapper接口和mapper映射文件,那么怎么把2个联系起来,就需要通过在application.yml中进行配置
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/你的表名?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: 数据库账号
password: 数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.mxf.monkey.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
创建service服务
package com.mxf.monkey.service;
import com.mxf.monkey.mapper.UserMapper;
import com.mxf.monkey.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List<User> queryUserList(){
//导入UserMapper中的queryUserList方法,并调用
return userMapper.queryUserList();
}
}
创建controller对应接口
package com.mxf.monkey.controller;
import com.mxf.monkey.pojo.User;
import com.mxf.monkey.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/queryUserList")
public List<User> queryUserList() {
return userService.queryUserList();
}
}