文章目录
1. 介绍
1.1 概念介绍
SSM项目是指
基于Spring+SpringMVC+MyBatis框架
搭建的Java Web项目。
- Spring是负责
管理和组织项目
的IOC容器和AOP功能- SpringMVC是一个轻量级的
MVC框架
,用于处理Web请求和响应- MyBatis是一种
持久化框架
,用于进行数据库操作的ORM框架。
学习笔记:
Spring:https://blog.csdn.net/meini32/article/details/132474555
SpringMVC:https://blog.csdn.net/meini32/article/details/132545058
Mybatis:https://blog.csdn.net/meini32/article/details/132068955
Mybatis商品增删改查案例:https://blog.csdn.net/meini32/article/details/132095237
2 SSM整合框架
关键内容
Spring
- SpringConfig
SpringMVC
- SpringMvcConfig
- ServletConfig
Mybatis
- MyBatisConfig
- JdbcConfig
- jdbc.properties
MyBatis
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/db1jdbc.username=rootjdbc.password=123456
package com.it.config;import javax.sql.DataSource;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;//jdbcConfigpublic class jdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; //数据源 @Bean public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; }}-----------------------------------------------------package com.it.config;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.mapper.MapperScannerConfigurer;import org.springframework.context.annotation.Bean;import javax.sql.DataSource;//MyBatisConfigpublic class MybatisConfig { //创建和管理数据库会话 @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.it.domain"); return factoryBean; } @Bean //创建并配置MapperScannerConfigurer对象 public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.it.dao"); return mapperScannerConfigurer; }}
web容器配置类
SpringMVC
- SpringMvcConfig
- ServletConfig
//ServletConfig@Configurationpublic class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; }}----------------------------------------------------------------------//SpringMvcConfig@Configuration@ComponentScan("com.it.controller")@EnableWebMvcpublic class SpringMvcConfig {}
Spring
- SpringConfig
//整合Spring应用程序,实现对组件和属性的管理和使用@Configuration@ComponentScan({"com.it.service"})@PropertySource("jdbc.properties")@Import({jdbcConfig.class, MybatisConfig.class})public class SpringConfig {}
3. SSM功能模块开发
内容
- 表与实体类;
- dao(接口+自动代理)
- service(接口+实现类)
- controller
添加相关依赖
<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.0modelVersion> <groupId>org.examplegroupId> <artifactId>spring-ssmartifactId> <packaging>warpackaging> <version>1.0-SNAPSHOTversion> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.2.9.RELEASEversion> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.9.0version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.5version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>1.3.0version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.32version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.3.13version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>5.3.13version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.2.6version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.28version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.1.0version> <scope>providedscope> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.mavengroupId> <artifactId>tomcat7-maven-pluginartifactId> <version>2.2version> <configuration> <port>81port> <path>path> configuration> plugin> plugins> build>project>
import lombok.Data;@Datapublic class User { private int id; private String username; private String password;}------------------------------------------------------------------------package com.it.dao;import com.it.domain.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;//用户增删改查接口public interface UserDao { @Insert("INSERT INTO tb_user values (null,#{name},#{password}}})") public void save(User user); @Update("UPDATE tb_user set name=#{id},password=#{password} where id=#{id}}") public void update(User user); @Delete("DELETE from tb_user where id = #{id}}") public void delete(Integer id); @Select("SELECT * FROM tb_user") public List<User> selectAll(); @Select("SELECT * FROM tb_user WHERE id=#{id}") public User selectById(Integer id);}----------------------------------------------------------------------package com.it.service;import com.it.domain.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.util.List;public interface UserService { public boolean save(User user); public boolean update(User user); public boolean delete(Integer id); public List<User> selectAll(); public User selectById(Integer id);}---------------------------------------------------------------------------package com.it.service.impl;import com.it.dao.UserDao;import com.it.domain.User;import com.it.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService { @Autowired //自动装配 private UserDao userDao; public boolean save(User user) { userDao.save(user); return true; } public boolean update(User user) { userDao.update(user); return true; } public boolean delete(Integer id) { userDao.delete(id); return true; } public List<User> selectAll() { return userDao.selectAll(); } public User selectById(Integer id) { return userDao.selectById(id); }}------------------------------------------------------------------------package com.it.controller;import com.it.domain.User;import com.it.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @PostMapping public boolean save(@RequestBody User user) { return userService.save(user); } @PutMapping public boolean update(@RequestBody User user) { return userService.update(user); } @DeleteMapping("/{id}") public boolean delete(@PathVariable Integer id) { System.out.println(userService.delete(id)); return userService.delete(id); } @GetMapping public List<User> selectAll() { System.out.println(userService.selectAll()); return userService.selectAll(); } @GetMapping("/{id}") public User selectById(@PathVariable Integer id) { return userService.selectById(id); }}
4 测试
4.1 业务层接口测试
4.2 表现层接口测试
5.优化 -表现层数据封装
- 前端接受数据格式封装到code属性用于区分操作
- 将特殊消息封装到message(msg)属性中
public class Result{private Object data;private Integer code;private String msg;}
package com.it.controller;public class Code { //成功 public static final Integer SAVE_OK = 20011; public static final Integer DELETE_OK = 20021; public static final Integer UPDATE_OK = 20031; public static final Integer GET_OK = 20041; public static final Integer PUT_OK = 20051; //失败 public static final Integer SAVE_ERR = 20010; public static final Integer DELETE_ERR = 20020; public static final Integer UPDATE_ERR = 20030; public static final Integer GET_ERR = 20040; public static final Integer PUT_ERR = 20050;}----------------------------------------------------------------------------package com.it.controller;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Datapublic class Result { private Object data; private Integer code; private String msg; public Result(Integer code, String msg) { this.code = code; this.msg = msg; } public Result(Object data, Integer code) { this.data = data; this.code = code; } public Result(Object data, Integer code, String msg) { this.data = data; this.code = code; this.msg = msg; }}---------------------------------------------------------------------------import java.util.List;@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @PostMapping public Result save(@RequestBody User user) { boolean save = userService.save(user); return new Result(save,save?Code.SAVE_OK:Code.SAVE_ERR); }
6.异常处理
出现异常位置及原因
- 框架内部抛出异常:不合规使用
- 数据层抛出异常:外边服务器故障(超时访问)
- 业务层抛出异常:业务逻辑书写错误(遍历业务书写操作等)
- 表现层抛出异常:数据收集、校验等规则导致
- 工具类抛出异常:工具书写不严谨不健壮
项目异常分类
- 业务异常
- 原因:用户行为导致
- 解决:发送相应的信息传递给用户,提醒规范操作。
- 系统异常
- 原因:项目运行时可预计但无法避免的异常
- 解决:发送固定消息安抚用户;发送消息给处理人员;记录日志;
- 其他异常
- 原因:未预期的异常
- 解决:同上
解决:集中统一处理项目中出现的异常(异常处理器)
@RestControllerAdvice
作用:为Rest风格开发的控制器类做增强
.
@ExceptionHandler
作用:位于方法上方,设置指定异常处理方案,出现异常后终止原始控制器,转入当前方法执行。
package com.it.controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ProjectExceptionAdvice { @ExceptionHandler(Exception.class) public Result doException(Exception e){ System.out.println("出现异常"); return new Result(500,"出错"); }}
步骤
- 自定义项目系统级异常
- 自定义业务级异常;
- 自定义异常编码;
- 触发自定义异常(模拟异常操作);
- 拦截异常并处理异常;
- 测试结果
package com.it.controller;import lombok.Data;//自定义项目级异常@Datapublic class SystemException extends RuntimeException{ private Integer code; public SystemException(String message, Throwable cause, Integer code) { super(message); this.code = code; } }
package com.it.controller;import lombok.Data;//自定义项目业务级异常@Datapublic class BusinessException extends RuntimeException{ private Integer code; public BusinessException(String message, Integer code) { super(message); this.code = code; } public BusinessException(String message, Throwable cause, Integer code) { super(message,cause); this.code = code; }}
//异常编码 public static final Integer SYSTEM_UNKNOW_ERROR = 50001; public static final Integer SYSTEM_TIMEOUT_ERROR = 50002; public static final Integer PROJECT_VALIDATE_ERROR = 60001; public static final Integer PROJECT_BUSINESS_ERROR = 60002;
package com.it.service.impl;import com.it.controller.BusinessException;import com.it.controller.Code;import com.it.dao.UserDao;import com.it.domain.User;import com.it.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService { @Autowired //自动装配 private UserDao userDao; //自定义异常 public User selectById(Integer id) { if(id<0){ throw new BusinessException("请输入正确的id",Code.PROJECT_BUSINESS_ERROR); } return userDao.selectById(id); }}
package com.it.controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.bind.annotation.RestControllerAdvice;//拦截并处理异常@RestControllerAdvicepublic class ProjectExceptionAdvice { @ExceptionHandler(Exception.class) public Result doException(Exception e){ System.out.println("出现异常"); return new Result(500,"出错"); } @ExceptionHandler(BusinessException.class) public Result doBussinessException(Exception e){ System.out.println("出现业务异常"); return new Result(501,null,"出错"); } @ExceptionHandler(SystemException.class) public Result doSystemException(Exception e){ System.out.println("出现系统异常"); return new Result(500,"出错"); }}
测试结果
来源地址:https://blog.csdn.net/meini32/article/details/132569931