1. 程式人生 > >spring-boot + mybatis 搭建

spring-boot + mybatis 搭建

spa thymeleaf mage oca 配置文件 otto json nac 包引入

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 搭建