springmvc整合mybatis怎么配置就可以直接用注解了呢?
java吧
全部回复
仅看楼主
level 10
这两句话可以用什么来替代呢????求助
public ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"mybatis-spring.xml","spring.xml"});
StudentService studentService =(StudentService)ac.getBean("studentService");
2014年11月09日 15点11分 1
level 10
在控制器中如果不写这两句话就报错:没有依赖关系,求个方案
2014年11月09日 15点11分 2
level 10
走过路过的都来看一眼呗
   --谁要是欺负我,我就把他挂在小尾巴上!!!
2014年11月09日 15点11分 3
level 12
测试是这样写吧
2014年11月09日 15点11分 4
注解运行出错,那就是没有扫描到包吗
2014年11月09日 15点11分
回复 西门_吹雪pj :开启扫描了吗
2014年11月09日 15点11分
回复 小茫的守候 :只扫描了包,在没有定义bean
2014年11月09日 16点11分
level 12
可以直接注解的
2014年11月09日 15点11分 5
注解service依赖不嫩会找到
2014年11月09日 15点11分
回复 西门_吹雪pj :要开启扫描指定的包下的所以注解,还要定义一个接口annotion用来声明注解接口,在控制器中住宿service
2014年11月09日 15点11分
回复 小茫的守候 :我只在spring中扫描service包,在spring~mybatis中定义了sqlsessionfactory,同时扫描mapper下的xml文件,还有一个扫描到接口,就这些了,你看还差啥
2014年11月09日 16点11分
level 10
没人马???
2014年11月10日 06点11分 6
level 10
spring.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
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<context:annotation-config />
<context:component-scan base-package="com.spring.service" />
<import resource="mybatis-spring.xml"/>
</beans>
@创世中文网魔孩
2014年11月10日 07点11分 10
level 10
spring-mybatis的配置:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="minIdle" value="1" />
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="mergeStat" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/spring/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com/spring/dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="query" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.spring.service.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
<property name="patterns">
<list>
<value>com.spring.service.impl</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>
@创世中文网魔孩
2014年11月10日 07点11分 11
回复 创世中文网魔孩 :感觉是找不到上下文,你看看,下楼的注解
2014年11月10日 08点11分
level 10
这样就能运行:
@Controller@RequestMapping(value="/test")
public class StudentController {
public ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"mybatis-spring.xml","spring.xml"});
@RequestMapping(value="/student",method=RequestMethod.GET)
public String test(){
StudentService studentService =(StudentService)ac.getBean("studentService");
Student stu = studentService.getStudentbysno("120406324");
System.out.println(stu.getSname());
List<Student> list = studentService.getAll();
for (Student l:list){
System.out.println(l);
}
return "index";
}
}
2014年11月10日 07点11分 12
level 10
这样就不能运行了:
@Controller
@RequestMapping(value="/test")
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping(value="/student",method=RequestMethod.GET)
public String test()
Student stu = studentService.getStudentbysno("120406324");
System.out.println(stu.getSname());
List<Student> list = studentService.getAll();
for (Student l:list){
System.out.println(l);
}
return "index";
}
}
2014年11月10日 07点11分 13
level 10
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.test.dao.StudentMapper;
import com.test.model.Student;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Override
public Student getStudentbysno(String sno) {
return this.studentMapper.selectByPrimaryKey(sno);
}
@Override
public List<Student> getAll(){
return this.studentMapper.getAll();
}
}
@s15951044971
2014年11月10日 08点11分 17
level 12
context:component-scan base-package="com.spring.service"
spring只会扫描service下的包,dao未扫描到
2014年11月10日 08点11分 21
那我是不在下面在写个扫描dao接口就可以了呢?
2014年11月10日 09点11分
回复 西门_吹雪pj :一般是扫描全包的吧配置:context:component-scan base-package="com.spring"
2014年11月10日 10点11分
回复 zhou686269 :吆西,我是一次
2014年11月10日 10点11分
回复 zhou686269 :扫描全包后,根本控制台上停不下来,我感觉是控制器找不到xml配置文件引起的
2014年11月10日 10点11分
level 7
你这个东西 弄好几天 了 。。。。。。
为什么不先学 IOC
IOC 理解了 就不会有 这种问题了。。。
2014年11月10日 10点11分 23
弄明白了
2014年11月10日 10点11分
level 10
成功了,我在springmvc的配置文件中加上了一句话<import resource="spring.xml">,在spring.xml中加了<import source="mybatis_spring.xml">后注解能用了
@tobeleaderman @zhou686269 @s15951044971 @创世中文网魔孩
谢谢你们的启发
2014年11月10日 10点11分 24
1