總結MyBatis+Spring的整合
阿新 • • 發佈:2018-11-02
第一步:
首先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