哈士奇发布SSM整合【纯注解配置版】

通过以下步骤可以成功整合SSM的纯注解配置版哦~

1、整合SSM步骤

1.1、创建项目,配置基本依赖

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.hashiqi</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>


    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-maven</id>
            <name>aliyunmaven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </pluginRepository>
    </pluginRepositories>

</project>

导入依赖后,创建相应的java、resources和跟resources同级的webapp,webapp下面有WEB-INF,在WEB-INF里有web.xml。

1.2、创建相关初始包

1.2.1、config配置包【配置模块】

在config中依次创建JdbcConfig、MyBatisConfig、SpringConfig、ServletConfig、SpringMvcConfig类

JdbcConfig类

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public 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 dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

MyBatisConfig类

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.hashiqi.pojo");
        return factoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.hashiqi.mapper");
        return msc;
    }
}

SpringConfig类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan({"com.hashiqi.service"})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {
}

ServletConfig类

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public 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类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("com.hashiqi.controller")
@EnableWebMvc
public class SpringMvcConfig {
}

resources下的jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=123456

1.2.2、非配置包【功能包】

controller控制层

import com.hashiqi.pojo.Book;
import com.hashiqi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public boolean save(@RequestBody Book book) {
        return bookService.save(book);
    }

    @PutMapping
    public boolean update(@RequestBody Book book) {
        return bookService.update(book);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        return bookService.delete(id);
    }

    @GetMapping("/{id}")
    public Book getById(@PathVariable("id") Integer id) {
        return bookService.getById(id);
    }

    @GetMapping
    public List<Book> getAll() {
        return bookService.getAll();
    }
}

mapper持久层

import com.hashiqi.pojo.Book;
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 BookMapper {

    //    @Insert("insert into ssm_db.tbl_book values (null, #{type}, #{name}, #{description})")
    @Insert("INSERT INTO ssm_db.tbl_book (type, name, description) VALUES (#{type}, #{name}, #{description})")
    public void save(Book book);

    @Update("UPDATE ssm_db.tbl_book SET type = #{type}, name = #{name}, description = #{description} WHERE id = #{id}")
    public void update(Book book);

    @Delete("DELETE FROM ssm_db.tbl_book WHERE id = #{id}")
    public void delete(Integer id);

    @Select("SELECT * FROM ssm_db.tbl_book WHERE id = #{id}")
    public Book getById(Integer id);

    @Select("SELECT * FROM ssm_db.tbl_book")
    public List<Book> getAll();
}

pojo实体类【这里可以使用lombok:要引入lombok依赖】

public class Book {

    private Integer id;
    private String type;
    private String name;
    private String description;

    public Book() {
    }

    public Book(String type, String name, String description) {
        this.type = type;
        this.name = name;
        this.description = description;
    }

    public Book(Integer id, String type, String name, String description) {
        this.id = id;
        this.type = type;
        this.name = name;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

service业务层和service.impl业务实现层

import com.hashiqi.pojo.Book;

import java.util.List;

public interface BookService {

    /**
     * 保存
     * @param book 书
     * @return 布尔值
     */
    public boolean save(Book book);

    /**
     * 更新
     * @param book 书
     * @return 布尔值
     */
    public boolean update(Book book);

    /**
     * 根据id删除
     * @param id 书id
     * @return 布尔值
     */
    public boolean delete(Integer id);

    /**
     * 根据id获取
     * @param id 书id
     * @return 书
     */
    public Book getById(Integer id);

    /**
     * 获取全部书
     * @return 全部书
     */
    public List<Book> getAll();
}
import com.hashiqi.mapper.BookMapper;
import com.hashiqi.pojo.Book;
import com.hashiqi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public boolean save(Book book) {
        bookMapper.save(book);
        return true;
    }

    @Override
    public boolean update(Book book) {
        bookMapper.update(book);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        bookMapper.delete(id);
        return true;
    }

    @Override
    public Book getById(Integer id) {
        return bookMapper.getById(id);
    }

    @Override
    public List<Book> getAll() {
        return bookMapper.getAll();
    }
}

1.3、测试

在测试test中创建以下测试类

package com.hashiqi.service;

import com.hashiqi.config.SpringConfig;
import com.hashiqi.pojo.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void testGetById() {
        Book book = bookService.getById(1);
        System.out.println(book);
    }
}

此测试类成功,即可开始测试控制层,控制层直接配置Tomcat及里面的访问路径,通过网页即可测试即可。

最后尝试,可以运行~~

1.4、配置事务注解

在springConfig配置类加上

@EnableTransactionManagement

在JdbcConfig配置类中加上

@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
    DataSourceTransactionManager ds = new DataSourceTransactionManager();
    ds.setDataSource(dataSource);
    return ds;
}

在业务层上

业务接口类上添加注解

@Transactional

1.5、乱码处理

在ServletConfig配置类中加入

// 乱码处理
@Override
protected Filter[] getServletFilters() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    return new Filter[]{filter};
}

1.6、表现层与前端数据传输协议统一

首先,创建返回统一结果集类

public class Result {

    private Object data;
    private Integer code;
    private String msg;

    public Result() {
    }

    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;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

接着,创建常量类,用于统一结果集中的code属性赋值

public class CodeConstants {

    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 SAVE_ERROR = 20010;
    public static final Integer DELETE_ERROR = 20020;
    public static final Integer UPDATE_ERROR = 20030;
    public static final Integer GET_ERROR = 20040;
}

再者,修改控制层返回类型为Result

import com.hashiqi.constant.CodeConstants;
import com.hashiqi.dto.Result;
import com.hashiqi.pojo.Book;
import com.hashiqi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag, flag ? CodeConstants.SAVE_OK : CodeConstants.SAVE_ERROR);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(flag, flag ? CodeConstants.UPDATE_OK : CodeConstants.UPDATE_ERROR);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = bookService.delete(id);
        return new Result(flag, flag ? CodeConstants.DELETE_OK : CodeConstants.DELETE_ERROR);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        Book book = bookService.getById(id);
        Integer code = book != null ? CodeConstants.GET_OK : CodeConstants.GET_ERROR;
        String msg = book != null ? null : "数据查询失败,请重试";
        return new Result(book, code, msg);
    }

    @GetMapping
    public Result getAll() {
        List<Book> bookList = bookService.getAll();
        Integer code = bookList != null ? CodeConstants.GET_OK : CodeConstants.GET_ERROR;
        String msg = bookList != null ? null : "数据查询失败,请重试";
        return new Result(bookList, code, msg);
    }
}

1.7、异常处理器

集中、统一的处理项目中出现的异常

  • 出现异常现象的常见位置与常见诱因

    • 框架内部抛出的异常:因使用不合规导致

    • 数据层抛出的异常:因外部服务器故障导致(比如:服务器访问超时)

    • 业务层抛出的异常:因业务逻辑书写错误导致(比如:遍历业务书写操作,导致索引异常等)

    • 表现层抛出的异常:因数据收集、校验等规则导致(比如:数据类型不匹配导致异常)

    • 工具类抛出的异常:因工具类书写不严谨不够健壮导致(比如:必要释放的连接长期未释放)

  • 各个层级均出现异常,异常处理代码书写在哪一层

    • 所有的异常均抛出到表现层进行处理

  • 表现层处理异常,每个方法中单独书写,代码书写量巨大且意义不强,如何解决

    • AOP思想

在controller包下创建该类

因为SpringMvcConfig配置类中已经通过@ComponentScan进行扫描controller包了。此方法可直接将抛出的异常返回的前端

import com.hashiqi.dto.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex) {
        return new Result(null, 404, "哈士奇,别跑了,我掉坑里了");
    }
}

1.8、项目异常处理方案

  • 项目异常分类

    • 业务异常

      • 规范的用户行为产生的异常

      • 不规范的用户行为操作产生的异常

    • 系统异常

      • 项目运行过程中可预计且无法避免的异常

      • 解决:发送特定消息给运维人员、提醒维护,记录日志

    • 其它异常

      • 未预期到的异常

      • 解决:发送特定消息给编程人员、提醒维护(纳入预期范围内)、记录日志

  • 项目异常处理方案

    • 业务异常

      • 发送对应消息传递给用户,提醒规范操作

    • 系统异常

      • 发送对应消息传递给用户,安抚用户

      • 发送特定消息给运维人员,提醒维护

      • 记录日志

    • 其它异常

      • 发送固定消息传递给用户,安抚用户

      • 发送特定消息给编程人员,提醒维护(纳入预期范围内)

      • 记录日志

在项目中创建exception包

在该包下创建业务异常类和系统异常类

public class BusinessException extends RuntimeException {

    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public BusinessException(String message, Integer code, Throwable cause) {
        super(message, cause);
        this.code = code;
    }
}
public class SystemException extends RuntimeException {

    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public SystemException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public SystemException(String message, Integer code, Throwable cause) {
        super(message, cause);
        this.code = code;
    }
}

修改全局异常处理类ExceptionAdvice

import com.hashiqi.constant.CodeConstants;
import com.hashiqi.dto.Result;
import com.hashiqi.exception.BusinessException;
import com.hashiqi.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionAdvice {


    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex) {
        // 记录日志

        // 发送消息给运维

        // 发送邮件给开发人员,ex对象发送给开发人员
        return new Result(null, ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doSystemException(BusinessException ex) {
        return new Result(null, ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex) {
        // 记录日志

        // 发送消息给运维

        // 发送邮件给开发人员,ex对象发送给开发人员
        return new Result(null, CodeConstants.SYSTEM_UNKNOWN_ERROR, "系统繁忙,请稍后再试");
    }
}

修改code赋值的常量类

public class CodeConstants {

    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 SAVE_ERROR = 20010;
    public static final Integer DELETE_ERROR = 20020;
    public static final Integer UPDATE_ERROR = 20030;
    public static final Integer GET_ERROR = 20040;

    public static final Integer SYSTEM_ERROR = 50001;
    public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;
    public static final Integer BUSINESS_ERROR = 60001;

    public static final Integer SYSTEM_UNKNOWN_ERROR = 59999;
}

在业务实现层进行演示异常

@Override
public Book getById(Integer id) {
    if (id == 1) {
        throw new BusinessException("哈士奇不要再搞了", CodeConstants.BUSINESS_ERROR);
    }
    // 将可能出现的异常进行包装,转换成自定义异常
    try {
        int i = 1 / 0;
    } catch (Exception e) {
        throw new SystemException("服务器访问超时,请重试", CodeConstants.SYSTEM_TIMEOUT_ERROR, e);
    }

    return bookMapper.getById(id);
}

1.9、前后端进行联调

因为前端有静态资源,所以要将资源进行开放。资源不必放到WEB-INF下面

在config配置类下创建SpringMvcSupport

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
    }
}

创建成功后,将其加入到SpringMvcConfig的注解中进行扫描

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan({"com.hashiqi.controller", "com.hashiqi.config"})
@EnableWebMvc
public class SpringMvcConfig {
}

感谢~ 到这里就可以去实现你的前端页面,通过异步技术Axios【封装ajax】实现前后端联调。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值