当前位置:首页 > 开发 > 开源软件 > 正文

【持久化框架MyBatis3八】Spring集成MyBatis3

发表于: 2014-09-07   作者:bit1129   来源:转载   浏览:
摘要: pom.xml配置 Maven的pom中主要包括: MyBatis MyBatis-Spring Spring MySQL-Connector-Java Druid applicationContext.xml配置   <?xml version="1.0" encoding="UTF-8"?> &

pom.xml配置

Maven的pom中主要包括:

  • MyBatis
  • MyBatis-Spring
  • Spring
  • MySQL-Connector-Java
  • Druid

applicationContext.xml配置

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
						http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
	
	<context:annotation-config />

	<context:component-scan base-package="com.mybatis3" />

	<context:property-placeholder location="classpath:application.properties" />
	
    <!--定义TransactionManager,在业务层使用@Transactional注解使Service成为事务管理的service,比如StudentService-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  		<property name="dataSource" ref="dataSource" />
	</bean>
	
    <!--SqlSessionTemplate实现了SqlSession接口,线程安全,可以单例使用,-->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	  <constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	<!--sqlSessionFactory与主配置文件对应,可以直接使用configLocation指定主配置文件-->
    <!--也可以对主配置文件中的配置项单独配置-->
   	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  		<property name="dataSource" ref="dataSource" />
  		<property name="typeAliasesPackage" value="com.mybatis3.domain"/>
  		<property name="typeHandlersPackage" value="com.mybatis3.typehandlers"/>
  		<property name="mapperLocations" value="classpath*:com/mybatis3/**/*.xml" /> <!--指向映射配置文件-->
	</bean>
	
    <!--数据源配置,在sqlSessionFactory引用-->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
</beans>
 
  得到sqlSession对象,其它的操作区别不大,比如TutorService的实现
 
/**
 * 
 */
package com.mybatis3.services;

import com.mybatis3.domain.Tutor;
import com.mybatis3.mappers.TutorMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author Siva
 *
 */
@Service
@Transactional
public class TutorService 
{
	@Autowired
	private SqlSession sqlSession; //自动注入Spring定义的SqlSession
	
	private TutorMapper getTutorMapper(){
		return sqlSession.getMapper(TutorMapper.class);//通过sqlSession获得相应的Mapper
	}
	public List<Tutor> findAllTutors() {
		return getTutorMapper().findAllTutors();
	}
	
	public Tutor findTutorById(int tutorId) {
		return getTutorMapper().findTutorById(tutorId);
	}
	
	public Tutor findTutorByNameAndEmail(String name, String email) {
		return getTutorMapper().findTutorByNameAndEmail(name, email);
	}
	
	public Tutor createTutor(Tutor tutor) {
		getTutorMapper().insertTutor(tutor);
		return tutor;
	}
	
	public Tutor updateTutor(Tutor tutor) {
		getTutorMapper().updateTutor(tutor);
		return tutor;
	}
	
	public boolean deleteTutor(int tutorId) {
		boolean deleted = false;
		int nor = getTutorMapper().deleteTutor(tutorId);
		deleted = (nor == 1);
		return deleted;
	}
	
	public Tutor selectTutorById(int tutorId) {
		return getTutorMapper().selectTutorById(tutorId);
	}
	
	public Tutor selectTutorWithCoursesById(int tutorId) {
		return getTutorMapper().selectTutorWithCoursesById(tutorId);
	}
}
 
 
 
 
 
 

【持久化框架MyBatis3八】Spring集成MyBatis3

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
MyBatis的主配置文件对MyBatis进行配置和定制,包括 全局属性定义(properties) MyBatis的行为设置(s
MyBatis的主配置文件对MyBatis进行配置和定制,包括 全局属性定义(properties) MyBatis的行为设置(s
MyBatis的主配置文件对MyBatis进行配置和定制,包括 全局属性定义(properties) MyBatis的行为设置(s
转载请注明: TheViper http://www.cnblogs.com/TheViper autoMappingBehavior是一个容易被忽略的属
开发环境: System:Windows WebBrowser:IE6+、Firefox3+ JavaEE Server:tomcat5.0.2.8、tomcat6
开发环境: System:Windows WebBrowser:IE6+、Firefox3+ JavaEE Server:tomcat5.0.2.8、tomcat6
开发环境: System:Windows WebBrowser:IE6+、Firefox3+ JavaEE Server:tomcat5.0.2.8、tomcat6
1.pom.xml文件增加mybatis3的jar包如下,本人用的spring版本为4.1.6.RELEASE !-- 添加mybatis的核心
目录结构: 步骤: 1、创建User表 DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(1
您还未登录 ! 登录 注
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号