逆向工程
jar
org.mybatis
mybatis
3.5.7
junit
junit
4.12
test
mysql
mysql-connector-java
5.1.3
log4j
log4j
1.2.17
com.github.pagehelper
pagehelper
5.2.0
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.0
org.mybatis.generator
mybatis-generator-core
1.3.2
com.mchange
c3p0
0.9.2
mysql
mysql-connector-java
5.1.8
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
<?xml version="1.0" encoding="UTF-8"?>
@Test
public void testMBG(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//查询所有数据
List list = mapper.selectByExample(null);
list.forEach(emp -> System.out.println(emp));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testMBG(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//根据条件查询
EmpExample example = new EmpExample();
// 查询name为张三的,age大于等于20的数据
example.createCriteria().andEmpNameEqualTo("张三").andAgeGreaterThanOrEqualTo(20);
// 或者id不为空的
example.or().andDidIsNotNull();
// 执行查询操作
List list = mapper.selectByExample(example);
// 输出查询结果集
list.forEach(emp -> System.out.println(emp));
} catch (IOException e) {
e.printStackTrace();
}
}
# 常用方法简介
andEmpNameBetween() 在什么之间
andEmpNameEqualTo() 等于
andEmpNameGreaterThan() 大于
andEmpNameGreaterThanOrEqualTo() 大于等于
andEmpNameIn() 在什么中
andEmpNameIsNotNull() 不为null
andEmpNameIsNull() 为null
andEmpNameLessThan() 小于
andEmpNameLessThanOrEqualTo() 小于等于
andEmpNameLike() 模糊查询
andEmpNameNotBetween 不在什么之间
andEmpNameNotEqualTo 不等于
andEmpNameNotIn() 不在什么中
andEmpNameNoLike() 不包括某些字段
# 修改
@Test
public void testMBG(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
// updateByPrimaryKeySelective表示字段为null时不会修改
mapper.updateByPrimaryKeySelective(new Emp(1,"admin",22,null,"456@qq.com",3));
} catch (IOException e) {
e.printStackTrace();
}
}
分页插件
# 导入依赖
com.github.pagehelper
pagehelper
5.2.0
# mybatis核心配置中开启分页
# 测试
@Test
public void testPageHelper(){
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//开启分页
PageHelper.startPage(6, 4);
List list = mapper.selectByExample(null);
PageInfo page = new PageInfo<>(list, 5);
System.out.println(page);
} catch (IOException e) {
e.printStackTrace();
}
}
# 返回数据简介
pageNum:当前页的页码
pageSize:每页显示的条数
size:当前页显示的真实条数
total:总记录数
pages:总页数
prePage:上一页的页码
nextPage:下一页的页码
isFirstPage/isLastPage:是否为第一页/最后一页
hasPreviousPage/hasNextPage:是否存在上一页/下一页
navigatePages:导航分页的页码数
navigatepageNums:导航分页的页码,[1,2,3,4,5]