做SSM项目时犯的错误与总结

1. 报错Cannot resolve reference to bean 'sessionFactory' while setting bean property

原因:我写的XXXMapper接口继承了Mapper<T>,即有了如insert(), delete(), selectByPrimaryKey()这样的函数,虽然这些函数我们在XXXMapper接口中看不见,但是已经有了,此时如果再在XXXMapper.xml文件中写入id为已存在函数的名字的sql语句,就会出现这个错误。

 

2. 报错java.lang.IllegalStateException: Failed to load ApplicationContext

Cause by:Error creating bean with name 'mappingJacksonHttpMessageConverter'

原因:maven中引入的jar包不匹配。

如果光是Failed to load ApplicationContext有可能是你又写了一个xml但没有在@ContextConfiguration(locations={})中引入。

 

3. $("#XXX").val();中的XXX是标签中的id

 

4. input标签中的name值要与Controller层的@RequestParam中的value值(当参数只有一个时,“value=”可省去)对应

 

5. 通过applicationContext .getBean(“xxx”); 可以获得注解注入的bean

    但是set注入只能用ClassPathXmlApplicationContext取而不能用@Autowired!!!

    用@Autowired取的只能是@Component/@Service/@Repository/@Controller注解的类!!!

    但是可以新增一个配置类,用@ImportResource()导入xml配置,就可以用@Autowired拿到xml中的bean了。

@ImportResource("classpath:beans.xml")
@Configuration
public class BeansConfig {
    //beans.xml中有<bean id="hello" class="com.example.yang.bean.Hello"/>
}
@RestController
public class TestController {
    
    @Autowired
    private Hello hello; 
    //因为在配置类中@ImportResource了xml,配置类有有@Configuration,所以资源已经import进来了
    
    
    @RequestMapping("/hello")
    public String say() {
        return hello.say("小明");
    }
}

    Spring的Test类中@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring-mvc.xml"})应该也是一样,已经将xml加载进来,而xml中有<context:component-scan base-package="com.wu"/>,也就相当于把这个包中有注解的类加载进来,可以直接用Autowired。

 

6. java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

如果数据库连接(class driver,连接,用户名,密码)都没有写错,那很可能是db.properties没有真正被引进applicationContext.xml。用<context:property-placeholder location="classpath:db.properties"/>引入无效,在数据源DataSource那里直接写数据库连接参数就发现没有这个错误了。

 

7. mybatis异常:Invalid bound statement (not found)

如果xml中mapper的命名空间没写错,对应函数名也没有写错,那就很可能是没有把XXXmapper.xml文件引入,应该在pom文件中加入:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

 

8. 只要用了resultMap,即使数据库字段和实体类字段对应或者开启了驼峰命名规则,也必须全部写<result property="" column="">,否则实体类得到的字段为null

 

9. java.sql.SQLSyntaxErrorException: Unknown column 'ROWNUM' in 'field list'

这句话的意思是没有ROWNUM这个列,如果使用了pagehelper,很可能是数据库类型配置错误。我使用的是mysql数据库,没有ROWNUM这个列,但是我配置pagehelper时配置成了oracle数据库,这是改成mysql就好:

<property name="plugins">
    <array>
        <!-- 传入插件的对象 -->
        <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
                <props>
                    <prop key="helperDialect">mysql</prop>
                    <prop key="reasonable">true</prop>
                </props>
            </property>
         </bean>
    </array>
</property>

 

10. 通用 Mapper 的 selectByPrimaryKey 方法无法识别 int 类型,需要在 POJO 类中将 int 改为包装类型 Integer或者Long

 

11.映射文件中的where标签可以过滤掉条件语句中的第一个and或or关键字

 

12.@Import注解是引入java类,@ImportResource是引入spring配置文件.xml

 

13.静态变量不能直接注入,要另开一个类并加@Component,然后在其中写set方法并在方法上注解@Autowired,之后调用set方法

 

14.如果一个类的成员变量用了@Autowired,那么那个类必须加注解注入Spring容器。而不是new出来,new出来的话,那个类中注入的成员变量会为null

 

未完待续。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值