ssm整合模版(注解配置类方式)

ssm整合模版

版本:

jdk 17 ; idea2023.3.1 ; maven3.6.0 ;tomcat10 ;

依赖

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.21</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.15</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.16.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.33</version>
        </dependency>

        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>3.0.0-M1</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet.jsp.jstl</groupId>
            <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>

整合mybatis的配置类

package com.ketai.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;
import javax.sql.DataSource;

/**
 * spring配置文件 整合mybatis
 * @PropertySource 导入外部属性配置文件
 * @ComponentScan 排除controller和restController注解的类
 */
@Configuration
@ComponentScan(value = "com.ketai.mapper")
@ComponentScan(basePackages = "com.ketai",excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class,RestController.class})})
@PropertySource(value = "classpath:jdbc.properties")
public class SpringApplicationContextConfig {

    @Value("${jdbc.driver}")
    private String DriverClass ;

    @Value("${jdbc.url}")
    private String url ;

    @Value("${jdbc.username}")
    private String userName ;

    @Value("${jdbc.password}")
    private String password ;


    /**
     * 配置数据源
     * 交给spring管理
     * @return DataSource
     */
    @Bean
    public DataSource getDataSource(){

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(DriverClass);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(password);
        return druidDataSource ;
    }


    /**
     * 配置 sqlsessionfactorybean
     * @return
     */
    @Bean
    public SqlSessionFactory getSqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactory factory = null;
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        //注入数据源
        sessionFactoryBean.setDataSource(dataSource);
        //加载mapper映射文件
        sessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath*:/mappers/*.xml"));

        //设置别名
        sessionFactoryBean.setTypeAliasesPackage("com.ketai.pojo");

        //获取SqlsessionFactory对象
        org.apache.ibatis.session.Configuration config = factory.getConfiguration();

        //开启驼峰自动映射
        config.setMapUnderscoreToCamelCase(true);


        //把mybaits配置文件对象给到factorybean
        sessionFactoryBean.setConfiguration(config);


        try {
            factory = sessionFactoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return  factory ;
    }


    /**
     *  配置事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager setDataSourceTransactionManager(@Autowired DataSource dataSource){

        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        //注入数据源
        manager.setDataSource(dataSource);

        return  manager ;
    }

}

整合springmvc的配置类

package com.ketai.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * springmvc配置类
 * @EnableWebMvc 启用webmvc
 */
@Configuration
@ComponentScan("com.ketai.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    /**
     * 配置视图解析器
     * @return
     */
    @Bean
    public InternalResourceViewResolver InternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return  resolver ;
    }


    /**
     * 添加静态资源处理程序
     * 避免静态资源被拦截
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("/static/");
    }


    /**
     * 添加视图控制器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/manager/login").setViewName("backendlogin");
//        registry.addViewController("/dev/login").setViewName("devlogin");
    }
}

springmvc启动加载类

package com.ketai;

import com.ketai.config.SpringApplicationContextConfig;
import com.ketai.config.SpringMvcConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;


/**
 * 启动加载类
 * 加载配置文件
 */
public class SystemContextServletInit extends AbstractAnnotationConfigDispatcherServletInitializer {


    /**
     * 获取spring配置类
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringApplicationContextConfig.class};
    }

    /**
     * 获取springwebmvc配置类
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    /**
     * 设置DispatcherServlet拦截路径
     * @return
     */
    @Override
    protected String[] getServletMappings() {

        return new String[]{"/"};
    }
}

属性配置文件 jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/beyond_platform_db?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值