• 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());
        }
    }
  • 相关阅读:
    xcodebuild changed some of the values
    ar和nm命令的使用(转载)
    POJ 3678 Katu Puzzle (2SAT)
    ZOJ 3664 Split the Rectangle 第37届ACM/ICPC长春赛区现场赛 J 题(模拟建树,暴力 求LCA)
    HDU 4115 Eliminate the Conflict (2SAT)
    ZOJ 3665 Yukari's Birthday 第37届ACM/ICPC长春赛区现场赛K题 (水题,枚举,二分)
    ZOJ 3662 Math Magic 第37届ACM/ICPC长春赛区H题(DP)
    【原创】2012ACM长春赛区现场赛总结
    ZOJ 3656 Bit Magic 第37届ACM/ICPC长春赛区现场赛B题 (2SAT)
    ZOJ 3657 The Little Girl who Picks Mushrooms 第37届ACM/ICPC长春赛区现场赛C题(水题)
  • 原文地址:https://www.cnblogs.com/szhhhh/p/11798060.html
Copyright © 2020-2023  润新知