package cn.itcast.ssm.mapper;
import java.io.Reader;
import java.util.List;
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.Before;
import org.junit.Test;
import cn.itcast.ssm.po.User;
import cn.itcast.ssm.po.UserExample;
import cn.itcast.ssm.po.UserExample.Criteria;
public class UserMapperTest {
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory;
private SqlSession sqlSession;
/**
* Junit 测试初始化函数
* @throws Exception
*/
@Before
public void init() throws Exception {
//1.获取数据库会话工厂构建者
this.builder = new SqlSessionFactoryBuilder();
//2.加载核心配置文件SqlMapConfig.xml
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//3.获取SQLSession工厂
this.factory = builder.build(reader);
//4.获取SQLSession
this.sqlSession = factory.openSession();
}
@Test
public void findUser() throws Exception {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
UserExample example = new UserExample();
String value = "";
String value1 = "";
String value2 = "";
List<String> values = null;
Criteria criteria = example.createCriteria();
// where XXX between value1 and value2
criteria.andAddressBetween(value1, value2);
// where XXX = value
criteria.andAddressEqualTo(value);
// where XXX > value
criteria.andAddressGreaterThan(value);
// where XXX >= value
criteria.andAddressGreaterThanOrEqualTo(value);
// where XXX in (values[0],values[1],values[n],)
criteria.andAddressIn(values);
// where XXX!=''
criteria.andAddressIsNotNull();
// where xxx=''
criteria.andAddressIsNull();
// where XXX < value
criteria.andAddressLessThan(value);
//where xxx <= value
criteria.andAddressLessThanOrEqualTo(value);
// where xxx like value
criteria.andAddressLike(value);
// where xxx not between value1 and value2
criteria.andAddressNotBetween(value1, value2);
// where xxx != value
criteria.andAddressNotEqualTo(value);
// where xxx not in (values[0],values[1],values[n],,,,);
criteria.andAddressNotIn(values);
// where xxx not like value
criteria.andAddressNotLike(value);
Integer id =null;
User user = null;
// 根据条件查询
userMapper.selectByExample(example);
// 根据主键查询
userMapper.selectByPrimaryKey(id);
// 根据条件删除
userMapper.deleteByExample(example);
// 根据主键删除
userMapper.deleteByPrimaryKey(id);
// 根据条件查询个数
userMapper.countByExample(example);
// 插入记录,并且如果某属性为空,且该列有默认值,使用空值
userMapper.insert(user);
// 插入纪录,并且如果某属性为空,且该列有默认值,则使用默认值
userMapper.insertSelective(user);
// 根据example更新记录,user为空的属性也会进行更新为空
userMapper.updateByExample(user, example);
// 根据example更新记录,user为空的属性忽略,不更新
userMapper.updateByExampleSelective(user, example);
// 根据user.id更新记录,user为空的属性也会更新为空
userMapper.updateByPrimaryKey(user);
// 根据user.id更新记录,user为空的属性忽略,保存原有值
userMapper.updateByPrimaryKeySelective(user);
}
}
package cn.itcast.ssm.mapper;
import java.io.Reader;
import java.util.List;
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.Before;
import org.junit.Test;
import cn.itcast.ssm.po.User;
import cn.itcast.ssm.po.UserExample;
import cn.itcast.ssm.po.UserExample.Criteria;
public class UserMapperTest {
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory;
private SqlSession sqlSession;
/**
* Junit 测试初始化函数
* @throws Exception
*/
@Before
public void init() throws Exception {
//1.获取数据库会话工厂构建者
this.builder = new SqlSessionFactoryBuilder();
//2.加载核心配置文件SqlMapConfig.xml
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//3.获取SQLSession工厂
this.factory = builder.build(reader);
//4.获取SQLSession
this.sqlSession = factory.openSession();
}
@Test
public void findUser() throws Exception {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
UserExample example = new UserExample();
String value = "";
String value1 = "";
String value2 = "";
List<String> values = null;
Criteria criteria = example.createCriteria();
// where XXX between value1 and value2
criteria.andAddressBetween(value1, value2);
// where XXX = value
criteria.andAddressEqualTo(value);
// where XXX > value
criteria.andAddressGreaterThan(value);
// where XXX >= value
criteria.andAddressGreaterThanOrEqualTo(value);
// where XXX in (values[0],values[1],values[n],)
criteria.andAddressIn(values);
// where XXX!=''
criteria.andAddressIsNotNull();
// where xxx=''
criteria.andAddressIsNull();
// where XXX < value
criteria.andAddressLessThan(value);
//where xxx <= value
criteria.andAddressLessThanOrEqualTo(value);
// where xxx like value
criteria.andAddressLike(value);
// where xxx not between value1 and value2
criteria.andAddressNotBetween(value1, value2);
// where xxx != value
criteria.andAddressNotEqualTo(value);
// where xxx not in (values[0],values[1],values[n],,,,);
criteria.andAddressNotIn(values);
// where xxx not like value
criteria.andAddressNotLike(value);
Integer id =null;
User user = null;
// 根据条件查询
userMapper.selectByExample(example);
// 根据主键查询
userMapper.selectByPrimaryKey(id);
// 根据条件删除
userMapper.deleteByExample(example);
// 根据主键删除
userMapper.deleteByPrimaryKey(id);
// 根据条件查询个数
userMapper.countByExample(example);
// 插入记录,并且如果某属性为空,且该列有默认值,使用空值
userMapper.insert(user);
// 插入纪录,并且如果某属性为空,且该列有默认值,则使用默认值
userMapper.insertSelective(user);
// 根据example更新记录,user为空的属性也会进行更新为空
userMapper.updateByExample(user, example);
// 根据example更新记录,user为空的属性忽略,不更新
userMapper.updateByExampleSelective(user, example);
// 根据user.id更新记录,user为空的属性也会更新为空
userMapper.updateByPrimaryKey(user);
// 根据user.id更新记录,user为空的属性忽略,保存原有值
userMapper.updateByPrimaryKeySelective(user);
}
}