• MyBatis与Spring整合


    一,基于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>
  • 相关阅读:
    WEB服务器3--IIS7.0安装和配置
    组件与组件之间的通信以及vue2.0中的变化、示例
    Vue2.0组件之间通信
    weex学习资源集合贴
    主题 : 好了,今天周六了,既然没人了,那么开讲多线程编程
    使用vue2.0 vue-router vuex 模拟ios7操作
    iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值
    iOS开发之使用Runtime给Model类赋值
    AFNetworking到底做了什么?
    webview300毫秒点击问题
  • 原文地址:https://www.cnblogs.com/cw172/p/11809483.html
Copyright © 2020-2023  润新知