一、xml配置版
1.导入依赖
<!--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--MyBatis的核心jar文件--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency>
2.创建实体类
package cn.spring.entity;
import java.io.Serializable;
public class Bank implements Serializable {
private String card_id;
private String user_name;
private Integer card_money;
public String getCard_id() {
return card_id;
}
public void setCard_id(String card_id) {
this.card_id = card_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getCard_money() {
return card_money;
}
public void setCard_money(Integer card_money) {
this.card_money = card_money;
}
}
3.创建Dao层接口及其映射文件
package cn.spring.dao;
import cn.spring.entity.Bank;
import org.springframework.stereotype.Repository;
import java.util.List;
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="Bank">
select * from bank
</select>
</mapper>
4.Service层接口及其实现类
package cn.spring.service;
import cn.spring.entity.Bank;
import java.util.List;
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;
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;
}
}
5.配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- xml文件的头文件,起到对文件的约束作用(例如:必须存在哪些节点) --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.wdksoft.entity"/> </typeAliases> <mappers><!--管理我们配置的映射文件--> <package name="com.wdksoft.dao"/> </mappers> </configuration>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bank?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=12345
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载配置文件-->
<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>
</beans>
二、注解版
Spring配置文件:
<!--包扫描器--> <context:component-scan base-package="com.wdksoft"/> <!--加载配置文件--> <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="com.wdksoft.dao"/> </bean>
其他加入相应注解即可