SSM框架入门学习记录
//项目结构
包名多种多样,命名习惯问题。
流程:Controller(接受请求)——>Service(biz)(bl)(写业务函数接口)(impl实现)——>Mapper(dao)(data)(接口)——>mapper.xml(写sql)
impl文件夹,写函数实现
entity实体类(pojo)//有的命名pojo
Mapper:将实体映射到数据库的接口
Dao: Data Access Objects 连接数据库
logo4j2.xml(日志处理的配置文件)
controller调用service,service调用mapper;
控制层要@Controller,服务层@Service,mapper@Mapper
每个mapper对应一个mapper.xml
mapper.xml文件中:我们要做到mapper namespace路径和mapper接口路径一致
@ResponseBody//自动转换成相应类型返回给前端
@RequestBody接收json数据
@RequestParam接收参数
可一起使用
mapper写接口,mapper.xml写实现,id对应函数名。
添加完新依赖,动了pom文件之后,都install一下。
项目用utf-8编码。
application 内嵌tomcat服务,不用另外安装tomcat
各种配置使用注解配置,不用xml配置。
Mybatis:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCYYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 38//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="写对应Mapper包的路径">
<insert id="写对应Mapper接口的函数名" parameterType="写传入的参数类型,可省略" resultType="">
写SQL语句,#{传入的参数名}
</insert>
<resultMap id="Teacher" type="">//返回不规则数据集,自定义
</mapper>```
```xml
//yml配置文件
server:
port: 8080//端口
spring:
datasource:
username: root
password: hfut2018214813
#上面用户名和密码要改,下面数据库名字也要改
url: jdbc:mysql://localhost:3306/paikaosystem?serverTimezone=CTT&characterEncoding=UTF-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.example.demo.entity
#showSql
logging:
level:
com:
example:
mapper : debug
//pom.xml依赖配置
<?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.3.2.RELEASE</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-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<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.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.3.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>