在上一章我们已经完成了对于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);
}
}
可以看到,控制台可以显示数据库中的数据,表示测试成功