当前位置:网站首页>SSM integration (integrated configuration)
SSM integration (integrated configuration)
2022-07-28 03:20:00 【lwj_ 07】

in other words : The first spring、springmvc、mybatis All configurations are integrated ( I have taken notes before , This time it can be integrated )
The steps are as follows :

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 {
/**
* Complete attribute assignment by annotation
*
* Be careful : The annotation value here is obtained by jdbc.properties Obtained in the file , So don't forget to add @PropertySource("#") Note form
*/
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* Define a method : Used to obtain the third party to be managed bean/ object
*/
@Bean
public DataSource dataSource(){
// 1、new The third party bean/ object
DruidDataSource dataSource =new DruidDataSource();
// 2、 To a third party bean/ Object property assignment
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
// 3、 Put the third party bean/ Object is returned to the method
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){ // The purpose of parameter passing is to handle referential dependencies DataSource object
// 1、new SqlSessionFactoryBean object
SqlSessionFactoryBean sqfb =new SqlSessionFactoryBean();
// 2、 Integrate mybatis The core configuration information of
sqfb.setTypeAliasesPackage("com.itheima.domain");
sqfb.setDataSource(dataSource); // The referential dependency is mybatis Core profile settings jdbc Connection information
return sqfb;
}
/*
<mappers>
<!-- load sql The mapping file -->
<package name="com.itheima.dao"></package>
</mappers>
*/
/**
* The mapperScannerConfigurer Method new Coming out MapperScannerConfigurer Object is equivalent to integration mybatis The above information in the core configuration file of
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
// 1、new MapperScannerConfigurer object
MapperScannerConfigurer mp =new MapperScannerConfigurer();
// 2、 load sql The mapping file
mp.setBasePackage("com.itheima.dao");
// amount to mybatis Load in sql The mapping file UserMapper.xml, It's just... Here UserMapper The proxy interface is directly annotated
// form
// Written sql sentence , No, UserMapper.xml, So it is loaded directly into dao Data under the package UserMapper,
// It's just... Here 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;
/**
* tell tomcat Server load springmvc Container environment
*/
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
// load springmvc Container configuration method
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
// Set to attribute all client requests to springmvc management
protected String[] getServletMappings() {
return new String[]{"/"};
}
// load spring Container configuration method
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
// Chinese code scrambling
// filter
// These two methods can be used in the project development by turning notes
}
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 Container notes
@ComponentScan({"com.itheima.service"})
@PropertySource({"jdbc.properties"}) // Configuration is loaded jdbc.properties file Use $ Symbol to get data
// @PropertySource({"classpath:jdbc.properties"}) // If you report an error in the above annotation form, use this annotation instead of the above
@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"}) // scanning controller Does the package contain bean annotation
@EnableWebMvc // json Data format annotation
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边栏推荐
- 行业洞察 | 语音识别真的超过人耳朵了吗?
- ELS keyboard information
- GNU 通用公共许可证 v2.0 GNU GENERAL PUBLIC LICENSE
- 对象数组转成strin再进行,隔开的字符串,包括赛选某个字段的子,或者求和,
- 上位机与MES对接的几种方式
- Stm32f407 ------- DSP learning
- 阿里云国际版邮件服务套餐购买流程
- QML使用Layout布局时出现大量<Unknown File>: QML QQuickLayoutAttached: Binding loop detected for property循环绑定警告
- OA项目之我的审批(会议查询&会议签字)
- Scheme sharing | experts gather to jointly explore accent AI speech recognition
猜你喜欢

Games101 review: ray tracing

嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践

IO analog serial port of stm32

Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment

并发编程面试题总结

When a dialog box pops up, the following form is not available

Softek Barcode Reader 9.1.5

Kubernetes -- Introduction

Full of dry goods, hurry in!!! Easy to master functions in C language

C -- switch case statement
随机推荐
【ACwing 1064 小国王】状压dp
exness:日本物价上涨收入下降,英镑/日元突破 165
阿里云国际版邮件服务套餐购买流程
C -- switch case statement
Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough
一键重装win7系统详细教程
Four methods of closing forms in C #
Redis实现分布式锁
数字孪生智慧楼宇可视化平台实现对园区企业、公众服务一体化
CAD creation group is not combined?
[download file] uniapp develops small programs, downloads files and saves them locally
stm32F407-------DSP学习
【Codeforces Round #806 (Div. 4)(A~F)】
20条心灵鸡汤唯美句子,句句温情暖心!
els 定时器
GNU 通用公共许可证 v2.0 GNU GENERAL PUBLIC LICENSE
四、固态硬盘存储技术的分析(论文)
Shell:资源监控脚本和高负载报警
基于OpenCV的轮廓检测(3)
【R语言】环境指定删除 rm函数