SpringBoot学习2之注解简单配置Springboot+MyBatis

1.创建springboot工程,pom文件主要包如下:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

2.利用application.proprities创建数据源配置:

# dataSource configuration
spring.server.port = 8080
spring.datasource.url=jdbc:postgresql://10.143.1.30:5491/temp
spring.datasource.username=temp
spring.datasource.password=temp
spring.datasource.driverClassName=org.postgresql.Driver

#mybatis configuration:讲数据库字段是下划线,改变为程序中使用驼峰
mybatis.configuration.map-underscore-to-camel-case=true

3.在SpringBootApplication类文件添加自动扫描注解:

  @MapperScan //作用:是将指定包下的接口自动生成实现类。

  通常配置SpringBootApplication类中

  @SpringBootApplication
  @MapperScan("com.sso.mapper")
  public class SsoApplication {

  public static void main(String[] args) {
      SpringApplication.run(SsoApplication.class, args);
    }

  }

       @Mapper //作用:是将指定的接口自动生成实现类。相当于是一个mapper.xml 文件

4.创建mapper接口主要内容:

public interface UserMapper {
@Select("select * from user where user_id = #{id}")
  public Security getDays (String id);
}

  

5.其他controller,model示例暂时省略。

原文地址:https://www.cnblogs.com/michaelShao/p/14517391.html