记录学习的点滴(Spring+MyBatis注解配置)
2016-09-18 13:58
447 查看
声明:
1,搭建框架使用的Spring+MyBatis。
2,JDK版本1.7以上不需要添加架包【common-annotations.jar】。
原先非注解配置的配置文件【applicationContext.xml】:
<bean class>指向的都是接口的实现类。
source的文件结构如下,只是controller,service,dao的层次,其他的就没贴。
注解的方式来配置Spring,先上配置文件。
配置文件的开头 要把以下内容添加,不然就会识别不了类似于这种<context:annotation-config>注解的标签。
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"[/code]
版本号可以不用写的,不写的话就使用最新版。
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>
相关文章推荐
- Spring学习记录(2) Spring MVC+Mybatis 注解配置
- MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)
- Spring学习记录(十二)---AOP理解和基于注解配置
- 记录学习的点滴(Spring事务注解方式)
- 记录学习的点滴(Eclipse+Spring+Mybatis搭建问题总结)
- 记录学习的点滴(Spring+Mybatis+Atomikos+Tomcat实现分布式事务管理)
- 《Spring+Mybatis》Mybatis简单注解配置的用法学习笔记与代码
- 记录学习的点滴(Mybatis配置注册Mapper的方式)
- 记录学习的点滴(Spring注解学习过程中问题总结)
- 记录学习的点滴(MyBatis log4j日志输出配置)
- Spring学习笔记(六)注解方式配置事物
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql .
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- Spring3.1 MVC注解配置学习
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置