MyBatisX插件:
按照单个数据库的操作方法重新创建BrandMapper.xml , BrandMapper接口 , Brand实体类
注意在mybatis-config.xml中加上BrandMapper.xml映射
注意:如果查询结果为:
实体类属性名和数据库表列明不一致,不能自动封装数据
在BrandMapper接口中定义查询方法
// 查询tb_brand中的一条数据
Brand selectByid(int id);
在BrandMapper.xml文件中编辑查询的sql语句
在test文件中编写测试程序
@Test
public void selectOne() {
int id = 1;
InputStream is = null;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sf.openSession(true);
// 获取Mapper接口
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
// 执行sql语句
Brand selectByid = mapper.selectByid(id);
// 返回结果在控制台输出
System.out.println("您所查询的为:" + selectByid);
// 释放资源
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
===================================================
您所查询的为:Brand{id=1, brand_name ='三只松鼠', ordered=10, address='安徽'}
Process finished with exit code 0