记录学习的点滴(Spring+MyBatis注解配置)

来源:互联网 发布:sql insert 语句例子 编辑:程序博客网 时间:2024/06/06 01:35
声明:
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@RequestMappingpublic class LoginControllerImpl implements LoginController {@Autowiredprivate UserService userService;@Override@RequestMapping("/login.do")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {}}
Service层【UserServiceImpl.java】代码:
@Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;}
Dao层【UserDaoImpl.java】代码:
@Repositorypublic class UserDaoImpl  extends SqlSessionDaoSupport implements UserDao {// 1,重写父类 【SqlSessionDaoSupport】方法实现注入【sqlSessionFactory】。// 2,必须要有,在配置文件【applicationContext.xml】要配置过。@Resourcepublic 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>





1 0