ssm框架【springmvc spring mybatis】。其实就是spring和mybatis的整合。spring的整合mybatis的配置文件到自己的配置文件中。
1 创建表
create database test03;
use test03;
create table tbl_student(
id int primary key auto_increment comment '主键',
name varchar(20) not null comment '姓名',
age int default 18 comment '年龄',
gender char(1) default '男' comment '性别',
birthday date comment '生日',
headImg varchar(100) comment '头像',
class_id int comment '班级' -- 逻辑外键。
);
create table tbl_class(
id int primary key auto_increment comment '主键',
name varchar(20) not null comment '名称'
);
insert into tbl_class values(null,'1班');
insert into tbl_class values(null,'2班');
insert into tbl_class values(null,'3班');
insert into tbl_student values(null,'张三',18,'男','1998-01-01','https://qy178.oss-cn-hangzhou.aliyuncs.com/177f1aab969248dfaa3b2583e8b359a21.jpg',1);
insert into tbl_student values(null,'李四',19,'男','1997-01-01','https://qy178.oss-cn-hangzhou.aliyuncs.com/177f1aab969248dfaa3b2583e8b359a21.jpg',2);
insert into tbl_student values(null,'王五',20,'男','1996-01-01','https://qy178.oss-cn-hangzhou.aliyuncs.com/177f1aab969248dfaa3b2583e8b359a21.jpg',3);
insert into tbl_student values(null,'赵六',20,'男','1996-01-01','https://qy178.oss-cn-hangzhou.aliyuncs.com/177f1aab969248dfaa3b2583e8b359a21.jpg',2);
2 创建工程并添加依赖
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ykq</groupId>
<artifactId>qy178-ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.2.15.RELEASE</spring.version>
<mybatis.version>3.5.16</mybatis.version>
</properties>
<dependencies>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--spring和mybatis整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.1.2</version>
</dependency>
<!--jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!---->
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.23</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
</build>
</project>
3 生成实体类和dao和映射文件
4 spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--包扫描-->
<context:component-scan base-package="com.ykq"/>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--放行静态资源-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--引入属性文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--数据源-->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--SqlSessionFactoryBean 把数据源和映射文件以及mybatis其他配置文件都整合在该类中 reference-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<!--加载映射文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--为dao接口生成代理实现类。-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--dao接口包名.把这些代理实现类放入容器内容。-->
<property name="basePackage" value="com.ykq.mapper"/>
</bean>
</beans>
5 注册spring前端控制器并且读取配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6 controller
package com.ykq.controller;
import com.ykq.entity.Student;
import com.ykq.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController //返回的都是json数据
@RequestMapping("/student")
public class StudentController {
@Autowired //根据spring为接口生成的代理实现类 帮你注入该属性的值。
private StudentMapper studentMapper;
//查询所有学生
@GetMapping("/getById")
public Student getById(Integer id){
Student student = studentMapper.selectByPrimaryKey(id);
return student;
}
//添加操作
@PostMapping("/add")
public String add(Student student){ //接受的表单参数。
studentMapper.insertSelective(student);
return "添加成功";
}
//修改
@PutMapping("/update")
public String update(Student student){
studentMapper.updateByPrimaryKeySelective(student);
return "修改成功";
}
//删除操作
@DeleteMapping("/delete")
public String delete(Integer id){
studentMapper.deleteByPrimaryKey(id);
return "删除成功";
}
@GetMapping("/list")
public List<Student> list(){
List<Student> students = studentMapper.selectAll();
return students;
}
//可以使用postman或apipost【软件】测试。 测试controller方法接口的。
}
如果前后端分离。 后端返回的都是json数据。 要求后端返回的json格式必须统一。
{
code: "状态码",
msg: "消息提示",
data: 数据
}
package com.ykq.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author: 闫克起 view object 视图对象
* @Description:统一格式的json数据
* @Date: Create in 11:21 2024/10/12
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R {
private Integer code;//状态码
private String msg;//提示消息
private Object data;//数据
public R(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static R ok(){
return new R(200,"操作成功");
}
public static R ok(Object data){
return new R(200,"操作成功",data);
}
public static R error(String msg){
return new R(500,"操作失败");
}
}
controller
package com.ykq.controller;
import com.ykq.entity.Student;
import com.ykq.mapper.StudentMapper;
import com.ykq.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController //返回的都是json数据
@RequestMapping("/student")
public class StudentController {
@Autowired //根据spring为接口生成的代理实现类 帮你注入该属性的值。
private StudentMapper studentMapper;
//查询所有学生
@GetMapping("/getById")
public R getById(Integer id){
Student student = studentMapper.selectByPrimaryKey(id);
if(student!=null){
return R.ok(student);
}
return R.error("查询失败");
}
//添加操作
@PostMapping("/add")
public R add(Student student){ //接受的表单参数。
int i = studentMapper.insertSelective(student);
if(i>0){
return new R(200,"添加成功");
}
return new R(500,"添加失败");
}
//修改
@PutMapping("/update")
public R update(Student student){
int i = studentMapper.updateByPrimaryKeySelective(student);
if(i>0){
return new R(200,"修改成功");
}
return new R(500,"修改失败");
}
//删除操作
@DeleteMapping("/delete")
public R delete(Integer id){
int i = studentMapper.deleteByPrimaryKey(id);
if(i>0){
return new R(200,"删除成功");
}
return new R(500,"删除失败");
}
@GetMapping("/list")
public R list(){
List<Student> students = studentMapper.selectAll();
return new R(200,"查询成功",students);
}
//可以使用postman或apipost【软件】测试。 测试controller方法接口的。
}
标签:return,整合,SSM,student,org,import,com,public
From: https://www.cnblogs.com/xiaomubupi/p/18631268