首先,在上一篇博客中通过逆向工程生成了代码;这篇博客主要内容就是怎么使用生成的逆向工程代码;
使用逆向工程的项目如下:
1.使用普通的逆向工程代码:
@Before
public void setUp() throws Exception {
//mybatis配置文件
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//使用SqlSessionFactoryBuilder创建sessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testfindUserById()throws Exception{
//获取session
SqlSession session = sqlSessionFactory.openSession();
//获限mapper接口实例
UserMapper userMapper = session.getMapper(UserMapper.class);
//查询user信息
User user = userMapper.selectByPrimaryKey(88);
System.out.println(user);
//关闭session
session.close();
}
查询结果:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
User [id=88, username=张宇, birthday=Fri Jul 30 00:00:00 CST 1993, sex=2, address=苏州市]
2.使用带Example类的逆向工程代码:
对UserExample类进行介绍;
//升序,降序:字段+空格+asc(desc)
protected String orderByClause;
//去除重复:true是选择不重复记录,false,反之
protected boolean distinct;
//自定义查询条件
protected List<Criteria> oredCriteria;
对Example类的代码进行使用:
@Test
public void testFindUserByName(){
//获取session
SqlSession session = sqlSessionFactory.openSession();
//获限mapper接口实例
UserMapper userMapper = session.getMapper(UserMapper.class);
//通过criteria构造查询条件
UserExample userExample = new UserExample();
userExample.setOrderByClause("id desc"); //asc升序,desc降序排列
userExample.setDistinct(false); //去除重复,true是选择不重复记录,false反之
UserExample.Criteria criteria = userExample.createCriteria(); //构造自定义查询条件
//criteria.andUsernameEqualTo("张宇");
//criteria.andUsernameLike("张三");
//criteria.andUsernameBetween("张三", "张宇");
criteria.andIdGreaterThan(1);
//自定义查询条件可能返回多条记录,使用List接收
List<User> users = userMapper.selectByExample(userExample);
for (User user : users) {
System.out.println(user);
}
}
查询结果:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
User [id=96, username=唐拓, birthday=Sat Aug 11 00:00:00 CST 2018, sex=2, address=中国]
User [id=94, username=tangtuo, birthday=Wed Aug 08 00:00:00 CST 2018, sex=null, address=北京]
User [id=93, username=tangtuo, birthday=Wed Aug 08 00:00:00 CST 2018, sex=null, address=北京]
User [id=91, username=张小明, birthday=null, sex=1, address=郑州]
User [id=90, username=zhangfei, birthday=Tue Aug 07 00:00:00 CST 2018, sex=2, address=北京]
User [id=88, username=张宇, birthday=Fri Jul 30 00:00:00 CST 1993, sex=2, address=苏州市]
User [id=25, username=陈小明, birthday=null, sex=1, address=河南郑州]
User [id=24, username=张三丰, birthday=null, sex=1, address=河南郑州]
User [id=22, username=陈小明, birthday=null, sex=1, address=河南郑州]
User [id=16, username=张小明, birthday=null, sex=1, address=郑州]
User [id=10, username=张三, birthday=Thu Jul 10 00:00:00 CST 2014, sex=1, address=北京市]
上面就是对带有example类的使用,可以向数据库中查到数据;