总结MyBatis+Spring的整合

第一步:

首先jar包导入。必要jar包,即缺失了就会不能正常启动,若需要用到额外功能,导入相关jar包即可

commons-logging-1.2.jar

log4j.jar

spring-aop-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

spring-jdbc-4.0.0.RELEASE.jar

spring-tx-4.0.0.RELEASE.jar

mybatis-3.4.6.jar

ant-1.9.6.jar

asm-5.2.jar

cglib-3.2.5.jar

slf4j-api-1.7.25.jar

slf4j-log4j12-1.7.25.jar

依赖包:

mybatis-spring-1.3.2.jar

aopalliance-1.0.jar

第二步:

beans类创建,以及数据表的创建

第三步:

测试类的创建:

package test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.School;
import beans.Teacher;
import service.IPublicService;

public class unit {
    IPublicService service;
    @Before
    public void before() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        service = (IPublicService)context.getBean("service");
    }
    @Test
    public void test_selectSchool() {
        School school=new School();
        school.setSid(2);
        School schools = service.selectSchool(school);
        System.out.println(schools.toString());
    }
    @Test
    public void test_selectTeacher() {
        Teacher teacher=new Teacher();
        teacher.setTid(3);
        Teacher selectTeacher = service.selectTeacher(teacher);
        System.out.println(selectTeacher.toString());
    }
}

第四步:

创建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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			http://www.springframework.org/schema/beans/spring-beans.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd
			http://www.springframework.org/schema/tx
			http://www.springframework.org/schema/tx/spring-tx.xsd">
			<!-- 配置spring默认数据源 -->
		
		
		
		<!-- 配置数据源 -->
		<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
			<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
			<property name="url" value="jdbc:mysql:///test"></property>
			<property name="username" value="root"></property>
			<property name="password" value="1314"></property>
		</bean>
		<!--  注册sqlsessionFactory-->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="myDataSource"/>
			<property name="configLocation" value="classpath:mybatis.xml"></property>
		</bean>
		<!-- 配置dao -->
		<bean id="SchoolDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
			<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
			<property name="mapperInterface" value="dao.ISchoolDao"/>
		</bean>
		<bean id="TeacherDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
			<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
			<property name="mapperInterface" value="dao.ITeacherDao"/>
		</bean>
		<!--  配置service-->
		<!-- 使用到了IOC以及DI设值注入 -->
		<bean id="service" class="service.PublicServiceImpl">
			<property name="schoolDao" ref="SchoolDao"/>
			<property name="teacherDao" ref="TeacherDao"/>
		</bean>
		
		
	<!-- ===================================事务======================================= -->
	<!-- 添加事务管理器 -->
	<bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="myDataSource"/>
	</bean>
	<!--  注册事务通知-->
	<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
		<tx:attributes>
			<!-- 设置每个连接点上所应用的事物属性 -->
			<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
			<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED"/>
			<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED"/>
			<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!--  aop的配置-->
	<aop:config>
		<!-- 指定切入点 -->
		<aop:pointcut expression="execution(* *..service.*.*(..))" id="myPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
	</aop:config>
</beans>












第五步:

创建service接口以及实现类

package service;

import beans.School;
import beans.Teacher;
import dao.ISchoolDao;
import dao.ITeacherDao;

public interface IPublicService {
	School selectSchool(School school);
	Teacher selectTeacher(Teacher teacher);
}


package service;

import beans.School;
import beans.Teacher;
import dao.ISchoolDao;
import dao.ITeacherDao;

public class PublicServiceImpl implements IPublicService {
	private ISchoolDao schoolDao;
	private ITeacherDao teacherDao;
	public void setSchoolDao(ISchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}
	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}
	@Override
	public School selectSchool(School school) {
		// TODO Auto-generated method stub
		return schoolDao.selectSchool(school);
	}

	@Override
	public Teacher selectTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		return teacherDao.selectTeacher(teacher);
	}
	
}

第六步:

创建dao类,由于这里用到动态代理,所以不需要创建其实现类

package dao;

import beans.School;

public interface ISchoolDao { 
    School selectSchool(School school); 
}


package dao;

import beans.Teacher;

public interface ITeacherDao { 
    Teacher selectTeacher(Teacher teacher); 
}

第七步:

创建对应的映射文件

ISchoolDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.ISchoolDao">
	<resultMap type="School" id="schoolResultMap">
		<id column="sid" property="sid"/>
		<result column="sname" property="sname"/>
		<result column="address" property="saddress"/>
		<collection property="teachers" ofType="Teacher">
			<id column="tid" property="tid"/>
			<result column="tname" property="tname"/>
			<result column="age" property="tage"/>
		</collection>
	</resultMap>
	<select id="selectSchool" resultMap="schoolResultMap">
		select sid,sname,address,tid,tname,age from school,teacher where sid=#{sid}
		and sid=schoolId;
	</select>
</mapper>

ITeacherDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.ITeacherDao">
	<resultMap type="Teacher" id="teacherResultMap">
		<id column="tid" property="tid"/>
		<result column="tname" property="tname"/>
		<result column="age" property="tage"/>
	</resultMap>
	<select id="selectTeacher" resultMap="teacherResultMap">
		select tid,tname,age from teacher
		where tid=#{tid};
	</select>
</mapper>

第八步:

配置日志文件

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] -%m%n

## not to tuijian
##log4j.rootLogger=debug,stdout

##test is namespace ,can not to build

## tuijian  more better
log4j.logger.dao.ISchoolDao=debug,stdout
log4j.logger.dao.ITeacherDao=debug,stdout

附:jdbc.properties 
 

jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql:///test 
jdbc.user=root 
jdbc.password=1314

猜你喜欢

转载自blog.csdn.net/sinat_37064286/article/details/82725175