• springboot使用入门2


     

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

        <!-- 所有的springboot的工程都以spring父工程为父工程 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.6.RELEASE</version>
        </parent>

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

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

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

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

    4.引入druid依赖

    <dependency>
        <groupId>com.github.drtrang</groupId>
        <artifactId>druid-spring-boot2-starter</artifactId>
        <version>1.1.10</version>
    </dependency>

    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提供的启动器

    <!--jdbc的启动器,默认使用HikariCP连接池-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    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连接池

    <!-- Druid连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>

    在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

    <!--mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    10.1配置 mybatis别名扫描

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

    10.2编写dao类

    @Mapper
    public interface UserMapper {
    }

    11.通用mapper

    引入启动类

    <!-- 通用mapper -->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.2</version>
    </dependency>

    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配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.test.springboot2</groupId>
        <artifactId>itest</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.6.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!-- Druid连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.10</version>
            </dependency>
            <!--mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!-- 通用mapper -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>2.0.2</version>
            </dependency>
        </dependencies>
    
    </project>

    结束!

  • 相关阅读:
    word设置的密码忘了怎么办?
    Navicat Report Viewer 设置 HTTP 的方法
    如何处理Navicat Report Viewer 报表
    excel密码忘记了怎么办
    Beyond Compare文本比较搜索功能详解
    Popular Cows POJ
    Problem B. Harvest of Apples HDU
    网络流模型整理
    The Shortest Statement CodeForces
    Vasya and Multisets CodeForces
  • 原文地址:https://www.cnblogs.com/zxh06820/p/12903122.html
Copyright © 2020-2023  润新知