SSM整合纯注解开发

 导航目录

1. 创建工程,添加依赖

2. Spring整合Mybatis

2.1 添加mysql.properties属性文件

2.2 添加JdbcConfig配置类

2.3 添加MybatisConfig配置类

2.4 添加SpringConfig配置类

3. Spring整合SpringMVC

3.1 添加SpringMvcConfig配置类

3.2 添加ServletConfig配置类

4. junit测试

4.1 数据层测试

4.2 业务层、表现层测试


1. 创建工程,添加依赖

    
        
        
            org.springframework
            spring-context
            5.3.14
        
        
        
            org.springframework
            spring-web
            5.3.16
        
        
        
            org.springframework
            spring-webmvc
            5.3.16
        
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.13.1
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.13.1
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.13.1
        
        
        
            org.springframework
            spring-jdbc
            5.3.15
        
        
        
            org.springframework
            spring-tx
            5.3.15
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        
        
            com.alibaba
            druid
            1.2.8
        
        
        
            org.mybatis
            mybatis
            3.5.4
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
        
            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            5.3.19
        
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.1
            provided
        
        
            javax.servlet
            jstl
            1.2
        
    

2. Spring整合Mybatis

2.1 添加mysql.properties属性文件

创建好数据库和表之后,在resources文件夹下创建mysql.properties属性文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
jdbc.username=root
jdbc.password=root

注意:

1、mysql是6.0及以上版本的,driver 为com.mysql.cj.jdbc.Driver,url 要加时区

2、防止和系统属性名冲突,名字前面加上 jdbc. 前缀

2.2 添加JdbcConfig配置类

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

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
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //Spring事务管理需要的平台事务管理器对象
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager ds = new DataSourceTransactionManager();
        ds.setDataSource(dataSource);
        return ds;
    }
}

2.3 添加MybatisConfig配置类

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。SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.czp.domain");
        return factoryBean;
    }
    //定义bean,返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.czp.mapper");
        return msc;
    }
}

2.4 添加SpringConfig配置类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
//声明当前类为Spring配置类
@Configuration
//Spring注解扫描
@ComponentScan({"com.czp.service", "com.czp.mapper"})
//加载属性文件
@PropertySource("classpath:mysql.properties")
//导入配置信息
@Import({JdbcConfig.class,MybatisConfig.class})
//开启Spring事务管理
@EnableTransactionManagement
public class SpringConfig {

}

3. Spring整合SpringMVC

3.1 添加SpringMvcConfig配置类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

//声明当前类为Spring配置类
@Configuration
//SpringMVC注解扫描
@ComponentScan("com.czp.controller")
//开启SpringMVC多项辅助功能,如Json数据进行自动类型转换等
@EnableWebMvc
public class SpringMvcConfig {
}

3.2 添加ServletConfig配置类

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //加载SpringConfig配置类,产生spring容器
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
    //加载SpringMvcConfig配置类,产生springmvc容器
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
    //设置由springmvc控制器处理的请求映射路径
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

4. junit测试

4.1 数据层测试

SSM整合纯注解开发_第1张图片

SSM整合纯注解开发_第2张图片

    测试通过!

4.2 业务层、表现层测试

SSM整合纯注解开发_第3张图片

SSM整合纯注解开发_第4张图片

   测试成功~

你可能感兴趣的:(Spring学习记录,spring,java,学习)