记录学习的点滴(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层【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】的代码如下:
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
- 记录学习的点滴(Spring+MyBatis注解配置)
- 记录学习的点滴(Spring事务注解方式)
- 记录学习的点滴(MyBatis log4j日志输出配置)
- 记录学习的点滴(Spring多数据源配置)
- 记录学习的点滴(Spring注解学习过程中问题总结)
- 记录学习的点滴(Eclipse+Spring+Mybatis搭建问题总结)
- 记录学习的点滴(Spring+Mybatis+Atomikos+Tomcat实现分布式事务管理)
- 记录学习的点滴(Mybatis配置注册Mapper的方式)
- 记录学习的点滴(MyBatis缓存机制学习)
- 记录学习的点滴(MyBatis中#{}与${}区别)
- 记录学习的点滴(Spring Bean的生命周期)
- 记录学习的点滴(Spring设计模式)
- 记录学习的点滴(Struts2的Action配置)
- spring学习笔记(点滴记录)
- spring、springmvc、mybatis整合配置(注解)
- 记录学习的点滴
- 记录学习的点滴
- 记录学习的点滴(Eclipse创建Maven项目+MyBatis+Redis)
- Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击
- 为什么要多用组合少用继承?
- 初涉MySQL操作(三)
- HashMap源码
- Java8系列之重新认识HashMap
- 记录学习的点滴(Spring+MyBatis注解配置)
- HashMap扩容
- 33.81.Search in Rotated Sorted Array
- 工厂模式和抽象工厂模式
- Spring IOC源码-bean加载
- 【Leetcode】400. Nth Digit
- Spring IOC 源码-ClassPathXmlApplicationContext-bean解析
- Spring-BeanPostProcessor
- 几种单例创建的方式