package com.powernode.mybatis.util;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
//这是一个mybatis工具类
public class SqlSessionUtil
{
private static final Logger logger = LoggerFactory.getLogger(SqlSessionUtil.class);
//工具类的构造方法一般都是私有化的,为了防止它创建对象
//工具类所有的方法都是静态的,直接采用类名即可调用,不需要创建对象
private SqlSessionUtil(){};
private static SqlSessionFactory sqlSessionFactory;
static
{
//静态代码块,让它在第一次加载的时候创建SqlSessionBuilder对象
try
{
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
logger.info("我在这");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public static SqlSession openSqlSession()
{
logger.info("嘿嘿嘿");
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
// public static SqlSession openSession()
// {
// SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
// //SqlSessionFactory对象的特点是,一个SqlSessionFactory对应一个environment
// //environments表示数据库
// //每有一个数据库就需要创建一个新的SqlSessionFactory,对应那个数据库
// SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
// SqlSession sqlSession = build.openSession();
// return sqlSession;
// }
}
package com.powernode.mybatis.util;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
//这是一个mybatis工具类
public class SqlSessionUtil
{
private static final Logger logger = LoggerFactory.getLogger(SqlSessionUtil.class);
//工具类的构造方法一般都是私有化的,为了防止它创建对象
//工具类所有的方法都是静态的,直接采用类名即可调用,不需要创建对象
private SqlSessionUtil(){};
private static SqlSessionFactory sqlSessionFactory;
static
{
//静态代码块,让它在第一次加载的时候创建SqlSessionBuilder对象
try
{
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
logger.info("我在这");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public static SqlSession openSqlSession()
{
logger.info("嘿嘿嘿");
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
// public static SqlSession openSession()
// {
// SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
// //SqlSessionFactory对象的特点是,一个SqlSessionFactory对应一个environment
// //environments表示数据库
// //每有一个数据库就需要创建一个新的SqlSessionFactory,对应那个数据库
// SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
// SqlSession sqlSession = build.openSession();
// return sqlSession;
// }
}
package com.powernode.mybatis.test;
import com.powernode.mybatis.service.MathService;
import com.powernode.mybatis.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MathServiceTest
{
@Test
public void TestInsertByUtil()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
int insertCar = sqlSession.insert("insertCar");
int expected = 1;
Assert.assertEquals(insertCar,expected);
sqlSession.commit();
sqlSession.close();
}
//一个业务类对应一个测试用例
private static final Logger logger = LoggerFactory.getLogger(MathServiceTest.class);
//单元测试类:名字规范,测试类名加上Test
//单元测试方法写多少个,一般是一个业务方法对应一个测试方法
//测试方法一般是Test加方法名
@Test
public void TestSum()
{
//单元测试的概念,一个是实际值(被测试业务方法的真正执行结果),一个是期望值(业务方法你期待的结果)
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int actual = mathService.sum(1, 2);
//获取实际值
logger.info("" + actual);
//期望值
// int expected = 30;
int expected = 3;
//加断言进行测试
Assert.assertEquals(actual,expected);
}
@Test
public void TestSub()
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int sub = mathService.sub(10, 6);
int ex = 4;
logger.info("" + sub);
Assert.assertEquals(sub,ex);
}
}
package com.powernode.mybatis.test;
import com.powernode.mybatis.service.MathService;
import com.powernode.mybatis.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MathServiceTest
{
@Test
public void TestInsertByUtil()
{
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
int insertCar = sqlSession.insert("insertCar");
int expected = 1;
Assert.assertEquals(insertCar,expected);
sqlSession.commit();
sqlSession.close();
}
//一个业务类对应一个测试用例
private static final Logger logger = LoggerFactory.getLogger(MathServiceTest.class);
//单元测试类:名字规范,测试类名加上Test
//单元测试方法写多少个,一般是一个业务方法对应一个测试方法
//测试方法一般是Test加方法名
@Test
public void TestSum()
{
//单元测试的概念,一个是实际值(被测试业务方法的真正执行结果),一个是期望值(业务方法你期待的结果)
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int actual = mathService.sum(1, 2);
//获取实际值
logger.info("" + actual);
//期望值
// int expected = 30;
int expected = 3;
//加断言进行测试
Assert.assertEquals(actual,expected);
}
@Test
public void TestSub()
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring6.xml");
MathService mathService = context.getBean("mathService", MathService.class);
int sub = mathService.sub(10, 6);
int ex = 4;
logger.info("" + sub);
Assert.assertEquals(sub,ex);
}
}