SpringMVC整合,出现注解注解没有起作用

在spring的applicationContext.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-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--启动注解-->
    <context:annotation-config />
    <!--  base-package 注解所在的包根据自己的需要划分注解类使用的范围  -->
    <context:component-scan base-package="main.com.talkweb">
        <!--不再管理Controller 注解  因为它单独给mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

</beans>

明明有启动注解,但有些注解还是没起作用

<!--启动注解-->
    <context:annotation-config />

后来发现原因是:下面这句作与springMVC的controller注解的排除关注时,把扫描注解的返回局限了
原来 base-package=”main.com.talkweb.Controller”:

!--  base-package 注解所在的包根据自己的需要划分注解类使用的范围  -->
    <context:component-scan base-package="main.com.talkweb.Controller">
        <!--不再管理Controller 注解  因为它单独给mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

现在的包范围 base-package=“main.com.talkweb”:

!--  base-package 注解所在的包根据自己的需要划分注解类使用的范围  -->
    <context:component-scan base-package="main.com.talkweb">
        <!--不再管理Controller 注解  因为它单独给mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

刚学习会出现各种各样的问题,分享出来相互学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC和MyBatis的整合可以通过注解来完成。下面是一个简单的示例代码,演示了如何在Spring MVC项目中使用纯注解方式整合MyBatis。 首先,确保你的项目中已经加入了Spring MVC和MyBatis的依赖。然后按照以下步骤进行配置。 1. 创建数据源配置类 ```java @Configuration public class DataSourceConfig { @Bean public DataSource dataSource() { // 配置数据源,例如使用HikariCP等 return new HikariDataSource(); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); // 配置MyBatis扫描路径 sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); return sessionFactoryBean.getObject(); } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer(); scannerConfigurer.setBasePackage("com.example.mapper"); return scannerConfigurer; } } ``` 2. 创建MyBatis配置类 ```java @Configuration public class MyBatisConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3. 创建控制器(Controller)和服务(Service)类 ```java @Controller @RequestMapping("/example") public class ExampleController { @Autowired private ExampleService exampleService; @RequestMapping("/list") public String list(Model model) { List<Example> examples = exampleService.getAllExamples(); model.addAttribute("examples", examples); return "example/list"; } } @Service public class ExampleService { @Autowired private ExampleMapper exampleMapper; public List<Example> getAllExamples() { return exampleMapper.getAllExamples(); } } ``` 4. 创建MyBatis Mapper接口和XML映射文件 ```java public interface ExampleMapper { List<Example> getAllExamples(); } ``` ```xml <!-- src/main/resources/mapper/ExampleMapper.xml --> <mapper namespace="com.example.mapper.ExampleMapper"> <select id="getAllExamples" resultType="com.example.model.Example"> SELECT * FROM example </select> </mapper> ``` 以上是一个简单的示例,展示了如何使用纯注解方式在Spring MVC项目中整合MyBatis。你可以根据自己的需求进行相应的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值