测试SSM框架环境是否搭建成功

在上一章我们已经完成了对于SSM框架的基本配置,接下来我们对它进行测试,看是否已经成功搭建成功。

1.框架结构

下面是我们框架的基本结构
在这里插入图片描述

2.创建数据库表

在这里插入图片描述
我们数据库已经创建完成,使用IDE链接mysql数据库测试一下
在这里插入图片描述

实体类

接下来我们创建实体类实现Serializable的接口

生成它们的get和set方法,tostring方法

service方法

首先创建一个service的接口,里面编写我们需要用要的方法名在这里插入图片描述

之后写一个service的类,实现AccountService接口

在这里插入图片描述

sql语句, 这边用到的是语句注入的方式


在这里插入图片描述

package com.lx.dao;

import com.lx.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
//        mybatis的注解开发
public interface IAccount {

        @Select("select * from account")
        public List<Account> findAll();
        @Insert("insert into account (name,money) value(#{name},#{money})")
        public void saveAccount(Account account);

}

之后在AccountService里面使用sql接口,使用查询方法

在这里插入图片描述

package com.lx.service;

import com.lx.dao.IAccountDao;
import com.lx.domain.Account;
import com.lx.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service("accountService")
public class AccountService  implements AccountServiceImpl {

    @Autowired
    public IAccountDao iAccountDao;


    @Override
    public List<Account> findAll() {
        System.out.println("查询");
        return iAccountDao.findAll();
    }

    @Override
    public void saveAccount(Account account) {
        System.out.println("111");
        iAccountDao.saveAccount(account);
    }
}

此时大概框架如下

在这里插入图片描述

此时我们的项目环境已经搭建完成,可以创建Test类进行测试,是否能插入数据库中。基本完成

测试spring环境

创建Testspring类

package com.lx.test;

import com.lx.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void run1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        AccountService as = (AccountService) ac.getBean("accountService");
        as.findAll();
    }
}

运行run1进行测试,是否搭建spring环境

测试TestMybatis环境


package com.lx.test;

import com.lx.dao.IAccountdao;
import com.lx.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestMyBatis {

    @Test
    public void run1() throws IOException {
        Account account =new Account();
        account.setName("支付");
        account.setMoney(200d);
        // 加载配置文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        // 创建SqlSession对象
        SqlSession session = factory.openSession();
        // 获取到代理对象
        IAccountdao dao = session.getMapper(IAccountdao.class);

        // 保存
        dao.saveAccount(account);

        // 提交事务
        session.commit();

        // 关闭资源
        session.close();
        in.close();
    }

    @Test
    public void run2() throws Exception {
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        SqlSession session = factory.openSession();

        IAccountdao dao = session.getMapper(IAccountdao.class);

        List<Account> list = dao.findAll();
        for (Account account: list ) {
            System.out.println(account);
        }

        session.close();
        in.close();
    }


}

运行编写测试mybatis代码,进行测试,是否mybatis搭建成功

当我们测试完成以后,编写Test1测试类,测试service方法是否能够使用

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:spring-servlet.xml"})
public class Testdao {
    @Autowired
    private AccountService accountService;

    @Test
    public void test1(){
        System.out.println("Controller表现层:查询所有账户...");
        // 调用service的方法
        List<Account> list = accountService.findAll();
        System.out.println(list);
    }


}

在这里插入图片描述

可以看到,控制台可以显示数据库中的数据,表示测试成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值