
Mybatis的介绍和基本使用
发布日期:2021-05-09 00:33:11
浏览次数:12
分类:博客文章
本文共 2864 字,大约阅读时间需要 9 分钟。
数据库操作框架的历程
JDBC,几乎不用了,但其他框架都是依据JDBC搭建的
DBUtils
Hibernate,很少用了
Hibernate 优势
Hibernate 劣势
memcache已经被淘汰掉了,都用redis
JDBCTemplate
Mybatis
官方教程
- 用maven在pom中加载
- 构建SqlSessionFactory,使用xml或者不使用xml
- 从SqlSessionFactory 中获取 SqlSession
EmpDao.xml
mybatis-config.xml
environment,transactionManager,dataSource
db.properties
mybatis-config.xml
探究已映射的 SQL 语句
public class Emp{ private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno;}public interface EmpDao{ public void save(Emp emp); public Integer update(Emp emp); public Integer delete(Integer empno); @Select() public Emp selectEmpByEmpno(Integer empno);}public class MyTest{ SqlSessionFactory sqlSessionFactory = null; public void init(){ String resource = "mybatis-config.xml"; InputStream inputStream = null; try{ inputStream Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }catch(IOException e){ e.printStackTrace(); } } @Test public void test01 thorws IOException(){ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 读取数据库会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取对应的映射接口对象 EmpDao mapper = sqlSession.getMapper(EmpDao.class); // 执行具体的sql语句 Emp emp = mapper.selectEmpByEmpno(7369); System.out.println(emp); Integer i = mapper.save(new Emp()); System.out.println(i);// 插入不成功, 需要在EmpDao.xml或者注解中编写完整, 复杂就写配置文件 // 关闭会话 sqlSession.close(); } @Test public void test02() thorws IOException{ SqlSession sqlSession = sqlSessionFactory.openSession(); EmpDao mapper = sqlSession.getMapper(EmpDao.class); Emp emp = new Emp(); emp.setEmpno(3333); emp.setEname("zs"); Integer save = mapper.save(emp); System.out.println(save); } @Test public void test03() thorws IOException{ SqlSession sqlSession = sqlSessionFactory.openSession(); EmpDao mapper = sqlSession.getMapper(EmpDao.class); Emp emp = new Emp(); emp.setEmpno(3333); emp.setEname("zs"); emp.setComm(500.0); Integer update = mapper.save(emp); System.out.println(update); sqlSession.commit(); sqlSession.close(); }}
补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月16日 20时38分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Linux】2.3 Linux目录结构
2019-03-06
远程触发Jenkins的Pipeline任务的并发问题处理
2019-03-06
Web应用程序并发问题处理的一点小经验
2019-03-06
entity framework core在独立类库下执行迁移操作
2019-03-06
Asp.Net Core 2.1+的视图缓存(响应缓存)
2019-03-06
RE套路 - 关于pyinstaller打包文件的复原
2019-03-06
【wp】HWS计划2021硬件安全冬令营线上选拔赛
2019-03-06
Ef+T4模板实现代码快速生成器
2019-03-06
c++ static笔记
2019-03-06
C++中头文件相互包含与前置声明
2019-03-06
JQuery选择器
2019-03-06
SQL--存储过程
2019-03-06
多线程之volatile关键字
2019-03-06
2.1.4奇偶校验码
2019-03-06
2.2.2原码补码移码的作用
2019-03-06
Java面试题:Servlet是线程安全的吗?
2019-03-06
Java集合总结系列2:Collection接口
2019-03-06
Linux学习总结(九)—— CentOS常用软件安装:中文输入法、Chrome
2019-03-06
MySQL用户管理:添加用户、授权、删除用户
2019-03-06
比技术还重要的事
2019-03-06