下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、hibernate 与 mybatis 异同与比较 (涵盖网上各个版本,清晰明了 )一、序言最近一直用mybatis 做开发,以前用过hibernate,能感受到一些它们在使用上的区别,不过总想抽出时间来好好比较比较弄弄清楚它们各自的优劣,以便更好进行选择和深入的了解。网上也看了很多资料,结合自己的使用体会,粗率地概括和总结了一下,以供大家参考。二、具体运用上的不同1、所需的jar包Mybatis:只需要3 个( mybatis-3.1.1.jar,mybatis-3.1.1-javadoc.jar, mybatis-3.1.1-sources.jar)Hibernate: 根据功能不同大概需
2、要十几个2、映射关系Mybatis :实体类与sql 之间的映射Hibernate :实体类与数据库之间隐射3、配置文件Student :属性 :int id,String name,String password; 方法配置 :getStudentByName;/通过name查找getStudentById/通过id查找insertStudent/添加StudentupdateStudent/更改StudentdeleteStudent/通过id删除StudentdeleteStudentById/通过name伤处StudentselectStudentmohu/通过name模糊查询Myba
3、tis:总配置文件: mybatisConfig.xml<configuration><typeAliases><typeAlias alias=Studenttype=com.niit.model.Student/></typeAliases><environments default=development><environment id=development><transactionManager type=JDBC/><dataSource type=POOLED><property
4、 name=drivervalue=com.MySQL.jdbc.Driver/><property name=urlvalue=jdbc:mysql:/:3306/mybatis/><property name=username value=root/><property name=password value=root/></dataSource></environment></environments><mappers><mapperresource=com/niit/mod
5、el/StudentMap.xml/></mappers></configuration>l 实体类映射文件: StudentMap.xml (一个或多个) <mapper namespace=com.niit.model.StudentMap> <select id=getStudentByName resultType=Student parameterType=string>select * from student where name=#name </select><select id=getStudentByI
6、d resultType=StudentparameterType=int>select * from student where id=#id</select><insert id=insertStudent parameterType=Student> insert into student(id, name, password) value(#id, #name, #password)</insert><update id=updateStudentparameterType=Student>update student set na
7、me=#name, password=#passwordwhere id=#id</update><delete id=deleteStudent parameterType=String> delete from student where name=#name </delete><delete id=deleteStudentById parameterType=int> delete from student where id=#id </delete><select id=selectStudentmohu parame
8、terType=StringresultType=Student>select * from student where name like %#name%</select></mapper>Hibernate :l 总配置文件: hibernate.cfg.xml<hibernate-configuration><session-factory><propertyname=connection.username>root</property><propertyname=connection.url>jd
9、bc:mysql:/:3306/sample</property><propertyname=dialect>org.hibernate.dialect.MySQLDialect</property><propertyname=connection.password>123</property><propertyname=connection.driver_class>com.mysql.jdbc.Driver</property><propertyname=hibernate.show
10、_sql>True</property><mapping resource=com/niit/model/Student.hbm.xml/></session-factory></hibernate-configuration>l 实体类配置文件:Student.hbm.xml(一个或多个)<hibernate-mapping package=com.niit.model.><class name=Student table=student> <id name=id column=idtype=int>&
11、lt;generator class=identity/> </id> <property name=nametype=java.lang.String><column name=name length=20not-null=true /></property><property name=passwordtype=java.lang.String><column name=password length=20not-null=true /></property></class></hi
12、bernate-mapping>4、基本用法(增删改查模糊)Mybatis :Testl select by namepublic void test() throws IOExceptionString resource = mybatisConfig.xml;Reader reader =Resources.getResourceAsReader(resource);SqlSessionFactory sessionFactory = newSqlSessionFactoryBuilder().build(reader);SqlSession session = sessionFac
13、tory.openSession();session.selectOne(com.niit.model.StudentMap.getStudentByName,b);l select by idsession.selectOne(com.niit.model.StudentMap.getStudentById,2);l insertsession.insert(com.niit.model.StudentMap.insertStudent,student);l /updatesession.insert(com.niit.model.StudentMap.updateStudent , stu
14、dent);l /delete by namesession.insert(com.niit.model.StudentMap.deleteStudent,wl);l /delete by idsession.insert(com.niit.model.StudentMap.deleteStudent ById, 3);l /select muhu(模糊查询 )session.selectList(com.niit.model.StudentMap.selectStud entmohu, b);Hibernate :l /select by idConfiguration cfg = new
15、Configuration().configure();SessionFactory sf = cfg.buildSessionFactory();Session session = sf.openSession(); Session.get(Student.class,id); l /select by name session.createQuery(from Student as s where = a ).list();l /insertsession.save(Student); l /update Session.update(Student) ;l /delete
16、by nameSession.delete (Student) ;l /delete by idUser user = new User();user.setId(1);session.delete(user);l /select muhu(模糊查询 )session.createQuery(from Student as s like %+str+%).list();5、与 Spring 的整合Mybatis :配置数据源文件<bean id=dataSourceclass=mons.dbcp.BasicDataSource><property na
17、me=driverClassName value=com.mysql.jdbc.Driver></property><propertyname=urlvalue=jdbc:mysql:/:3306/spring?useUnicode=true&characterEncoding=UTF-8></property><property name=usernamevalue=root></property><property name=passwordvalue=1234></propert
18、y><property name=maxActivevalue=100></property><property name=maxIdlevalue=30></property><property name=maxWaitvalue=500></property><property name=defaultAutoCommitvalue=true></property></bean>配置 sqlsessionfactory(将数据源注入)<bean id=sqlSession
19、Factoryclass=org.mybatis.spring.SqlSessionFactoryBean><property name=configLocation value=classpath:MyBatis-Configuration.xml></prope rty><property name=dataSourceref=dataSource /></bean>在 Dao 实现层中通过 spring Ioc 愉快使用SqlSessionFactory<bean id=userDaoclass=org.mybatis.spri
20、ng.mapper.MapperFactoryBean><property name=mapperInterface value=com.mybatis.UserDao></property><property name=sqlSessionFactoryref=sqlSessionFactory></property></bean>spring 整合 mybatis 需要的 jar 包Hibernate :配置数据源文件<beanclass=org.springframework.beans.factory.config
21、.PropertyPlaceholderConfigurer><property name=locations><value>classpath:perties</value></property></bean><bean id=dataSource destroy-method=close class=mons.dbcp.BasicDataSource> <property name=driverClassName value=$jdbc.driverClassName /><pr
22、operty name=url value=$jdbc.url /> <property name=username value=$jdbc.username /><property name=password value=$jdbc.password/></bean>配置 sessionfactory(将数据源注入)<bean id=sfclass=org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean><property name=dat
23、aSource ref=dataSource /><property name=packagesToScan><list><value>com.niit.model</value></list></property><property name=hibernateProperties> <props><propkey=hibernate.dialect>org.hibernate.dialect.MySQLDialect</prop><prop key=hiber
24、nate.show_sql>true</prop></props></property></bean>配置 hibernatetemplete(将 SessionFactory注入)<bean id=hibernateTemplateclass=org.springframework.orm.hibernate3.HibernateTemplate><property name=sessionFactoryref=sf></property></bean>6、注解支持mybatis:启用注解并
25、注入testMapper1.Repository(testBaseDAO)2. Autowiredpublic void setTestMapper(Qualifier(testMapper)TestMapper testMapper)this.testMapper = testMapper;SelectProvider(type = TestSqlProvider.class, method = getSql) 或 Select(select * from .)( SelectBuilder/SqlBuilder )InsertProvider(type = TestSqlProvider.
26、class, method = insertSql)DeleteProvider(type = TestSqlProvider.class, method = deleteSql)Options(flushCache = true, timeout = 20000)UpdateProvider(type = TestSqlProvider.class, method =updateSql)Param(id)Result(id = true, property = id, column = test_id)Hibernate :比较基础不解释三、各种“效果”上的不同(10 点 )1. Hiber
27、nate是全自动ORM 框架,而 Mybatis 是半自动的。hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成sql 。而 mybatis 仅有基本的字段映射,对象数据以及对象实际关系仍然需要通过手写sql 来实现和管理。2. hibernate 数据库移植性远大于 mybatis 。 hibernate 通过它强大的映射结构和 hql 语言,大大降低了对象与数据库(oracle 、 mysql 等)的耦合性,而 mybatis 由于需要手写sql ,因此与数据库的耦合性直接取决于程序员写sql的方法,如果sql不具通用性而用了很多某数据库特性的sql语句的话,移植性也会随之降低很多,成本很高。3. hibernate拥有完整的日志系统,mybatis 则欠缺一些。hibernate日志系统非常健全,涉及广泛,包括:sql 记录、关系异常、 优化警告、 缓存提示、 脏数
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 口腔卫生的重要性
- 双J管护理规范与标准
- 护理护理与医疗设备
- 《原发性肝癌诊疗指南(2026年版)》解读课件
- 护理团队沟通障碍分析
- 《有趣的墨痕》教学课件-2025-2026学年苏少版(新教材)小学美术二年级下册
- 零售业会计招聘面试宝典:专业知识与技巧
- 零售业财务管理者招聘问答指南
- 华侨中学学生扩展中心及地下公共停车场项目水土保持方案报告表
- 客户经理工作日志与总结模板
- 《机械制图》电子教材
- 机电一体化三章接口技术
- 《渐进式放松训练》课件
- 柴油发电机房安全管理制度及操作规程
- 光伏支架防腐设计规范
- 肌筋膜链 完整版
- 体育培优补差记录表模板
- 2023初三模拟考试历史答题卡word版可编辑A4版
- 四年级下册语文第二单元 快乐读书吧十万个为什么 导读一等奖创新教学设计
- 平米三层综合楼框架结构计算书、结构图
- JJF 1458-2014磁轭式磁粉探伤机校准规范
评论
0/150
提交评论