您的位置:首页 > 编程语言 > Java开发

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

2016-09-18 13:58 447 查看
声明:
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>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: