通过简单案例了解MyBatis和Spring整合
创建IStudentdb实体类
package cb.mybatis.spring.entity; public class IStudentdb { private String sname; //姓名 private String sgender; //性别 @Override public String toString() { return "IStudentdb{" + "sname='" + sname + '\'' + ", sgender='" + sgender + '\'' + ", sage=" + sage + ", saddress='" + saddress + '\'' + '}'+"\n"; } private Integer sage; //年龄 private String saddress; //地址 public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSgender() { return sgender; } public void setSgender(String sgender) { this.sgender = sgender; } public Integer getSage() { return sage; } public void setSage(Integer sage) { this.sage = sage; } public String getSaddress() { return saddress; } public void setSaddress(String saddress) { this.saddress = saddress; } }
一、创建Dao层
package cb.mybatis.spring.dao; import cb.mybatis.spring.entity.IStudentdb; import org.springframework.stereotype.Repository; import java.util.List; public interface IStudentDao { public List<IStudentdb> getAll(); }
二、创建Dao.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"> <!--namespace需要指向接口全路径--> <mapper namespace="cb.mybatis.spring.dao.IStudentDao"> <select id="getAll" resultType="cb.mybatis.spring.entity.IStudentdb"> select * from studentinfo; </select> </mapper>
三、创建Service层
package cb.mybatis.spring.service; import cb.mybatis.spring.entity.IStudentdb; import java.util.List; public interface IStudentService { public List<IStudentdb> getAll(); }
四、创建ServiceImpl层
package cb.mybatis.spring.service.impl; import cb.mybatis.spring.dao.IStudentDao; import cb.mybatis.spring.entity.IStudentdb; import cb.mybatis.spring.service.IStudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.List; public class IStudentServiceImpl implements IStudentService { //注入Dao层对象 IStudentDao studentDao; public IStudentDao getStudentDao() { return studentDao; } public void setStudentDao(IStudentDao studentDao) { this.studentDao = studentDao; } @Override @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) public List<IStudentdb> getAll() { return studentDao.getAll(); } }
五、配置mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--核心配置--> <configuration> <settings> <!--配置二级缓存--> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <package name="cb.mybatis.spring.entity"/> </typeAliases> <!-- 映射文件 --> <!-- <mappers> <mapper resource="cb/mybatis/spring/dao/IStudentDao.xml"/> </mappers>--> </configuration>
六、配置applicationContext-MyBatis-Spring.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.导入database.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties"></property> </bean> <!--2.配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property value="${jdbc.driver}" name="driverClassName"/> <property value="${jdbc.url}" name="url"/> <property value="${jdbc.username}" name="username"/> <property value="${jdbc.password}" name="password"/> </bean> <!--3.配置MyBatis的核心对象sqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--完成对数据源的注入--> <property name="dataSource" ref="dataSource"></property> <!--设置包的别名--> <property name="typeAliasesPackage" value="cb.mybatis.spring.entity"></property> <!--扫描MyBatis所产生的小配置文件--> <property name="mapperLocations" value="classpath*:mapper/*.xml"></property> <!--可指向MyBatis的核心配置文件--> <property name="configLocation" value="mybatis-config.xml"></property> </bean> <!--4.MyBatis的Dao接口的包扫描器(借助Dao接口生成动态代理)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指向Dao接口的所在包--> <property name="basePackage" value="cb.mybatis.spring.dao"></property> </bean> <!--5.将Service实现类对象声明到Spring容器中--> <!--Spring的隐式注入:byType--> <bean id="iStudentService" class="cb.mybatis.spring.service.impl.IStudentServiceImpl" autowire="byType"></bean> <!--6.配置事物--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="getAll" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--AOP织入--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cb.mybatis.spring.service.*.*(..))"></aop:advisor> </aop:config> </beans>
七、创建Test测试类
import cb.mybatis.spring.config.MyBatisAutoConfiguration; import cb.mybatis.spring.service.IStudentService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyBatisTest { public static void main(String[] args) { ApplicationContext ctx=new AnnotationConfigApplicationContext(MyBatisAutoConfiguration.class); IStudentService iStudentService = ctx.getBean(IStudentService.class); System.out.println(iStudentService.getAll()); } }
八、运行结果如下:
方式二:使用注解的方式实现
给完常用注解后稍微改变一下applicationContext.xml文件内容即可
一、配置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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注解方式配置MyBatis和Spring--> <!--1.导入database.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties"></property> </bean> <!--2.配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property value="${jdbc.driver}" name="driverClassName"/> <property value="${jdbc.url}" name="url"/> <property value="${jdbc.username}" name="username"/> <property value="${jdbc.password}" name="password"/> </bean> <!--3.配置MyBatis的核心对象sqlSessionFactoryBean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--完成对数据源的注入--> <property name="dataSource" ref="dataSource"></property> <!--设置包的别名--> <property name="typeAliasesPackage" value="cb.mybatis.spring.entity"></property> <!--扫描MyBatis所产生的小配置文件--> <property name="mapperLocations" value="classpath*:mapper/*.xml"></property> <!--可指向MyBatis的核心配置文件--> <property name="configLocation" value="mybatis-config.xml"></property> </bean> <!--4.MyBatis的Dao接口的包扫描器(借助Dao接口生成动态代理)--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指向Dao接口的所在包--> <property name="basePackage" value="cb.mybatis.spring.dao"></property> </bean> <!--6.配置事物--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启注解配置+事务的注解配置--> <context:component-scan base-package="cb.mybatis.spring"></context:component-scan> <!--开启事务的支持--> <context:annotation-config/> <!--开启事务注解的支持--> <tx:annotation-driven/> </beans>
二、Test测试类
import cb.mybatis.spring.config.MyBatisAutoConfiguration; import cb.mybatis.spring.service.IStudentService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyBatisTest { public static void main(String[] args) { /*ApplicationContext ctx=new AnnotationConfigApplicationContext(MyBatisAutoConfiguration.class);*/ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-MyBatis-Spring2.xml"); IStudentService iStudentService = ctx.getBean(IStudentService.class); System.out.println(iStudentService.getAll()); } }
三、测试结果如下