記錄學習的點滴(Spring+MyBatis註解配置)

聲明:
1,搭建框架使用的Spring+MyBatis。
2,JDK版本1.7以上不需要添加架包【common-annotations.jar】。
原先非註解配置的配置文件【applicationContext.xml】:
	<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>
<bean class>指向的都是接口的實現類。
source的文件結構如下,只是controller,service,dao的層次,其他的就沒貼。

註解的方式來配置Spring,先上配置文件。
	<!-- 開啓註解 -->
	<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>
	-->
配置文件的開頭 要把以下內容添加,不然就會識別不了類似於這種<context:annotation-config>註解的標籤。
    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層【UserServiceImpl.java】代碼:
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
}
Dao層【UserDaoImpl.java】代碼:
@Repository
public class UserDaoImpl  extends SqlSessionDaoSupport implements UserDao {
	
	// 1,重寫父類 【SqlSessionDaoSupport】方法實現注入【sqlSessionFactory】。
	// 2,必須要有,在配置文件【applicationContext.xml】要配置過。
	@Resource	
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
		super.setSqlSessionFactory(sqlSessionFactory);
	}
}
註解方式的source的文件結構如下:
與非註解方式相比多個【UserDao.java】的接口實現類。
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>





發佈了37 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章