一,基于XML
实体:
public class Bank implements Serializable { private String card_code; //卡号 private String user_name; //所属人姓名 private double card_balance; //卡内余额
dao层:
public interface IBankDao { public List<Bank> getAllBank(); }
<?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="cn.spring.entity.Bank"> select * from bank </select> </mapper>
service层:
public interface IBankService { public List<Bank> getAllBank(); }
package cn.spring.service.impl; import cn.spring.dao.IBankDao; import cn.spring.entity.Bank; import cn.spring.service.IBankService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service("iBankService") public class IBankServiceImpl implements IBankService { @Resource private IBankDao iBankDao; @Override public List<Bank> getAllBank() { return iBankDao.getAllBank(); } }
配置文件:
<!--配置数据源--> <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"/>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bank?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123
<?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>
测试:
package cn.spring; import cn.spring.entity.Bank; import cn.spring.service.IBankService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class SpringMybatisTest { 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()); } } }
二,基于注解
配置文件:
<!--扫描注解--> <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>
dao层:
public interface IBankDao { public List<Bank> getAllBank(); }
service层:
@Service("iBankService") public class IBankServiceImpl implements IBankService { @Resource private IBankDao iBankDao; @Override public List<Bank> getAllBank() { return iBankDao.getAllBank(); } }
<?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="cn.spring.entity.Bank">
select * from bank
</select>
</mapper>