配置springMVC
注解
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedistemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author : Edward_wei
* @data: 2021/6/14 20:36
* @classNam: ControllerConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration//表明这是一个配置类
@ComponentScan(basePackages = {"com.holy.cn.controller"})//配置controller层的包扫描
@EnableWebMvc //开启controller层的注解驱动
public class ControllerConfig implements WebMvcConfigurer {//实现这个接口才可以配置mvc特有的一些配置
/**
* 设置拦截器
* <mvc:interceptor/>
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())//添加拦截器
.addpathPatterns("/**")//添加匹配路径
.excludePathPatterns("/getCode")//排除路径
.excludePathPatterns("/verifyLogin");
}
/**
* 创建了StringRedistemplate的模板对象,这玩意只能整字符串,但是放里面不乱吗,整其他的还得redistemplate
* @return
*/
@Bean
public StringRedistemplate getStringRedistemplate(){
StringRedistemplate stringRedistemplate = new StringRedistemplate();
stringRedistemplate.setConnectionFactory(getRedisConnectionFactory());//把连接工厂给放进去
return stringRedistemplate;
}
/**
* spring-data-redis的连接工厂,创建Redis必须得这个连接方式
* @return
*/
@Bean
public RedisConnectionFactory getRedisConnectionFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();//上连接池
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setMaxTotal(50);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.setHostName("localhost");
return jedisConnectionFactory;
}
/**
* <mvc:cors/>
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")//匹配所有路径
.allowedOrigins("*")//匹配所有的请求源
.allowedHeaders("*")//匹配所有请求头
.allowedMethods("*")//匹配所有请求方法
.allowCredentials(true);//允许跨域请求携带cookie
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描controller-->
<context:component-scan base-package="com.holy.controller"/>
<!-- 开启注解驱动-->
<mvc:annotation-driven/>
<!-- 开启静态资源不拦截-->
<mvc:default-servlet-handler/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".html"/>
<property name="prefix" value="/WEB-INF/pages/"/>
</bean>
<!-- 配置所有拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/login.html"/>
<mvc:exclude-mapping path="/verify_login"/>
<mvc:exclude-mapping path="/toVerify/*"/>
<mvc:exclude-mapping path="/verify_login"/>
<mvc:exclude-mapping path="/sendActiveLink"/>
<mvc:exclude-mapping path="/successActiveAndJumpToLogin"/>
<mvc:exclude-mapping path="/toHome"/>
<mvc:exclude-mapping path="/css/*"/>
<mvc:exclude-mapping path="/fonts/*"/>
<mvc:exclude-mapping path="/js/*"/>
<mvc:exclude-mapping path="/layui/*"/>
<mvc:exclude-mapping path="/images/*"/>
<bean class="com.holy.controller.MyInterceptor"/>
</mvc:interceptor>
<!-- 符合规则的干进来-->
</mvc:interceptors>
<!-- 配置跨域-->
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>
</beans>
配置service层spring
注解
import com.holy.cn.MapperConfig;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author : Edward_wei
* @data: 2021/6/14 20:22
* @classNam: ServiceConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration
@ComponentScan(basePackages = {"com.holy.cn.service"})//开启service包扫描
@EnableTransactionManagement//开启事务注解驱动
public class ServiceConfig {
@Autowired
MapperConfig mapperConfig;
@Bean("transactionManager")//设置事务数据源
public DataSourceTransactionManager getDataSourceTransactionManager(){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(mapperConfig.getDatasource());
return dataSourceTransactionManager;
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/>
<!-- 配置service层-->
<!--开启service包扫描-->
<context:component-scan base-package="com.holy.service"/>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解驱动-->
<tx:annotation-driven/>
<!-- 配置邮件bean-->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<property name="host" value="${mail.host}"/>
</bean>
</beans>
mapper层spring
注解
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pageHelper.PageInterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.sqlSessionfactorybean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ResourceBundle;
/**
* @author : Edward_wei
* @data: 2021/6/14 19:25
* @classNam: MapperConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration
public class MapperConfig {
//配数据源
@Bean
public DruidDataSource getDatasource(){
ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(resourceBundle.getString("MysqL.Driver"));
druidDataSource.setUrl(resourceBundle.getString("MysqL.url"));
druidDataSource.setUsername(resourceBundle.getString("MysqL.username"));
druidDataSource.setPassword(resourceBundle.getString("MysqL.password"));
return druidDataSource;
}
//配sqlsessionfactory
@Bean
public sqlSessionfactorybean getsqlSessionfactorybean(){
sqlSessionfactorybean sqlSessionfactorybean = new sqlSessionfactorybean();
sqlSessionfactorybean.setDataSource(getDatasource());
//配置mybatis
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);//设驼峰
configuration.setLogImpl(Log4jImpl.class);
sqlSessionfactorybean.setConfiguration(configuration);
//配置分页
sqlSessionfactorybean.setPlugins(new PageInterceptor());
return sqlSessionfactorybean;
}
//配置mapper层包扫描
@Bean
public MapperScannerConfigurer setMapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.holy.cn.mapper");
return mapperScannerConfigurer;
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${MysqL.Driver}"/>
<property name="url" value="${MysqL.url}"/>
<property name="password" value="${MysqL.password}"/>
<property name="username" value="${MysqL.username}"/>
</bean>
<!--配置mapper层-->
<bean id="sqlSessionfactorybean" class="org.mybatis.spring.sqlSessionfactorybean">
<property name="dataSource" ref="dataSource"/>
<!-- 引入mybatis的配置-->
<property name="configuration" ref="configuration"/>
<!-- 分页-->
<property name="plugins">
<bean class="com.github.pageHelper.PageInterceptor">
</bean>
</property>
</bean>
<!-- 原先为mybatis配置文件中的mapper层扫描-->
<bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.holy.mapper"/>
</bean>
<!-- mybatis的配置-->
<bean id="configuration" class="org.apache.ibatis.session.Configuration">
<!-- 开启驼峰-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</beans>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。