点关注,不迷路;持续更新Java相关技术及资讯!!
一、What is mybatis?
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
翻译成中文:
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
1、源码下载到本地
从github下载mybatis源码到idea中。
2、快速开始
构建mybatis-config.xml文件
usermapper.xml文件
select * from user where id = #{id}
3、创建POJO类
@ToString@Getter@Setterpublic class User implements Serializable{ private Integer id; private String username; private Integer age; private String phone; private String desc;
4、创建测试类
/** * 源码学院-Monkey * 只为培养BAT程序员而生 * http://bat.ke.qq.com * 往期视频加群:516212256 暗号:6 */@Slf4jpublic class MybatisTest { public static void main(String[] args) throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); try { User user = session.selectOne("bat.ke.qq.com.mybatis.UserMapper.selectUser