springboot使用入门

2021-01-20 05:14

阅读:658

标签:mapping   dbcp   nsa   nal   min   connect   and   start   pre   

 

1.创建maven工程,引入依赖

    
    org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE

2.引入SpringBoot提供的自动配置依赖

    org.springframework.boot
            spring-boot-starter-web
        

3.添加全局引导类,并使用@SpringBootApplication注解

@SpringBootApplication  // 相当于 EnableAutoConfiguration + ComponentScan + SpringbootConfiguration
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

4.引入druid依赖

com.github.drtrang
    druid-spring-boot2-starter
    1.1.10

5.添加jdbc.properties配置文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboottest
jdbc.username=root
jdbc.password=root

6使用 @Autowired注入datasource

@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {

    @Autowired
    private JdbcProperties jdbcProperties;

    @Bean
    public DataSource dataSource() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(jdbcProperties.getDriverClassName());
        dataSource.setUrl(jdbcProperties.getUrl());
        dataSource.setUsername(jdbcProperties.getUsername());
        dataSource.setPassword(jdbcProperties.getPassword());
        return dataSource;
    }
}

7.添加连接池 

只需要在pom中找到springboot提供的启动器


org.springframework.boot
    spring-boot-starter-jdbc
mysql
    mysql-connector-java

8.指定连接池参数 

在resource目录下创建:application.properties配置文件

# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/springboottest
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver

9.使用druid连接池


com.alibaba
    druid-spring-boot-starter
    1.1.10

在application.properties中添加一些连接属性

#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true

10.整合mybatis


org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2

10.1配置 mybatis别名扫描

# mybatis 别名扫描
mybatis.type-aliases-package=cn.test.pojo

10.2编写dao类

@Mapper
public interface UserMapper {
}

11.通用mapper

引入启动类


tk.mybatis
    mapper-spring-boot-starter
    2.0.2

12.编写server类  注意添加事务@Transactional

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User queryUserById(Long id){
        return this.userMapper.selectByPrimaryKey(id);
    }
    @Transactional
    public void  deleteUserById(Long id){
        this.userMapper.deleteByPrimaryKey(id);
    }
}

13编写controller类

@RestController
@RequestMapping("User")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    @ResponseBody
    public User queryUserById(@PathVariable("id")Long id){
        return this.userService.queryUserById(id);
    }
}

 

application.properties配置文件

server.port=8888
spring.mvc.static-path-pattern=/**
# 这个配置是为了能访问resource/static/目录下的静态资源

# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true

# mybatis 别名扫描
mybatis.type-aliases-package=com.test.pojo

pom.xml配置文件


         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0cn.test.springboot2
    itest
    1.0-SNAPSHOTorg.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASEorg.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
            spring-boot-starter-jdbc
        mysql
            mysql-connector-java
        com.alibaba
            druid-spring-boot-starter
            1.1.10org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2tk.mybatis
            mapper-spring-boot-starter
            2.0.2

结束!

 

springboot使用入门

标签:mapping   dbcp   nsa   nal   min   connect   and   start   pre   

原文地址:https://www.cnblogs.com/zxh06820/p/12903122.html


评论


亲,登录后才可以留言!