一、ssm开发是通过整合spring,springmvc,MyBatis实现的,
先通过Spring和MyBatis整合,获取dao层以及service层。再通过SpringMvc实现controller实现业务的增删改查。
二、前期准备:配置核心配置类
①、先配置连接数据库,jdbc通过@PropertySource("classpath:jdbc.properties"),同时配置开启事务的类
@Bean //平台事务管理员,参数需要数据源 public PlatformTransactionManager platformTransactionManager(DataSource dataSource){ //创建平台事务管理员 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager; }
②、MyBatisConfig配置两个类;
@Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.pojo"); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; }
③、Spring的核心配置类
@Configuration @ComponentScan("com.itheima.service") @Import({JdbcConfig.class, MyBatisConfig.class}) //开启事务自动 @EnableTransactionManagement public class SpringConfig { }
三、配置SpringMvc类
①、配置连接spring与SpringMvc同时拦截所有的类
//设置拦截器通过ctr+o查看所有要重写的方法 public class ServletConfigInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { //这是配置SSM(Spring SpringMvc Mybatis)的主配置 @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class}; } //配置MVC的主配置 @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMVCConfig.class}; } //配置所有拦截 @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
②、配置Springmvc核心配置
@Configuration @ComponentScan({"com.itheima.controller","com.itheima.config","com.itheima.exception"}) @EnableWebMvc public class SpringMVCConfig { }
③、设置通过不拦截静态资源
@Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { //前面一个是浏览器资源位置,后面一个是真实资源位置 registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/js/**").addResourceLocations("/js/"); registry.addResourceHandler("/pages/**").addResourceLocations("/pages/"); registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/"); }
四、核心配置完成后后面就是增删改查了
这里着重说下异常的处理,我们在写代码的时候每一层都可以出现错误,我们可以层层往外抛错误,都统一抛到controller层处理。
查询逻辑是从 dao -> service ->controller这种顺序来的,我们可以在controller来进行处理,通过aop思想对controller进行增强处理。
五、异常处理
在正常情况下我们都是要处理异常的不能直接给用户看到如404,500等等异常信息,需要展示更加友好的提示信息如:“系统繁忙请稍后再试!”等等我们给定的,这样可以较好安抚用户情绪。
①、异常说明:有些异常是用户输入有问题,有些是本身有异常都要进行处理。当然能够对具体错误做具体处理是最好的,但有时也是只能抓个大概
这里定义异常通常有报错码和信息,信息继承自底层
@Getter public class LeadNewsException extends RuntimeException{ private Integer code; public LeadNewsException(String msg,Integer code){ super(msg); this.code=code; } public LeadNewsException(AppHttpCodeEnum a){ super(a.getErrorMessage()); this.code=a.getCode(); } }
②、作为最大异常Exception处理。
@RestControllerAdvice //返回JSON数据 public class ProjectExceptionAdvice{ //表示用这个方法来 处理控制器中的Exception 类型的异常 @ExceptionHandler(Exception.class) public Result doException(Exception e){ return new Result(null, Code.SYSTEM_ERR,"服务器繁忙,请稍后再试!"); } @ExceptionHandler(BusinessException.class) public Result doBusinessException(BusinessException e){ return new Result(null,e.getCode(),e.getMessage()); }
这里返回的是自已定义的类型可以统一风格主要有返回数值:data,返回码:code,以及返回信息:msg 即返回JSON类型的 [data:{},code:{},msg{}].
③、对具体的异常进行处理
先定义异常的类型
public class BusinessException extends RuntimeException{ //保存异常状态码 private int code; public BusinessException(String message, int code) { super(message); this.code = code; } }
在各层中可以抛出定义的异常
throw new BusinessException ("提示信息",自定义的报错码,在上面的实体类中)