Springboot Mybatis Sqlserver初配

Controller、Entity、Service、Mapper编写和spring一致

其他配置注意:

1、启动类需添加两个注解:

  @SpringBootApplication
  @MapperScan("com.xx.xx.Mapper") //扫描Mapper接口

2、Mapper接口类,需添加注解:@Mapper

3、Mapper.xml配置:按spring方式配置即可

  ①位置放置要和下面的mybatis-locations保持一致哦,不然会读取不到(resources包内);

  ②如果不配置Mapper.xml,也可以使用注释方式设置语句,在mapper接口内对应的位置使用注释,如@Select("Select * from xxx")

4、application.yml配置

server: 
 port: xxxx   #当多个项目时,可重设端口号;可不重新设置,则为默认你本机端口号
 
spring:
  datasource:
    url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=xxx
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    username: xx
    password: xxxxx
       
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.xx.xx.Entity

5、pom.xml配置:

--首先记得加载sqlserver依赖配置

 <dependencies>

  <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>

 </dependencies>

--第二,静态资源包位置也要记得配置正确

<resources>
      <resource>
          <!--   描述存放资源的目录,该路径相对POM路径-->
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
              <include>**/*.yml</include>
          </includes>
          <filtering>false</filtering>
      </resource>
      <resource>
          <directory>src/main/resources</directory>
          <includes>
              <include>**/*.xml</include>
              <include>**/*.yml</include>
          </includes>
          <filtering>false</filtering>
      </resource>
  </resources>

--第三,其他按需加载依赖

 

posted on 2020-03-31 10:06  22xth  阅读(710)  评论(0编辑  收藏  举报