org.mybatis
mybatis
3.5.7
mysql
mysql-connector-java
5.1.49
jdbc.driver= com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username= root
jdbc.password= root
mybatis-config文件中的标签必须按照固定的顺序(有的标签可以不写,但顺序一定不能乱): properties、settings、typeAliases、typeHandlers、objectFactory、objectWrapperFactory、reflectorFactory、plugins、environments、databaseIdProvider、mappers
public interface PaymentMapper {
List selectAll();
}
//读取MyBatis的核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取sqlSession,此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//通过代理模式创建UserMapper接口的代理实现类对象
PaymentMapper mapper = sqlSession.getMapper(PaymentMapper.class);
List payments = mapper.selectAll();
安装之后会出现小鸟图标,直接导航和快速生成映射文件
1) pom
log4j
log4j
1.2.17
2) log4j.xml文件,存放的位置是src/main/resources目录下
1) 映射文件
2) package结构必须和mapper文件结构一样,名字也必须一样
1) 接口
public interface PaymentMapper {
List selectAll(@Param("id") int id);
}
2) 映射文件
1) 接口
public interface PaymentMapper {
List selectAll(Payment id);
}
2) 映射文件
1) 接口
public interface PaymentMapper {
List selectAll(Map map);
}
2) 映射文件
用2.3即可
1) 接口
按2.3即可
2) 映射文件
com.github.pagehelper
pagehelper
5.2.0
在MyBatis的核心配置文件(mybatis-config.xml)中配置插件,注意顺序
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
//访问第一页,每页四条数据
PageHelper.startPage(2,2);
List selects = mapper.selectAll();
3.4 PageInfo
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
PageHelper.startPage(1, 4);
List emps = mapper.selectByExample(null);
PageInfo page = new PageInfo<>(emps,5);
System.out.println(page);
insert into payment(serial_name)
values (#{serialName})
!--int updateUser();-->
update t_user set username = '张三' where id = 5
delete from t_user where id = 6
delete from t_user where id in (${ids})
//测试类
@Test
public void deleteMore() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
int result = mapper.deleteMore("1,2,3,8");
System.out.println(result);
}