1、先导入需要用的jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.23</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.27</version>
</dependency>
</dependencies>
2、创建配置类
2.1、我的配置类包名
2.2、AppConfig类
@Configuration
@ComponentScan("com.cxd")
@Import({SpringConfig.class, WebMvcConfig.class, MybatisConfig.class})
public class AppConfig {
}
2.3、MybatisConfig类
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
System.out.println("data:"+dataSource);
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
//mybatis配置文件
Configuration configuration=new Configuration();
configuration.setLogImpl(StdOutImpl.class);
configuration.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(configuration);
factoryBean.setPlugins(new PageInterceptor());
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.cxd.mapper");
return mapperScannerConfigurer;
}
}
2.4、MyWebApplicationInit类
public class MyWebApplicationInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//关联AppConfig配置类
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
//前端控制器
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
//添加前端控制器到web容器
ServletRegistration.Dynamic dynamic =
servletContext.addServlet("spring-servlet", dispatcherServlet);
//设置该servlet启动时机 (web容器启动时,实列化servlet)
dynamic.setLoadOnStartup(1);
//设置servlet的匹配路径
dynamic.addMapping("/");
//添加编码过滤器
servletContext.addFilter("encodingFilter", new CharacterEncodingFilter("UTF-8"));
MultipartConfigElement configElement =
new MultipartConfigElement("", 1024 * 1024, 1024 * 1024, 0);
dynamic.setMultipartConfig(configElement);
}
}
2.5、SpringConfig类
@PropertySource(value = "classpath:mysql.properties")
@EnableTransactionManagement
public class SpringConfig {
@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
public DataSource dataSource() {
System.out.println("driver"+driver);
DriverManagerDataSource dataSource =
new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public TransactionManager transactionManager(DataSource dataSource) {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
2.6、WebMvcConfig类
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
//拦截器
public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(new LoginInterceptor())
// .addPathPatterns("/**")
// .excludePathPatterns("/","/user/login","/user/getCode");
}
// 文件解析器
@Bean
public MultipartResolver multipartResolver() {
StandardServletMultipartResolver multipartResolver =
new StandardServletMultipartResolver();
return multipartResolver;
}
//视图控制,默认首页
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver =
new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/page/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
3、mysql.properties文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxx
jdbc.username=xxx
jdbc.password=xxx
标签:jdbc,return,框架,class,SSM,dataSource,new,public,搭建
From: https://blog.csdn.net/or77iu_N/article/details/143733174