spring-boot + mybatis 搭建

Posted youer66

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot + mybatis 搭建相关的知识,希望对你有一定的参考价值。

 

spring boot + mybatis 环境搭建
    • maven 项目的jar包引入

      <dependencies>
      
         <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.11</version>
           <scope>test</scope>
         </dependency>
      
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
      
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
      
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
      
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
        </dependency>
      
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.2.8</version>
        </dependency>
      
        <dependency>
          <groupId>org.apache.tomcat</groupId>
          <artifactId>tomcat-jdbc</artifactId>
        </dependency>
      
        <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>ojdbc6</artifactId>
          <version>11.2.0.1.0</version>
        </dependency>
      
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.1.43</version>
        </dependency>                                                                    
      </dependencies>
    • 构造maven项目结构

  • 技术分享图片

  •   Java
          |_contrille 控制层
          |_mapper 存放DAO 运用的是mybatis所以存放的都是接口
          |_model 实体类
          |_services 业务层
          |_Application.java  是 springBoot 启动入口
      resources
          |_mybatis 存放mapper.xml 配置文件 ,主要的是读取这个文件过后创  建DAO的实现类
          |_templates 存放页面
          |_Application.properties 配置文件 数据库连接参数
  • Application.java 入口类

      @EnableAutoConfiguration
    
      /*读取配置文件的路劲,在启动的时候自动加载该路径下的类到Spring容器中*/
      @SpringBootApplication
    
      @ComponentScan
      /*扫描 具有 Controller,services,repository注解的类到容器中*/
    
      @MapperScan("com.youer.shop.inventory.mapper")
      /*直接指定要扫描的mapper,这样在mapper接口上就不用一个个加@Mapper*/
    
      public class Application {
          @Bean
          @ConfigurationProperties(prefix="spring.datasource")
          /*加载配置信息,Application.porperties*/
          public DataSource dataSource() {
              return new org.apache.tomcat.jdbc.pool.DataSource();
          }
    
          @Bean
          /*数据连接*/
          public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
              SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
              sqlSessionFactoryBean.setDataSource(dataSource());
              PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
       sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
              return sqlSessionFactoryBean.getObject();
          }
    
          @Bean
          /*事务*/
          public PlatformTransactionManager transactionManager() {
              return new DataSourceTransactionManager(dataSource());
          }
    
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }    
  • mapper.xml 的格式

        <?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">
          <mapper namespace="com.youer.shop.inventory.mapper.UserMapper">
    
          </mapper>
  • 后续奖和redis整合 搭建一个分布式换成架构
      redis依赖
      <dependency>
           <groupId>redis.clients</groupId>
           <artifactId>jedis</artifactId>
      </dependency>

以上是关于spring-boot + mybatis 搭建的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse对spring-boot,spring-boot-mybatis的搭建

IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis反向工程;mybatis+springboot逆向工程

搭建MyBatis开发环境

博客路线

spring+mybatis+mina+logback框架搭建

spring-boot+mybatis整合简写