全注解整合SSM框架

目录

1.创建WebAppInitializer配置类

2.配置dispatcherServlet

3.配置Spring IoC

4.配置Mybatis

5.配置映射器

6.创建Mapper接口

7.最后


1.创建WebAppInitializer配置类

继承AbstractAnnotationConfigDispatcherServletInitializer类,配置拦截请求,同时引入dispatcherServlet配置和spring ioc配置

package com.ssm.config;

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

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	//拦截请求配置
	@Override
	protected String[] getServletMappings() {
		
		return new String[] {"*.do"};
	}
	
	//dispatcherServlet环境配置
	@Override
	protected Class[] getServletConfigClasses() {
		
		return new Class[] {WebConfig.class};
	}

	//spring Ioc环境配置
	@Override
	protected Class[] getRootConfigClasses() {
		
		return new Class[] {RootConfig.class};
	}
}

2.配置dispatcherServlet

创建配置类WebConfig.class,其中配置了视图解析器以及自定义适配器。

package com.ssm.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
//定义spring mvc扫描的包
@ComponentScan(value="com.*",includeFilters= {@Filter(type=FilterType.ANNOTATION,value=Controller.class)})
//启动srping mvc配置
@EnableWebMvc
public class WebConfig {
	/*
	 * 通过注解@Bean初始化视图解析器
	 * @return ViewResolver视图解析器
	 */
	@Bean(name="internalResourceViewResolver")
	public ViewResolver initViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/jsp");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}
	/*
	 * 自定义适配器
	 * 初始化RequestMappingHandlerAdapter,并加载Http的Json转换器
	 * @return RequestMappingHandlerAdapter对象
	 */
	@Bean(name="requestMappingHandlerAdapter")
	public HandlerAdapter initRequestMappingHandlerAdapter() {
		//RequestMappingHandlerAdapter
		RequestMappingHandlerAdapter rmha = new RequestMappingHandlerAdapter();
		//http json转换器
		MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
		//MappingJackson2HttpMessageConverter接收json类型消息的转换
		MediaType mediaType = MediaType.APPLICATION_JSON_UTF8;
		List mediaTypes = new ArrayList();
		mediaTypes.add(mediaType);
		//加入转换器的支持类型
		jsonConverter.setSupportedMediaTypes(mediaTypes);
		//往适配器加入json转换器
		rmha.getMessageConverters().add(jsonConverter);
		return rmha;
	}
}

3.配置Spring IoC

创建配置类RootConfig.class,其中配置了数据库连接池、sqlSessionFactory、Mybatis Mapper接口的自动扫描以及注解事务,

其中sqlSessionFactory的配置中又引入了mybatis配置文件mybatis-config.xml

package com.ssm.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

//表明其为一个配置类
@Configuration
//定义spring扫描的包
@ComponentScan("com.*")
//使用事务驱动管理器
@EnableTransactionManagement
//实现接口TransactionManagementConfigurer,这样可以配置注解驱动事务
public class RootConfig implements TransactionManagementConfigurer{
	
	private DataSource dataSource = null;
	/*
	 * 配置数据库
	 * @return 数据库连接池
	 */
	@Bean(name="dataSource")
	public DataSource initDataSource() {
		if(dataSource != null) {
			return dataSource;
		}
		Properties props = new Properties();
		props.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
		props.setProperty("url", "jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false");
		props.setProperty("username", "root");
		props.setProperty("password", "123");
		try {
			dataSource = BasicDataSourceFactory.createDataSource(props);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataSource;
	}
	/*
	 * 配置SqlSessionFactoryBean
	 * @return SqlSessionFactoryBean
	 */
	@Bean(name="sqlSessionFactory")
	public SqlSessionFactoryBean initSqlSessionFactory() {
		
		SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
		sqlSessionFactory.setDataSource(initDataSource());
		//配置mybatis配置文件
		Resource resource = new ClassPathResource("com/ssm/dao/mybatis-config.xml");
		sqlSessionFactory.setConfigLocation(resource);
		return sqlSessionFactory;
	}

	/*
	 * 通过自动扫描,发现Mybatis Mapper接口
	 * @return Mapper扫描器
	 */
	@Bean
	public MapperScannerConfigurer initMapperScannerConfigurer() {
		
		MapperScannerConfigurer msc = new MapperScannerConfigurer();
		//扫描包
		msc.setBasePackage("com.*");
		msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
		//区分注解扫描
		msc.setAnnotationClass(Repository.class);
		return msc;
	}
	/*
	 * 实现接口方法,注册注解事务,当@Transactional使用的时候产生数据库事务
	 */
	@Override
	@Bean(name="annotationDrivenTransactionManager")
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		
		DataSourceTransactionManager transactionManager =  new DataSourceTransactionManager();
		transactionManager.setDataSource(initDataSource());
		return transactionManager;
	}

}

4.配置Mybatis

创建mybatis配置文件mybatis-config.xml,其中包含了一些基础设置,别名配置,以及最后引入了映射器配置文件(mapper)

  
 

	
		
		
		
		
		
		
		
		
		
		
	
	
	
		
	
	
	
		
		
		
	

5.配置映射器

根据你的实体类创建映射器,sql语句都需要写在映射器中,映射器根据namespae和id定位到你的sql语句并执行数据库操作

  
 


	
	
	
	
		insert into t_role (role_name,note)
		values (#{roleName},#{note})
	

	
		update t_role set
		role_name = #{roleName},
		note = #{note}
		where id = #{id}
	

	
		delete from t_role where id = #{id}
	
	

6.创建Mapper接口

要操作数据库,光有映射器还不够,还需要对应的接口,接口名即为映射器中的namespace,接口中的方法即为映射器中sql语句的id名

package com.ssm.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.ssm.pojo.Role;

@Repository
public interface RoleDao {

	public Role getRole(Long id);
	public int deleteRole(Long id);
	public int insertRole(Role role);
	public int updateRole(Role role);
	public List findRoles(@Param("roleName") String roleName,@Param("note") String note);
}

7.最后

到此为止SSM整合基本的配置就完成了,当然作为Web项目,接下来就是对Controller和Service基于业务的开发了。。。。 

 

你可能感兴趣的:(java)