前后端项目练习(整合 Spring)
将处理之后的结果,按照特定的要求?返回给用户(返回) SpringMVC Controller
?过程
1.数据库(内容可自行添加)
建表语句:
CREATE TABLE
demo_user
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
char(40) DEFAULT NULL,
age
int(11) DEFAULT NULL,
sex
char(40) DEFAULT NULL,
PRIMARY KEY (
id
),
KEY
cc
(id
),
KEY
user_index
(name
)
) ENGINE=InnoDB AUTO_INCREMENT=234 DEFAULT CHARSET=utf8
数据库示例:
2.搭建后端项目(IDEA)
2.1 目录结构(创建 Maven 项目)
![](https://im
g-blog.csdnimg.cn/20210726215648759.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ4MjMzMw==,size_16,color_FFFFFF,t_70)
2.2? 配置 pom 文件 添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<!--跳过测试类打包-->
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--springboot启动项(器)在包的内部SpringBoot
已经完成了项目的"整合"(配置) 用户拿来就用
web导入SpringMVC
-->
<artifactId>spring-boot-starter-web</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-devtools</artifactId>
</dependency>
<!--添加lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--导入MP包之后,删除原有的Mybatis的包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.2</version>
</plugin>
</plugins>
</build>
2.3 配置 application.yml 文件
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#SpringBoot 整合 MP
mybatis-plus:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/*.xml
configuration:
map-underscore-to-camel-case: true
Mapper 接口执行 打印 Sql 日志
logging:
level:
com.jt.mapper: debug
2.4 配置映射文件 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.jt.mapper.UserMapper">
</mapper>
2.5 启动类
@SpringBootApplication
@MapperScan("com.jt.mapper") //Spring 容器内部为接口创建代理对象
//JDK 的动态代理对象
public class SpringBootRun {
//标准写法
评论