现在IT行业SSH的三大框架的魄力越来越大,而如何选择适合自己程序的持久层确实需要谨慎思考!下面小编就带着大家一起来治愈持久层框架的“选择恐慌症”:
首先来看小编给大家准备的一幅图,来对这两个持久层框架进行基本了解:
对两个框架有了基本的了解之后,我们一起来看二者在程序中具体应用的不同:
mybatis:只需3个(mybatis-3.1.1.jar,mybatis-3.1.1-javadoc.jar,mybatis-3.1.1-sources.jar)
hibernate:根据功能不同,大概需要十几个包
mybatis:实体类与sql之间的映射
hibernate:实体类与数据库之间的映射
MybatisConfig.xml:
<span style="font-size:18px;"><configuration> <typeAliases> <typeAlias alias="Student" type="com.niit.model.Student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/niit/model/StudentMap.xml"/> </mappers> </configuration></span>StudentMap.xml:
<span style="font-size:18px;"><mapper namespace="com.niit.model.StudentMap"> <select id="getStudentById" resultType="Student" parameterType="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="updateStudent" parameterType="Student"> update student set name=#{name}, password=#{password} where id=#{id} </update> <delete id="deleteStudent" parameterType="String"> delete from student where name=#{name} </delete> </mapper></span>
hibernate.cfg.xml
<span style="font-size:18px;"> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url"> jdbc:mysql://127.0.0.1:3306/sample</property> <property name="dialect">org.hibernate.dialect.MySQLDialect </property> <property name="connection.password">123</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.show_sql">True</property> <mapping resource="com/niit/model/Student.hbm.xml" /> </session-factory> </hibernate-configuration></span>
Student.htm.xml:
<hibernate-mapping package="com.niit.model."> <class name="Student" table="student"> <id name="id" column="id" type="int"> <generator class="identity"/> </id> <property name="name" type="java.lang.String"> <column name="name" length="20" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" not-null="true" /> </property> </class> </hibernate-mapping>
@Test public void test() throws IOException { String resource = "mybatisConfig.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); // select by name session.selectOne("com.niit.model.StudentMap.getStudentByName","b");} // select by id session.selectOne("com.niit.model.StudentMap.getStudentById",2); //insert session.insert("com.niit.model.StudentMap.insertStudent", student); //update session.insert("com.niit.model.StudentMap.updateStudent", student); //delete by name session.insert("com.niit.model.StudentMap.deleteStudent", "wl"); //delete by id session.insert("com.niit.model.StudentMap.deleteStudentById", 3); //select muhu(模糊查询) session.selectList("com.niit.model.StudentMap.selectStudentmohu", "b");
<span style="font-size:18px;"> Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); //select by name session.createQuery("from Student as s where s.name =’a’").list(); //select by id Session.get(Student.class,id); //insert session.save(Student); //update Session.update(Student) ; //delete by name Session.delete (Student) ; //delete by id User user = new User(); user.setId(1); session.delete(user); //select muhu(模糊查询) session.createQuery("from Student as s where s.name like '%") </span>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring? useUnicode=true&characterEncoding=UTF-8> </property> <property name="username" value="root"></property> <property name="password" value="1234"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean>
<span style="font-size:18px;"><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:MyBatis-Configuration.xml"> </property> <property name="dataSource" ref="dataSource" /> </bean></span>在dao层实现通过Spring ioc愉快使用sqlsessionFactory:
<span style="font-size:18px;"> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.mybatis.UserDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean></span>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>配置sessionFactory(将数据源注入):
<span style="font-size:18px;"><bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list><value> com.niit.model </value></list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean></span>注入sessionFactory:
<span style="font-size:18px;"><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sf"></property> </bean></span>
① Hibernate与MyBatis都可以是通过SessionFactoryBuider由XML配置文件生成SessionFactory,然后由 SessionFactory 生成Session,最后由Session来开启执行事务和SQL语句。 其中SessionFactoryBuider,SessionFactory,Session的生命周期都是差不多的。② Hibernate和MyBatis都支持JDBC和JTA事务处理。
① MyBatis可以进行更为细致的SQL优化,可以减少查询字段。
② MyBatis容易掌握,而Hibernate门槛较高。
① Hibernate的DAO层开发比MyBatis简单,Mybatis需要维护SQL和结果映射。
② Hibernate对对象的维护和缓存要比MyBatis好,对增删改查的对象的维护要方便。
③ Hibernate数据库移植性很好,MyBatis的数据库移植性不好,不同的数据库需要写不同SQL。
④ Hibernate有更好的二级缓存机制,可以使用第三方缓存。MyBatis本身提供的缓存机制不佳。