@Test
public void SelectByConditionMapper(){
Condition condition=new Condition(Test1.class);
condition.createCriteria().andCondition("name like '%zhangsan%'");
condition.setOrderByClause("name desc");
List<Test1> list = mapper.selectByExample(condition);
System.out.println(list.size());
}
通用Mapper可以简化CRUD操作,不必单独为每一Mapper指定CRUD接口
所需jar包mapper-3.3.9.jar
,persistence-api-1.0.jar
在spring配置文件中配置:
<!-- 配置通用Mapper -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
只要extends tk.mybatis.mapper.common.Mapper<T>就拥有了通用Mapper中所有方法
public interface Test1Mapper extends Mapper<Test1>{
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import tk.mybatis.mapper.entity.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yupont.gs.dao.mapper.Test1Mapper;
import com.yupont.gs.model.Test1;
/**
* 通用Mapper常用方法:
*
* 等号的CRUD:
* List<T> select(T record); 根据实体中的属性值进行查询,查询条件使用等号
* T selectByPrimaryKey(Object key); 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
* List<T> selectAll(); 查询全部结果,select(null)方法能达到同样的效果
* T selectOne(T record); 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
* int selectCount(T record); 根据实体中的属性查询总数,查询条件使用等号
* int insert(T record); 保存一个实体,null的属性也会保存,不会使用数据库默认值
* int insertSelective(T record); 保存一个实体,null的属性不会保存,会使用数据库默认值
* int updateByPrimaryKey(T record); 根据主键更新实体全部字段,null值会被更新
* int updateByPrimaryKeySelective(T record); 根据主键更新属性不为null的值
* int delete(T record); 根据实体属性作为条件进行删除,查询条件使用等号
* int deleteByPrimaryKey(Object key); 根据主键字段进行删除,方法参数必须包含完整的主键属性
*
* 条件的CRUD:
* List<T> selectByCondition(Object condition); 根据Condition条件进行查询
* int selectCountByCondition(Object condition); 根据Condition条件进行查询总数
* int updateByCondition(@Param("record") T record, @Param("example") Object condition); 根据Condition条件更新实体record包含的全部属性,null值会被更新
* int updateByConditionSelective(@Param("record") T record, @Param("example") Object condition); 根据Condition条件更新实体record包含的不是null的属性值
* int deleteByCondition(Object condition); 根据Condition条件删除数据
*
*
*
*/
public class CommonMapperTest {
public static Test1Mapper mapper = ((SqlSessionFactory) new ClassPathXmlApplicationContext("spring-db.xml").getBean("sqlSessionFactory")).openSession().getMapper(Test1Mapper.class);
@Test
public void selectAll(){
PageHelper.startPage(2, 5); //第一个参数offset是当前页数,第二个参数limit是每页显示多少数据
//Condition c = new Condition(Test1.class);
//c.createCriteria().andCondition("name in ('zhaoliu','zhangsan')");
List<Test1> list = mapper.selectAll();
PageInfo<Test1> page = new PageInfo<Test1>(list);
System.out.println(page.getTotal());
for (Test1 test1 : list) {
System.out.println(test1.getName());
}
}
@Test
public void insertSelective(){
Test1 test = new Test1();
test.setAge(33);
test.setName("zhaoliu");
mapper.insertSelective(test);
System.out.println(test.getId());
}
@Test
public void selectOne(){
Test1 test = new Test1();
test.setAge(22);
Test1 t = mapper.selectOne(test);
System.out.println(t.getName());
}
@Test
public void SelectByConditionMapper(){
Condition condition=new Condition(Test1.class);
condition.createCriteria().andCondition("name like '%zhangsan%'");
condition.setOrderByClause("name desc");
List<Test1> list = mapper.selectByExample(condition);
System.out.println(list.size());
}
@Test
public void generatorCode() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(new ClassPathResource("generatorConfig.xml").getInputStream());
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}