1.hibernate与spring的整合与mybatis与spring的整合大致区别
hibernate与spring的整合过程:
1、导入hibernate、spring的pom依赖
2、spring-hibernate.
数据库信息文件的注册
数据库连接池C3P0的配置
sessionfactory工厂相关信息的配置(hibernate.cfg. )
数据源
数据方言
show_sql,format_sql
映射文件配置
整个工程的事务配置(声明式事务)
1、动态代理
2、配置事务管理器(通知)
3、定一个类似于过滤通知的一个类
4、定义目标对象(pointcut)
mybatis与spring的整合过程:
1、导入spring、mybatis的pom依赖
2、spring-mybatis.
开启注解式开发
数据库信息文件的注册
数据库连接池C3P0的配置
sqlsessionfactory工厂相关信息的配置(mybatis.cfg. )
数据源
日志
github分页插件
映射文件
bookMapper.java 接口
bookMapper. 映射文件
配置事务(注解式事务)
事务管理管理器
使用事务管理器
3、动态代理(可以在网上了解)
2.创建spring配置文件applicationContext
<? version=\"1.0\" encoding=\"UTF-8\"?>
<beans ns=\"http://www.spring work.org/schema/beans\"
ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
ns:context=\"http://www.spring work.org/schema/context\"
ns:tx=\"http://www.spring work.org/schema/tx\" ns:aop=\"http://www.spring work.org/schema/aop\"
xsi:schemaLocation=\"http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans.xsd http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context.xsd http://www.spring work.org/schema/tx http://www.spring work.org/schema/tx/spring-tx.xsd http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop.xsd\">
<!--开启注解式开发-->
<!--数据库信息文件的注册-->
<!--数据库连接池C3P0的配置-->
<!--sqlsessionfactory工厂相关信息的配置(mybatis.cfg. )-->
<!--配置事务(注解式事务)-->
<!-- 注解驱动 -->
<context:annotation-config/>
<!-- 用注解方式注入bean,并指定查找范围:com.zking.ssh2及子子孙孙包-->
<context:component-scan -package=\"com.zking.ssm\"/>
<context:property-placeholder location=\"classpath:jdbc.properties\"/>
<bean id=\"dataSource\" class=\"org.apache.commons.dbcp2.BasicDataSource\"
destroy-method=\"close\">
<property name=\"driverClassName\" value=\"${jdbc.driver}\"/>
<property name=\"url\" value=\"${jdbc.url}\"/>
<property name=\"username\" value=\"${jdbc.username}\"/>
<property name=\"password\" value=\"${jdbc.password}\"/>
<!--初始连接数-->
<property name=\"initialSize\" value=\"10\"/>
<!--最大活动连接数-->
<property name=\"maxTotal\" value=\"100\"/>
<!--最大空闲连接数-->
<property name=\"maxIdle\" value=\"50\"/>
<!--最小空闲连接数-->
<property name=\"minIdle\" value=\"10\"/>
<!--设置为-1时,如果没有可用连接,连接池会一直无限期等待,直到获取到连接为止。-->
<!--如果设置为N(毫秒),则连接池会等待N毫秒,等待不到,则抛出异常-->
<property name=\"maxWaitMillis\" value=\"-1\"/>
</bean>
<!--4. spring和MyBatis整合 -->
<!--1) 创建sqlSessionFactory-->
<bean id=\"sqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">
<!-- 指定数据源 -->
<property name=\"dataSource\" ref=\"dataSource\"/>
<!-- 自动扫描XxxMapping. 文件,**任意路径 -->
<property name=\"mapperLocations\" value=\"classpath*:com/zking/ssm/**/mapper/*. \"/>
<!-- 指定别名 -->
<property name=\"typeAliasesPackage\" value=\"com/zking/ssm/**/model\"/>
<!--配置pagehelper插件-->
<property name=\"plugins\">
<array>
<bean class=\"com.github.pagehelper.PageInterceptor\">
<property name=\"properties\">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--2) 自动扫描com/zking/oa/**/mapper下的所有XxxMapper接口(其实就是DAO接口),并实现这些接口,-->
<!-- 即可直接在程序中使用dao接口,不用再获取sqlsession对象-->
<bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\">
<!-- Package 属性是映射器接口文件的包路径。-->
<!--你可以使用分号或逗号 作为分隔符设置多于一个的包路径-->
<property name=\" Package\" value=\"com/zking/ssm/**/mapper\"/>
<property name=\"sqlSessionFactoryBeanName\" value=\"sqlSessionFactory\"/>
</bean>
<bean id=\"transactionManager\" class=\"org.spring work.jdbc.datasource.DataSourceTransactionManager\">
<property name=\"dataSource\" ref=\"dataSource\" />
</bean>
<tx:annotation-driven transaction-manager=\"transactionManager\" />
<!-- 开启动态代理 -->
<aop:aspectj-autoproxy/>
</beans>
3.注解式开发
hibernate时代的开发
bookdao
bookBiz
bookBizImpl
bookAction
spring-book.
<bean id=\"bookDao\" class=\"com.zking.book.bookdao\"></bean>
<bean id=\"bookBiz\" class=\"com.zking.book.bookBizImpl\">
<property name=\"bookDao\" ref=\"bookDao\'>
</bean>
<bean id=\"bookAction\" class=\"com.zking.book.bookAction\">
<property name=\"bookBiz\" ref=\"bookBiz\'>
</bean>
mybatis时代的开发
具体的实现类中set、get方法,均由spring的注解代替(repository,service、component)
spring的注入,也由spring的注解代替(autowise/resource)
@Repository:将DAO类声明为Bean
@Service:通常作用在业务层
@Constroller:通常作用在控制层,将在Spring MVC中使用
@Component:是一个泛化的概念,仅仅表示spring中的一个组件(Bean),可以作用在任何层次
@Scope:模式声明(singleton|prototype)
@Autowired:将自动在代码上下文与其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方
@Resource:
1)@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
2)指定了name或者type则根据指定的类型去匹配bean
3)指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错
问题:@Autowired和@Resource两个注解的区别:
1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
2)@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了
Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。
@Transactional
注:个人感觉注解式事务比以前的声明式事务更加麻烦,要写的东西更多
继续阅读与本文标签相同的文章
下一篇 :
hadoop基本教程之开源数据仓库工具hive
-
基础c4d教程:简单的木质吊灯建模,小白也能学会
2026-05-18栏目: 教程
-
大族激光:智能装备LION系列光纤激光切割机发布仪式在湘隆重召开
2026-05-18栏目: 教程
-
实拍上汽首个“无人”仓库,本月正式运行
2026-05-18栏目: 教程
-
自动驾驶光车以外的硬件就7万 滴滴想让你不买车就能先坐上
2026-05-18栏目: 教程
-
滴滴迎来大整顿!1000万罚单认清现实,8万司机被开除
2026-05-18栏目: 教程
