ssm框架中mapper生成bean的过程

启动的时候MapperScannerConfigurer会扫描mapper接口,生成beanDefinition,将beanClass修改为MapperFactoryBean(实现了FactoryBean接口),放入spring中。在spring执行finishBeanFactoryInitialization方法的时候将beanDefinition生成bean。
下面是简化后的流程(删除了spring中的大部分步骤,如果想spring生成bean的步骤可以看看我的这篇博客,点击跳转)。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架使用的设计模式有: 1. MVC模式:SSM框架采用了MVC模式,即将应用程序分为三个部分:模型(Model)、视图(View)和控制器(Controller)。在SSM框架,Model一般指的是MyBatisMapper接口以及对应的Mapper.xml文件,View一般指的是前端页面,Controller一般指的是Spring MVC的Controller类。 2. 工厂模式:Spring框架Bean工厂就是一个典型的工厂模式的应用。在SSM框架,我们可以通过Spring框架来创建对象,并且通过IoC/DI的方式来管理这些对象的生命周期。 3. 单例模式:Spring框架Bean默认是单例模式的,即在容器只会存在一个实例。这样可以避免频繁的创建和销毁对象,提高系统性能。 4. 代理模式:MyBatisMapper接口是一个代理接口,其实现类由MyBatis框架生成。在SSM框架,我们可以通过Spring AOP来实现代理模式,从而实现事务管理、日志记录等功能。 具体代码示例: 1. MVC模式 Controller类: ```java @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/list") public String list(Model model) { List<User> userList = userService.getUserList(); model.addAttribute("userList", userList); return "user/list"; } } Model类: public class User { private Integer id; private String username; private String password; // getter和setter方法省略 } Mapper接口: public interface UserMapper { List<User> selectAll(); } Mapper.xml文件: <select id="selectAll" resultType="com.example.model.User"> select id, username, password from user </select> ``` 2. 工厂模式 Bean配置文件: ```xml <bean id="userService" class="com.example.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.example.dao.impl.UserDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> ``` 3. 单例模式 Bean配置文件: ```xml <bean id="userService" class="com.example.service.impl.UserServiceImpl" scope="singleton"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.example.dao.impl.UserDaoImpl" scope="singleton"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> ``` 4. 代理模式 Bean配置文件: ```xml <bean id="userService" class="com.example.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.example.dao.impl.UserDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.example.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值