Spring整合Mybatis
Posted 慕容子月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合Mybatis相关的知识,希望对你有一定的参考价值。
一、基于XML
实体类
package cn.spring.entity; import java.io.Serializable; public class Bank implements Serializable { private String card_code; //卡号 private String user_name; //所属人姓名 private double card_balance; //卡内余额 public String getCard_code() { return card_code; } public void setCard_code(String card_code) { this.card_code = card_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public double getCard_balance() { return card_balance; } public void setCard_balance(double card_balance) { this.card_balance = card_balance; } }
Dao层接口和XML配置文件
Dao层接口 @Repository public interface IBankDao { public List<Bank> getAllBank(); } 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="cn.spring.dao.IBankDao"> <select id="getAllBank" resultType="Bank"> select * from bank </select> </mapper>
Service层接口和实现类
Service层接口 public interface IBankService { public List<Bank> getAllBank(); } 实现类public class IBankServiceImpl implements IBankService { @Resource private IBankDao iBankDao; @Override public List<Bank> getAllBank() { return iBankDao.getAllBank(); } public IBankDao getiBankDao() { return iBankDao; } public void setiBankDao(IBankDao iBankDao) { this.iBankDao = iBankDao; } }
applicationContext.xml大配置文件
<!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层,省略代码--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.dao"/> </bean> <!--注入Dao层--> <bean id="iBankDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.spring.dao.IBankDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean> <!--Service层--> <bean id="iBankService" class="cn.spring.service.impl.IBankServiceImpl"> <property name="iBankDao" ref="iBankDao"/> </bean>
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> <!--配置别名--> <typeAliases> <package name="cn.spring.entity"/> </typeAliases> <!--加载小配置文件--> <mappers> <package name="cn.spring.dao"/> </mappers> </configuration>
测试类
public class SpringMybaitsTest { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IBankService iBankService = (IBankService)ctx.getBean("iBankService"); List<Bank> allBank = iBankService.getAllBank(); for(Bank bank:allBank){ System.out.println(bank.getUser_name()); } } }
二、基于注解配置
修改上面案例,使用注解注入Dao层
/** * Dao层接口 */ @Repository public interface IBankDao { public List<Bank> getAllBank(); }
使用注解注入Service层,并且使用注解调用Dao层对象
@Service("iBankService") public class IBankServiceImpl implements IBankService { @Resource private IBankDao iBankDao; @Override public List<Bank> getAllBank() { return iBankDao.getAllBank(); } }
修改applicationContext.xml大配置文件,开启注解扫描
<!--扫描注解--> <context:component-scan base-package="cn.spring"/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层,省略代码--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.dao"/> </bean>
其他的不变
以上是关于Spring整合Mybatis的主要内容,如果未能解决你的问题,请参考以下文章
请教mybatis+spring+atomikos的整合问题
基于Spring+SpringMVC+MyBatis开发书评网Spring和MyBatis的整合