• 【mybatis】02-spring集成


    POM配置

    <dependency>
        <groupId>org.mybatis</groupId>
         <artifactId>mybatis</artifactId>
         <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
     </dependency>

    web.xml中指定的spring配置

    <context:component-scan base-package="com.smart.daoMybatis"/>
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close"
            p:driverClassName="${jdbc.driverClassName}"
            p:url="${jdbc.url}"
            p:username="${jdbc.username}"
            p:password="${jdbc.password}" />
    
        <!-- Mybatis config -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 指定mapper.xml配置文件 -->
            <property name="mapperLocations" value="classpath:mybatis/BoardMapper.xml" />
        </bean>

    java实现:

    @Repository
    public class BoardMybatisDao extends BaseMybatisDao<User> {
    
        @Autowired
        SqlSessionFactory sqlSessionFactory;
    
        public List<Board> getAllBoards()
        {
            SqlSession sqlSession = sqlSessionFactory.openSession();
            List<Board> allBoards = sqlSession.getMapper(BoardMapper.class).getAllBoards();
    
            return allBoards;
        }
    }

    开发经验:IDEA安装Free mybatis plugin插件后 

    1、自动对Mapper.java和Mapper.xml进行跳转关联

     

     2、只在mapper.java定义函数,通过alt+enter生成xml的mapper语句

    进一步选择SQL语句

  • 相关阅读:
    《刀塔传奇》
    SSH端口转发
    objective-c与c++的差异
    mysql 远程连接速度慢的解决方案
    maven
    automapper 自动映射 集成asp.net Core2.1
    swagger 集成asp.net Core2.1
    asp.net Core 跨域配置
    处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    SharedPreferences
  • 原文地址:https://www.cnblogs.com/clarino/p/14787073.html
Copyright © 2020-2023  润新知