Spring整合Mybatis

本文详细介绍Spring框架与MyBatis框架的整合过程,包括所需依赖包、配置ApplicationContext.xml、数据库连接、创建SqlSessionFactory、扫描mapper文件、构建service层及controller层的方法。

一.Spring整合Mybatis

1.导包

[1]需要导入的MyBatis的全部包(10个)

asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
mybatis-3.2.7.jar

[2]mysql的驱动包(1个)
mysql-connector-java-5.1.30.jar

[3]Spring的包(9个)
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
commons-logging-1.1.3.jar

[4]Spring整合Mybatis需要的包(1个)
mybatis-spring-1.2.3.jar

2.书写配置ApplicationContext.xml文件

[1]连接数据库的内容-------Spring-jdbc

<!-- dataSource连接数据库 --在Spring-jdbc包下去找DataSource 利用set注入相应的属性值>
<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/mybatis"></property>
     <property name="username" value="root"></property>
     <property name="password" value="root"></property>
 </bean>

[2]创建sqlsessionFactory工厂-----mybatis-spring

<!-- 构建factory工厂
指定dataSource 在mybatis-spring 包下去找SqlSessionFactoryBean查看相应属性,为其赋值,这个factory的功能就相当于以前myBatis的mybatis.xml文件的功能
-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource"></property>
     <property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
 </bean>

[3]扫描mapper文件-------------mybatis-Spring

<!-- 扫描包 mapper的配置 -->
 <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <!-- 为其配置basePackage(和之前mybatis.xml文件中的mappers一样,用于配置包或者其他mapper)和 factory-->
     <property name="basePackage" value="com.bjsxt.mapper"></property>
     <property name="sqlSessionFactory" ref="factory"></property>
 </bean>
注意:
配置完成自动的把mapper层的bean类对象创建好 bean 的id名称就是mapper中接口的名称首字母小写

3.mapper层的文件

4.service层的构建

service层的构建也可以利用ApplicationContext.xml文件进行配置

即在service的对应接口中定义set方法,设置一个mapper层的对象进去,在applicationContext.xml中利用set方式注入,而mapper层的实现类对象,spring_IOC容器已经为我们创建好了,即在basePackage配置这个步骤,就是为了自动的把mapper层的bean类对象创建.

<!-- 构建FlowerServiceImpl的对象 -->
 <bean id="flowerService" class="com.bjsxt.service.impl.FlowerServiceImpl">
     <property name="flowerMapper" ref="flowerMapper"></property>
 </bean>

5.书写controller层(书写jsp)

[1]第一种直接new的方式 (在init方法中)

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
studentService = app.getBean("studentService",StudentService.class)

这时出现了一个问题,Spring容器在获取的代码的路径在init方法中的耦合性较高。applicationContext2.xml的文件名变了需要修改代码
这时我们引入了后面两张方式

[2]第二种方式 
将Spring容器相关路径信息配置到web.xml中 再通过ServletContext获取初始化参数信息

java代码
String applicationContextName = this.getServletContext().getInitParameter("applicationContextName");
ApplicationContext app = new ClassPathXmlApplicationContext(applicationContextName);
studentService = app.getBean("studentService",StudentService.class);

web.xml
<context-param>
     <param-name>applicationContextName</param-name>
     <param-value>applicationContext2.xml</param-value>
 </context-param>

[3]第三种方式 
将Spring容器相关路径信息配置到web.xml中,同时配置Spring监听器, 通过spring-web包提供的方法实例化相应的对象,这种方法将所有的配置都放在xml文件中

java代码
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService = app.getBean("studentService",StudentService.class);

web.xml
<context-param>      
<param-name>contextConfigLocation</param-name>      
<param-value>classpath:applicationContext2.xml</param-value>  
</context-param> 

<!--需要到spring-web的jar包下面找context包下的ContextLoaderListener 这个类,根据api提示去写相应的配置信息-->
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
classpath: 是项目class文件根目录的意思

Tips:

ContextLoaderListener作用:
在启动Web容器时,自动装配Spring的applicationContext.xml的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。

如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数

applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入

【负荷预测】基于VMD-CNN-LSTM的负荷预测研究(Python代码实现)内容概要:本文介绍了基于变分模态分解(VMD)、卷积神经网络(CNN)和长短期记忆网络(LSTM)相结合的VMD-CNN-LSTM模型在负荷预测中的研究与应用,采用Python代码实现。该方法首先利用VMD对原始负荷数据进行分解,降低序列复杂性并提取不同频率的模态分量;随后通过CNN提取各模态的局部特征;最后由LSTM捕捉时间序列的长期依赖关系,实现高精度的负荷预测。该模型有效提升了预测精度,尤其适用于非平稳、非线性的电力负荷数据,具有较强的鲁棒性和泛化能力。; 适合人群:具备一定Python编程基础和深度学习背景,从事电力系统、能源管理或时间序列预测相关研究的科研人员及工程技术人员,尤其适合研究生、高校教师及电力行业从业者。; 使用场景及目标:①应用于日前、日内及实时负荷预测场景,支持智慧电网调度与能源优化管理;②为研究复合型深度学习模型在非线性时间序列预测中的设计与实现提供参考;③可用于学术复现、课题研究或实际项目开发中提升预测性能。; 阅读建议:建议读者结合提供的Python代码,深入理解VMD信号分解机制、CNN特征提取原理及LSTM时序建模过程,通过实验调试参数(如VMD的分解层数K、惩罚因子α等)优化模型性能,并可进一步拓展至风电、光伏等其他能源预测领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值