当前位置:网站首页>ssm整合(整合配置)
ssm整合(整合配置)
2022-07-28 02:38:00 【lwj_07】

也就是说:先把spring、springmvc、mybatis的配置都整合在一起 (以前的笔记都做过,这次整合在一起即可)
步骤如下所示:

jdbcConfig:
package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class jdbcConfig {
/**
* 注解方式完成属性赋值
*
* 注意:这里注解的值是通过获取jdbc.properties文件中获取的,因此别忘记加@PropertySource("#")注解形式
*/
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 定义一个方法:用来获取要管理的第三方bean/对象
*/
@Bean
public DataSource dataSource(){
// 1、new 第三方bean/对象
DruidDataSource dataSource =new DruidDataSource();
// 2、给第三方bean/对象属性赋值
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
// 3、把第三方bean/对象返回给该方法
return dataSource;
}
}
MybatisConfig:
package com.itheima.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ // 传参的目的就是处理引用型依赖关系拿到DataSource对象
// 1、new SqlSessionFactoryBean 对象
SqlSessionFactoryBean sqfb =new SqlSessionFactoryBean();
// 2、整合mybatis的核心配置信息
sqfb.setTypeAliasesPackage("com.itheima.domain");
sqfb.setDataSource(dataSource); // 引用型依赖关系为mybatis核心配置文件设置jdbc连接信息
return sqfb;
}
/*
<mappers>
<!-- 加载sql映射文件 -->
<package name="com.itheima.dao"></package>
</mappers>
*/
/**
* 该mapperScannerConfigurer方法new出来的MapperScannerConfigurer对象相当于整合mybatis的核心配置文件中的上面那些信息
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
// 1、new MapperScannerConfigurer对象
MapperScannerConfigurer mp =new MapperScannerConfigurer();
// 2、加载sql映射文件
mp.setBasePackage("com.itheima.dao");
// 相当于mybatis中加载sql映射文件 UserMapper.xml,只不过这里是UserMapper代理接口直接用注的
//形式
// 写的sql语句,没有UserMapper.xml,所以这里直接加载到了dao包下的数据UserMapper,
//只不过这里是BookDao
return mp;
}
}
ServletContainersInitConfig:
package com.itheima.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* 告诉tomcat服务器加载springmvc容器环境
*/
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
// 加载springmvc容器配置方法
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
// 设置把客户端的请求都归属于springmvc管理
protected String[] getServletMappings() {
return new String[]{"/"};
}
// 加载spring容器配置方法
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
// 中文乱码问题
// 过滤器
// 这两个方法项目开发中用到了翻笔记即可
}
SpringConfig:
package com.itheima.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration // spring容器注解
@ComponentScan({"com.itheima.service"})
@PropertySource({"jdbc.properties"}) // 配置加载jdbc.properties文件 使用$符获取数据
// @PropertySource({"classpath:jdbc.properties"}) // 用上面的注解形式报错的话就用这个注解代替上面的
@Import({jdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
SpringMvcConfig:
package com.itheima.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan({"com.itheima.controller"}) // 扫描controller包下是否含有bean注解
@EnableWebMvc // json数据格式注解
public class SpringMvcConfig {
}
jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db?useSSL=false
jdbc.username=root
jdbc.password=123456边栏推荐
- 数据湖:海量日志采集引擎Flume
- [acwing 1064 little king] shaped pressure DP
- [2022 Niuke multi school 2 K link with bracket sequence I] bracket linear DP
- 53. Maximum Subarray最大子数组和
- 酒店vr全景展示拍摄提供更多合作和洽谈的机会
- CAD creation group is not combined?
- 【Codeforces Round #806 (Div. 4)(A~F)】
- Thread Foundation
- 静态博客搭建工具汇总
- QFileDevice、QFile、QSaveFile、QTemporaryFile
猜你喜欢
随机推荐
Decision tree and random forest learning notes (1)
Brush questions every day to consolidate knowledge
Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment
Niuke-top101-bm340
工程电磁场复习基本知识点
“讳疾忌医”的开源走不远
每日刷题巩固知识
QT专题1:实现一个简易计算器
嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践
力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
Redis经典面试题总结
【R语言】环境指定删除 rm函数
Data Lake (XVII): Flink and iceberg integrate datastream API operations
The test post changes jobs repeatedly, jumping and jumping, and then it disappears
Oracle basicfile lob field space recycling shrink space doubts
ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation
汇总了50多场面试,4-6月面经笔记和详解(含核心考点及6家大厂)
基于c8t6芯片开发RC522模块实现呼吸灯
Scheme sharing | experts gather to jointly explore accent AI speech recognition
[stream] parallel stream and sequential stream









