Mybatis的介绍和基本使用
数据库操作框架的历程#
JDBC,几乎不用了,但其他框架都是依据JDBC搭建的#
DBUtils#
Hibernate,很少用了#
Hibernate 优势#
Hibernate 劣势#
memcache已经被淘汰掉了,都用redis
JDBCTemplate#
Mybatis#
官方教程
https://mybatis.org/mybatis-3/getting-started.html
- 用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(); } }
补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字 补充文字
作者:BigBender
出处:https://www.cnblogs.com/BigBender/p/14280070.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!