ssm_单表增删改查小案例
文章目录
- 1.环境搭建
- 2.整体思路
- 3.测试
1.环境搭建
跳转链接
导入文件
- pom
<properties><java.version>1.8</java.version></properties><dependencies><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>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></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>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.1.5</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
- application.properties
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Hongkong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#开启驼峰命名规则
mybatis.configuration.map-underscore-to-camel-case=true
- log4j.properties
### 设置###
# debug 日志级别,常用的4个日志级别:ERROR、WARN、 INFO、DEBUG
log4j.rootLogger = debug,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
- 整体目录
2.整体思路
-
controller : 接收页面传输的数据,也可以将服务器的数据响应给页面
- 如何接收页面数据
- 如何响应数据给页面
-
service :代码层的业务逻辑书写
-
mapper :保存数据到数据库、也可以从数据库查询数据
- 如何修改数据(增删改)
- 如何查询数据
三者结合
- 第一步环境搭建
- 使用postman或页面发送数据给controller ,新增用户
- 在controller中去调用service的新增用户方法
- 在service中调用mapper的新增方法,将前端传过来的用户数据保存到数据库
- mapper将数据保存结果告诉service
- service根据响应结果,将处理的结果告诉controller
- controller响应结果给页面或postman
GET 查询
POST 增
PUT 改
DELETE 删
- Controller
package com.czxy.ssm001.controller;import com.czxy.ssm001.model.User;
import com.czxy.ssm001.service.User1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/*** @RequestMapping("/user1") 指定controller对应的访问路径 localhost:8080/user1* @RestController 相当于 @Controller 和@ResponseBody*/
@RestController
@RequestMapping("/user1")
public class User1Controller {@Autowiredprivate User1Service user1Service;/*** 添加*/@PostMappingpublic void add(@RequestBody User user) {user1Service.add(user);}/*** 修改*/@PutMappingpublic void update(@RequestBody User user) {user1Service.update(user);}/*** 删除*/@DeleteMappingpublic void delete(Integer id) {user1Service.delete(id);}/*** 查询*/@GetMappingpublic User findById(Integer id) {return user1Service.findById(id);}
}
- Mapper
package com.czxy.ssm001.mapper;import com.czxy.ssm001.model.User;
import tk.mybatis.mapper.common.Mapper;@org.apache.ibatis.annotations.Mapper//注解的mapper
public interface User1Mapper extends Mapper<User> {//Mapper自带 增 删 改 查//缺点是只支持单表操作 不支持多表操作 多表需要自己创建方法
}
- Service
package com.czxy.ssm001.service;import com.czxy.ssm001.model.User;
//Service 接口
public interface User1Service {/*** 新增用户*/void add(User user);/*** 修改用户*/void update(User user);/*** 删除用户*/void delete(Integer id);/*** 根据id查询用户*/User findById(Integer id);
}
- impl包下实现service接口
package com.czxy.ssm001.service.impl;import com.czxy.ssm001.mapper.User1Mapper;
import com.czxy.ssm001.model.User;
import com.czxy.ssm001.service.User1Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/*** @Service 表示给类是一个业务类,并交由spring管理* IoC 创建一个User1Service 的对象,并放到IoC 容器中,称之为bean 类型、名称* @Resource 按照名称注入* @Autowired 按照类型注入*/
@Service
@Transactional
public class User1ServiceImpl implements User1Service {@Resourceprivate User1Mapper user1Mapper;/*** 新增用户* insertSelective :只给有值的字段赋值(会对传进来的值做非空判断)* insert :所有的字段都会添加一遍,即使有的字段没有值*/@Overridepublic void add(User user) {user1Mapper.insert(user);}/*** 修改*/@Overridepublic void update(User user) {// updateByPrimaryKey:没有Selective :会更新全部属性//updateByPrimaryKeySelective : 只更新有值得属性user1Mapper.updateByPrimaryKeySelective(user);}/*** 删除*/@Overridepublic void delete(Integer id) {user1Mapper.deleteByPrimaryKey(id);}/*** 查询*/@Overridepublic User findById(Integer id) {return user1Mapper.selectByPrimaryKey(id);}
}
3.测试
- Application
package com.czxy.ssm001;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Ssm001Application {public static void main(String[] args) {SpringApplication.run(Ssm001Application.class, args);}}
使用 Postman
- 增
- 改
- 删
- 查
ssm_单表增删改查小案例
文章目录
- 1.环境搭建
- 2.整体思路
- 3.测试
1.环境搭建
跳转链接
导入文件
- pom
<properties><java.version>1.8</java.version></properties><dependencies><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>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></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>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.1.5</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
- application.properties
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Hongkong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#开启驼峰命名规则
mybatis.configuration.map-underscore-to-camel-case=true
- log4j.properties
### 设置###
# debug 日志级别,常用的4个日志级别:ERROR、WARN、 INFO、DEBUG
log4j.rootLogger = debug,stdout### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
- 整体目录
2.整体思路
-
controller : 接收页面传输的数据,也可以将服务器的数据响应给页面
- 如何接收页面数据
- 如何响应数据给页面
-
service :代码层的业务逻辑书写
-
mapper :保存数据到数据库、也可以从数据库查询数据
- 如何修改数据(增删改)
- 如何查询数据
三者结合
- 第一步环境搭建
- 使用postman或页面发送数据给controller ,新增用户
- 在controller中去调用service的新增用户方法
- 在service中调用mapper的新增方法,将前端传过来的用户数据保存到数据库
- mapper将数据保存结果告诉service
- service根据响应结果,将处理的结果告诉controller
- controller响应结果给页面或postman
GET 查询
POST 增
PUT 改
DELETE 删
- Controller
package com.czxy.ssm001.controller;import com.czxy.ssm001.model.User;
import com.czxy.ssm001.service.User1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;/*** @RequestMapping("/user1") 指定controller对应的访问路径 localhost:8080/user1* @RestController 相当于 @Controller 和@ResponseBody*/
@RestController
@RequestMapping("/user1")
public class User1Controller {@Autowiredprivate User1Service user1Service;/*** 添加*/@PostMappingpublic void add(@RequestBody User user) {user1Service.add(user);}/*** 修改*/@PutMappingpublic void update(@RequestBody User user) {user1Service.update(user);}/*** 删除*/@DeleteMappingpublic void delete(Integer id) {user1Service.delete(id);}/*** 查询*/@GetMappingpublic User findById(Integer id) {return user1Service.findById(id);}
}
- Mapper
package com.czxy.ssm001.mapper;import com.czxy.ssm001.model.User;
import tk.mybatis.mapper.common.Mapper;@org.apache.ibatis.annotations.Mapper//注解的mapper
public interface User1Mapper extends Mapper<User> {//Mapper自带 增 删 改 查//缺点是只支持单表操作 不支持多表操作 多表需要自己创建方法
}
- Service
package com.czxy.ssm001.service;import com.czxy.ssm001.model.User;
//Service 接口
public interface User1Service {/*** 新增用户*/void add(User user);/*** 修改用户*/void update(User user);/*** 删除用户*/void delete(Integer id);/*** 根据id查询用户*/User findById(Integer id);
}
- impl包下实现service接口
package com.czxy.ssm001.service.impl;import com.czxy.ssm001.mapper.User1Mapper;
import com.czxy.ssm001.model.User;
import com.czxy.ssm001.service.User1Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;/*** @Service 表示给类是一个业务类,并交由spring管理* IoC 创建一个User1Service 的对象,并放到IoC 容器中,称之为bean 类型、名称* @Resource 按照名称注入* @Autowired 按照类型注入*/
@Service
@Transactional
public class User1ServiceImpl implements User1Service {@Resourceprivate User1Mapper user1Mapper;/*** 新增用户* insertSelective :只给有值的字段赋值(会对传进来的值做非空判断)* insert :所有的字段都会添加一遍,即使有的字段没有值*/@Overridepublic void add(User user) {user1Mapper.insert(user);}/*** 修改*/@Overridepublic void update(User user) {// updateByPrimaryKey:没有Selective :会更新全部属性//updateByPrimaryKeySelective : 只更新有值得属性user1Mapper.updateByPrimaryKeySelective(user);}/*** 删除*/@Overridepublic void delete(Integer id) {user1Mapper.deleteByPrimaryKey(id);}/*** 查询*/@Overridepublic User findById(Integer id) {return user1Mapper.selectByPrimaryKey(id);}
}
3.测试
- Application
package com.czxy.ssm001;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Ssm001Application {public static void main(String[] args) {SpringApplication.run(Ssm001Application.class, args);}}
使用 Postman
- 增
- 改
- 删
- 查
发布评论