ssm整合
先展示目录结构
controller层
package com.yaokui.controller;
import com.yaokui.domain.Tblstudent;
import com.yaokui.service.StudentService;
import com.yaokui.util.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
/**
*@ClassName StudentController
*@Description TODO
*@Author 19649
*@Date 2023/4/9 9:23
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("getAll")
public ReturnData getAll(){
return studentService.queryAll();
}
@GetMapping("getById/{sid}")
public ReturnData getById(@PathVariable Integer sid){
return studentService.queryById(sid);
}
@PostMapping("insert")
public ReturnData insert(@RequestBody Tblstudent tblstudent){
return studentService.insert(tblstudent);
}
@PutMapping("update")
public ReturnData update(@RequestBody Tblstudent tblstudent){
return studentService.update(tblstudent);
}
@DeleteMapping("delete/{sid}")
public ReturnData delete(@PathVariable Integer sid){
return studentService.delete(sid);
}
}
service层接口
package com.yaokui.service;
import com.yaokui.domain.Tblstudent;
import com.yaokui.util.ReturnData;
/**
* 功能描述
*
* @author 19649
* @date 2023/04/09 9:21
*/
public interface StudentService {
ReturnData queryAll();
ReturnData queryById(Integer empId);
ReturnData insert(Tblstudent tblstudent);
//修改信息
ReturnData update(Tblstudent tblstudent);
//更新状态(逻辑删除)
ReturnData delete(Integer sid);
}
service层的实现类
package com.yaokui.service.impl;/**
* 功能描述
*
* @author 19649
* @date 2023/04/09 9:22
*/
import com.yaokui.domain.Tblstudent;
import com.yaokui.mapper.StudentMapper;
import com.yaokui.service.StudentService;
import com.yaokui.util.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*@ClassName StudentServiceImpl
*@Description TODO
*@Author 19649
*@Date 2023/4/9 9:22
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public ReturnData queryAll() {
List<Tblstudent> tblstudents = studentMapper.findAll();
return new ReturnData(200,"successful",tblstudents);
}
@Override
public ReturnData queryById(Integer sid) {
Tblstudent tblstudent = studentMapper.findById(sid);
return new ReturnData(200,"successful",tblstudent);
}
@Override
public ReturnData insert(Tblstudent tblstudent) {
int i = studentMapper.insert(tblstudent);
System.out.println(i);
if(i>0){
return new ReturnData(200,"添加成功");
}else {
return new ReturnData(500,"添加失败");
}
}
@Override
public ReturnData update(Tblstudent tblstudent) {
int i = studentMapper.update(tblstudent);
//System.out.println(i);
if(i>0){
return new ReturnData(200,"修改成功");
}else {
return new ReturnData(500,"修改失败");
}
}
@Override
public ReturnData delete(Integer sid) {
int i = studentMapper.delete(sid);
if(i>0){
return new ReturnData(200,"删除成功");
}else {
return new ReturnData(500,"删除失败");
}
}
}
mapper层
package com.yaokui.mapper;/**
* 功能描述
*
* @author 19649
* @date 2023/04/09 9:19
*/
import com.yaokui.domain.Tblstudent;
import java.util.List;
/**
*@ClassName StudentMapper
*@Description TODO
*@Author 19649
*@Date 2023/4/9 9:19
*/
public interface StudentMapper {
//查询所有学生信息---包含班级信息
List<Tblstudent> findAll();
//根据id进行查询
Tblstudent findById(Integer sid);
//新增
int insert(Tblstudent tblstudent);
//修改
int update(Tblstudent tblstudent);
//更新状态(逻辑删除)
int delete(Integer sid);
}
domain实体
package com.yaokui.domain;
/**
* (Tblclass)实体类
*
* @author makejava
* @since 2023-04-09 09:11:22
*/
public class Tblclass {
private Integer cid;
private String cname;
private Integer capacity;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Integer getCapacity() {
return capacity;
}
public void setCapacity(Integer capacity) {
this.capacity = capacity;
}
@Override
public String toString() {
return "Tblclass{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", capacity=" + capacity +
'}';
}
}
package com.yaokui.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* (Tblstudent)实体类
*
* @author makejava
* @since 2023-04-09 09:12:53
*/
public class Tblstudent {
private Integer sid;
private String username;
private Integer age;
private String gender;
@DateTimeFormat(pattern="yyyy-MM-dd")//接收参数的格式化
@JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
private Date birthday;
private Integer cid;
private Tblclass tblclass;
public Tblclass getTblclass() {
return tblclass;
}
public void setTblclass(Tblclass tblclass) {
this.tblclass = tblclass;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Override
public String toString() {
return "Tblstudent{" +
"sid=" + sid +
", username='" + username + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", birthday=" + birthday +
", cid=" + cid +
", tblclass=" + tblclass +
'}';
}
}
util工具包
package com.yaokui.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ReturnData {
private Integer code;//状态码
private String msg;//消息
private Object data;//数据
public ReturnData(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
mapperxml文件
<?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.yaokui.mapper.StudentMapper">
<!--查询全部信息-->
<resultMap id="studentResultMap" type="com.yaokui.domain.Tblstudent" autoMapping="true">
<id property="sid" column="sid"/>
<id property="username" column="username"/>
<id property="age" column="age"/>
<id property="gender" column="gender"/>
<id property="birthday" column="birthday"/>
<association property="tblclass" javaType="com.yaokui.domain.Tblclass" autoMapping="true">
<id property="cid" column="cid"/>
<id property="cname" column="cname"/>
<id property="capacity" column="capacity"/>
</association>
</resultMap>
<select id="findAll" resultMap="studentResultMap">
select s.username, s.age, s.gender, s.birthday, c.cname, c.capacity
from tblstudent s
left join tblclass c
on s.cid=c.cid
where s.flag=0
</select>
<!--根据id进行查询-->
<select id="findById" resultType="com.yaokui.domain.Tblstudent">
select * from tblstudent where sid=#{sid}
</select>
<!--新增-->
<insert id="insert" parameterType="com.yaokui.domain.Tblstudent">
insert into tblstudent (username, age, gender, birthday, cid, flag)
values (#{username}, #{age}, #{gender}, #{birthday},#{cid}, 0);
</insert>
<!--修改-->
<update id="update" parameterType="com.yaokui.domain.Tblstudent">
update tblstudent
<set>
<if test="username!=null and username!=''">
username=#{username},
</if>
<if test="age!=null and age!=''">
age=#{age},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
<if test="birthday!=null and birthday!=''">
birthday=#{birthday}
</if>
</set>
where sid=#{sid}
</update>
<!--删除-->
<update id="delete" parameterType="int">
update tblstudent set flag =1 where sid=#{sid}
</update>
</mapper>
pringmvc.xml配置文件
<?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"
xmlns:tx="http://www.alibaba.com/schema/stat"
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
http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--包扫描-->
<context:component-scan base-package="com.yaokui"/>
<!--数据源-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--mysql8.0以后一定要加时区-->
<property name="url" value="jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<!--sqlSessionFactory工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="mapperLocations" value="classpath:com/yaokui/mapper/*.xml"/>
</bean>
<!--dao接口的扫描类 为接口生成代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<property name="basePackage" value="com.yaokui.mapper"/>
</bean>
<!--事务管理类-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
<!--开启事务注解驱动-->
<tx:annotation-driven/>
<!--注解驱动-->
<mvc:annotation-driven/>
<!--静态资源放行-->
<mvc:default-servlet-handler/>
<!--视图解析器:前后端分离 返回给前端都是json数据 -->
</beans>
web.xml配置文件
<?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">
<!--加载springmvc配置文件-->
<servlet>
<servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<!–加载spring配置文件–>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
-->
<!--解决中文乱码问题-->
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
pom文件
<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>org.example</groupId>
<artifactId>qy163ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--spring整合orm框架需要的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--切面aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--spring和mybatis整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--druid依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
</dependency>
</dependencies>
</project>