qq_20391065
念桥丶
采纳率16.7%
2015-05-14 09:22 阅读 1.9k

springmvc+mybatis整合

spring-servlet.xml

<beans:bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
       <beans:property name="driverClassName" value="org.postgresql.Driver"/> 
       <beans:property name="url" 
           value="jdbc:postgresql://192.168.20.61:5432/atomdb"/> 
       <beans:property name="username" value="postgre"/> 
       <beans:property name="password" value="postgre"/> 
    </beans:bean> 
    <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
       <beans:property name="dataSource" ref="myDataSource"/> 
       <beans:property name="configLocation" value="/WEB-INF/db/SqlMapConfig.xml"/> 
    </beans:bean>
    <beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
       <beans:property name="basePackage" value="Mapper"/> 
    </beans:bean> 
    <beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
       <beans:property name="dataSource" ref="myDataSource"/> 
    </beans:bean>


SqlMapConfig.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <mappers>
        <mapper resource="ApiServiceRepoMapper.xml" />
    </mappers>

</configuration>

ApiServiceRepoMapper.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shangpin.EChars.repo.ComplexQueryRepo">

        <select id="count" resultType="Integer">
            SELECT "count"(*) FROM "public"."search_keyword_statistic_day"
        </select>
        </mapper>

Controller.java

 @Controller
@RequestMapping("/search")
public class Controller {

    @Autowired
    private SearchService searchService ;

    @RequestMapping("/get")
    public void ss(){

        System.out.println(searchService.count());
    }
}

SearchService.java

 @Service
public class SearchService {
    @Autowired
    ComplexQueryRepo complexQueryRepo;

    @Autowired
    public int count(){
        System.out.println("222");
        int a = complexQueryRepo.count();
        return a;
    }
}

ComplexQueryRepo.java

 public interface ComplexQueryRepo{
    public int count();
}

项目能启动,发送请求/search/get后会输出222然后

    int a = complexQueryRepo.count();

这一行报空指针

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • zhangjing7809 晶晶爱学习 2015-05-14 09:34

    先不问你注解扫描是否开启
    其他的代码问题太多了,springmvc用的动态代理,所有你的service要个接口,然后实现接口,然后注入,dao也是同样的
    @Autowired
    public int count(){
    System.out.println("222");
    int a = complexQueryRepo.count();
    return a;
    }
    还有这,@Autowired这个是自动注入,就是将容器里的bean注入,所有这行去了

    点赞 评论 复制链接分享
  • chentravelling 生活没有if-else 2015-05-14 10:13

    借口里的count()实现在哪里?不是class SearchService里那个count()~

    点赞 评论 复制链接分享
  • shuibaicai shuibaicai 2015-05-15 09:35

    select语句 count怎么加引号

    点赞 评论 复制链接分享

相关推荐