抑郁症健康,内容丰富有趣,生活中的好帮手!
抑郁症健康 > ssm整合(整合配置)

ssm整合(整合配置)

时间:2020-01-05 14:16:30

相关推荐

也就是说:先把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/对象*/@Beanpublic 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 {@Beanpublic 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的核心配置文件中的上面那些信息*/@Beanpublic 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,//只不过这里是BookDaoreturn 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.ponentScan;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.ponentScan;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.Driverjdbc.url=jdbc:mysql://localhost:3306/ssm_db?useSSL=falsejdbc.username=rootjdbc.password=123456

如果觉得《ssm整合(整合配置)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。