问题原因为 mvc:annotation driven引错了约束

记录下,只需要在开始在初始化时读取springmvc的配置文件即可,不需要错综复杂的操作
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app
然后Spring部分,搞个配置类,导入mybatis相关的组件
@ComponentScan(basePackages = "com.sifa")
@Configuration
public class MybatisAndResourceConfig{
@Bean("datasource")
public DruidDataSource getDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfig(){
MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("getSqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.sifa.dao");
return mapperScannerConfigurer;
}
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
本文详细描述了在Spring MVC中遇到的由于@AnnotationDriven配置不当导致的约束错误,并提供了初始化时正确配置Spring MVC和MyBatis的方法,包括web.xml中DispatcherServlet的设置和Spring配置类的编写实例。
271

被折叠的 条评论
为什么被折叠?



