依赖包
本文档对于毫无Java项目开发经验的同学来说可能理解困难一点,我记性不好,满足自己搭建框架时使用
引入项目必须的依赖包
<dependencies>
<!--Spring 核心jar包 IOC/DI-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.2</version>
</dependency>
<!--原来的J2EE, 新的命名,这里主要用servlet的功能-->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>10.0.0</version>
</dependency>
<!--Spring MVC 核心依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.2</version>
</dependency>
<!--Json处理-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>8.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.1.1</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!--开源的数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.21</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>name.remal.tools.test</groupId>
<artifactId>junit5</artifactId>
<version>1.26.147</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
JAVA类配置MVC
代码注释很清楚
package com.cd.ssm.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration //指明这个类是配置类
@EnableWebMvc //注入HandlerMapping/HandlerAdapter Json处理
@ComponentScan("com.cd.ssm.controller") //把controller包下的所有类注入spring容器中
public class SpringMvcConfig implements WebMvcConfigurer {
/*
此时会注册一个默认的Handler:DefaultServletHttpRequestHandler,这个Handler也是用来处理静态文件的,它会尝试映射/。
当DispatcherServlet映射/时,并且没有找到合适的Handler来处理请求时,就会交给DefaultServletHttpRequestHandler 来处理。
注意:这里的静态资源是放置在web根目录下,而非WEB-INF下
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/*
* 主要是用来匹配页面的,当在controller层跳转时,方便找到对应的页面
* */
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/","jsp");
}
/*添加controller层的拦截器,里面有3个方法可以重写*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// WebMvcConfigurer.super.addInterceptors(registry);
}
}
Java类配置Spring事务,AOP等
package com.cd.ssm.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/*
* 业务层配置类,service ,aop ,tx
* 1,service
* 2,开启aop注解的支持 aspect:@Before @After @AfterReturning etc.
* 3,TX声明式事务
* */
@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement
@ComponentScan("com.cd.ssm.service")
@PropertySource("classpath:jdbc.properties")
public class ServiceConfig {
@Autowired
DataSource dataSource;
/*
* Spring Jdbc
* */
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
/*Spring提供的事务管理类*/
@Bean
public TransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
Java类配置数据库连接池
package com.cd.ssm.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DatasourceConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/*配置的数据库连接池,这里用的开源的Druid*/
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
jdbc.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=916363
上面代码是jdbc.properties
Java类配置Mybatis信息
package com.cd.ssm.config;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ComponentScan("com.cd.ssm.mapper")
public class MapperJavaConfig {
@Autowired
DataSource dataSource;
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//开启驼峰映射
configuration.setMapUnderscoreToCamelCase(true);
//配置日志输出,记得在resources下面创建Logback.xml, 网上找模板
configuration.setLogImpl(Slf4jImpl.class);
//开启resultMap自动映射
configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setTypeAliasesPackage("com.cd.ssm.pojo");
//配置分页插件
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect","mysql");
pageInterceptor.setProperties(properties);
sqlSessionFactoryBean.addPlugins(pageInterceptor);
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.cd.ssm.mapper");
return mapperScannerConfigurer;
}
}
Java类引入配置java文件初始化项目
package com.cd.ssm.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ApplicationInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/*
* 返回的带有@Configuration注解的类用来配置ContextLoaderListener;
* 根据配置文件搭建Service/Mybatis运行环境
*
* */
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{DatasourceConfig.class,ServiceConfig.class,MapperJavaConfig.class};
}
/*
* 返回的带有@Configuration注解的类用来配置DispatcherServlet
* 根据配置文件搭建Spring MVC环境
* */
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
/*将一个或多个路径映射到DispatcherServlet上*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
项目结构
基本的三层架构,DatasourceConfig如果不单独创建,就读取不到数据连接信息,调试了很久没找到原因