SSM框架学习-SSM整合(整合配置、功能模块开发、接口测试)

1. 整合配置

在这里插入图片描述

首先创建maven模块,导入相应的坐标

  <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>
  <build>
    <finalName>springmvc_08_ssm</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>80</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

然后创建相应的包结构

在这里插入图片描述

整合Mybatis内容参考https://blog.csdn.net/qq_45681515/article/details/130609553?spm=1001.2014.3001.5502

整合mvc内容参考https://blog.csdn.net/qq_45681515/article/details/130768992?spm=1001.2014.3001.5502
https://blog.csdn.net/qq_45681515/article/details/130820654?spm=1001.2014.3001.5502

注释参考
@Import注释,并将JdbcConfig类和MyBatisConfig类引入当前配置类中。这样可以在SpringConfig中访问JdbcConfig和MyBatisConfig中定义的任何bean或方法定义;
在配置类上使用了@EnableWebMvc注释。这意味着应用程序将使用Spring MVC来处理Web请求,并启用了MVC Java配置支持。Spring MVC将自动注册注解处理器、消息转换器和其他必要的组件,以便可以处理controller类方法上的注解@RequestMapping

2. 功能模块开发

在这里插入图片描述
在这里插入图片描述

请求参数、映射路径、REST快速开发等内容参考
https://blog.csdn.net/qq_45681515/article/details/130823967?spm=1001.2014.3001.5502
https://blog.csdn.net/qq_45681515/article/details/130882852?spm=1001.2014.3001.5502

问题一:在service的实现类中自动注入bookDao报错在这里插入图片描述
这是因为dao目录下的文件采用了自动代理,并没有定义为bean,且idea此处报错并不是真的出现了错误

3. 接口测试

首先使用Junit对service层进行测试
参考:https://blog.csdn.net/qq_45681515/article/details/130618999?spm=1001.2014.3001.5502

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

    @Autowired
    private BookService bookService;

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

    @Test
    public void testGetAll(){
        List<Book> all = bookService.getAll();
        System.out.println(all);
    }
}

其次对controller层进行测试;
采用postman发送请求,示例save()操作;
启动Tomcat,然后咋子postman里面发送请求;
最后查看数据库表单是否更新

在这里插入图片描述

最后数据库实现了数据更新;
这里插入的数据为null是因为在BooKController中,save方法的参数中没有加@RequestBody,而@RequestBody主要用来接收前端传递给后端的json字符串中的数据的,所以没写之前得到是null

在这里插入图片描述

最后加入Spring事务,具体内容参考:
https://blog.csdn.net/qq_45681515/article/details/130766093?spm=1001.2014.3001.5502

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值