MyBatis和Spring整合案例

1.所需要导入的jar文件

!--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.MyBatisSpring整合(xml)

2.1创建Dao层接口

public interface AccountDao {
    //查询所有账户
    public List<Account> getAllAccount();
}

2.2创建Dao层接口小配置文件

<!--namespace需要指向接口全路径-->
<mapper namespace="cn.spring.dao.AccountDao">

    <select id="getAllAccount" resultType="cn.spring.entity.Account">
        select * from accounts
    </select>
</mapper>

2.3创建Service层接口

public interface AccountService {
    //查询所有账户
    public List<Account> getAllAccount();
}

2.4创建Service层接口实现类

 

public class AccountServiceImpl implements AccountService {

    
    AccountDao accountDao;

    @Override
    public List<Account> getAllAccount() {

        return accountDao.getAllAccount();
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }



}

2.5 配置applicationContext.xml文件

<context:component-scan base-package="cn.spring"/>
<!-- 导入jdbc.properties文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据源  spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>


<!--配置mybatis的核心对象sqlsessionfactorybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="cn.spring.entity"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--MyBatis的Dao接口的包扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.spring.dao"></property>
</bean>


<!--将Service实现类对象声明到Spring容器中
隐式注入-->
<bean id="accountService" class="cn.spring.service.impl.AccountServiceImpl" autowire="byType"></bean>

 

2.6编写测试类

@Test
 public void getAll(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService = (AccountService)context.getBean("accountService");
    List<Account> allAccount = accountService.getAllAccount();
    for (Account account:allAccount){
        System.out.println(account.getAccountid());
    }
}

3.MyBatisSpring整合(注解版)

3.1创建Dao层接口

@Repository
public interface AccountDao {
    //查询所有账户
    public List<Account> getAllAccount();
}

3.2同上2.2

3.3同上2.3

3.4创建Service层接口实现类

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;

    @Override
    public List<Account> getAllAccount() {

        return accountDao.getAllAccount();
    }

    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

}

3.5编写applicationContext.xml文件

<context:component-scan base-package="cn.spring"/>
<!-- 导入jdbc.properties文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置数据源  spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>


<!--配置mybatis的核心对象sqlsessionfactorybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="cn.spring.entity"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!--MyBatis的Dao接口的包扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.spring.dao"></property>
</bean>

3.6编写测试类

@Test
 public void getAll(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService = (AccountService)context.getBean("accountService");
    List<Account> allAccount = accountService.getAllAccount();
    for (Account account:allAccount){
        System.out.println(account.getAccountid());
    }
}

猜你喜欢

转载自www.cnblogs.com/1314Justin/p/11798060.html
今日推荐