<bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.springmybatis.system.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="service" class="com.springmybatis.system.service.UserServiceImpl">
<property name="userDao" ref="dao"></property>
</bean>
<bean id="loginController" class="com.springmybatis.system.control.LoginControllerImpl">
<property name="userService" ref="service"></property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">loginController</prop>
</props>
</property>
</bean>
<!-- 開啓註解 -->
<context:annotation-config/>
<!-- base-package指向註解要掃描的包 -->
<context:component-scan base-package="com.springmybatis.system"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 原先的配置文件就用不到,註釋掉 -->
<!--
<bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.springmybatis.system.dao.UserDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="service" class="com.springmybatis.system.service.UserServiceImpl">
<property name="userDao" ref="dao"></property>
</bean>
<bean id="loginController" class="com.springmybatis.system.control.LoginControllerImpl">
<property name="userService" ref="service"></property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">loginController</prop>
</props>
</property>
</bean>
-->
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
Controller層【LoginControllerImpl.java】代碼:
@Controller
@RequestMapping
public class LoginControllerImpl implements LoginController {
@Autowired
private UserService userService;
@Override
@RequestMapping("/login.do")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
}
@Repository
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
// 1,重寫父類 【SqlSessionDaoSupport】方法實現注入【sqlSessionFactory】。
// 2,必須要有,在配置文件【applicationContext.xml】要配置過。
@Resource
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
super.setSqlSessionFactory(sqlSessionFactory);
}
}
註解方式的source的文件結構如下:sqlSessionFactory在【applicationContext.xml】的代碼如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>