SSM框架整合(注解版)

文章目录

  • 前言
  • 一、整合Spring
    • 1.1SpringConfig
  • 二、整合Mybatis
    • 2.1MybatisConfig
    • 2.2 JdbcConfig
    • 2.3 jdbc.properties
  • 三、整合SpringMVC
    • 3.1ServletConfig
    • 3.2SpringMvcConfig
  • 四、项目结构
  • 五、pom.xml中的依赖和插件


前言

#博学谷IT学技术支持#


一、整合Spring

1.1SpringConfig

@Configuration
//代表这是一个配置类
@ComponentScan("com.itheima.service")
//扫描Component,多个数据用数组的格式
@PropertySource("classpath:jdbc.properties")
//导入properties文件源
@Import({JdbcConfig.class,MybatisConfig.class})
//导入其他配置类
public class SpringConfig {

}

二、整合Mybatis

2.1MybatisConfig

public class MybatisConfig {
    @Bean //产生一个Bean的方法并交给Spring管理
                                                    //形参dataSource自动装配
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }

}

2.2 JdbcConfig

public class JdbcConfig {
    @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的方法并交给Spring管理
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}

2.3 jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1?useSSL=false
jdbc.username=root
jdbc.password=123456

三、整合SpringMVC

3.1ServletConfig

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

3.2SpringMvcConfig

@Configuration
@ComponentScan("com.itheima.controller")
@EnableWebMvc//快捷配置Spring Webmvc的一个注解
public class SpringMvcConfig {
}

四、项目结构

SSM框架整合(注解版)_第1张图片

五、pom.xml中的依赖和插件

<dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.2.10.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>5.2.10.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>5.2.10.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.5.6version>
    dependency>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.0version>
    dependency>
    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.47version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.1.16version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
      <scope>testscope>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.0version>
    dependency>
  dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.1version>
        <configuration>
          <port>80port>
          <path>/path>
        configuration>
      plugin>
    plugins>
  build>

你可能感兴趣的:(spring,java,spring,boot)