天天看點

springboot內建druid+jdbc+mybatis建立一個Springboot項目在項目中的pom.xml檔案中引入以下依賴再将以下配置,放在yml配置檔案中編寫Druid配置類:代碼如下內建jdbc+mybatis并通過接口通路資料庫最後需要在啟動類加上@MapperScan注解,用于掃描mapper類最後啟動,通過接口通路,便有如下效果:

建立一個Springboot項目

在項目中的pom.xml檔案中引入以下依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
           

再将以下配置,放在yml配置檔案中

spring:
  datasource:
    #1.JDBC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.80.111:3306/jdbc?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    username: root
    password: 123456
    druid:
      #2.連接配接池配置
      #初始化連接配接池的連接配接數量 大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      #配置擷取連接配接等待逾時的時間
      max-wait: 60000
      #配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接配接,機關是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一個連接配接在池中最小生存的時間,機關是毫秒
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 是否緩存preparedStatement,也就是PSCache  官方建議MySQL下建議關閉   個人建議如果想用SQL防火牆 建議打開
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用于防火牆
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      #3.基礎監控配置
      web-stat-filter:
        enabled: true
        url-pattern: /*
        #設定不統計哪些URL
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        #設定監控頁面的登入名和密碼
        login-username: admin
        login-password: admin
        allow: 127.0.0.1
        #deny: 192.168.1.100

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.spring.jdbc.entity

           

編寫Druid配置類:代碼如下

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> initParams=new HashMap<String, String>();
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        initParams.put("allow","");//預設就是允許所有通路
        initParams.put("deny","");//禁止通路的ip
        bean.setInitParameters(initParams);
        return bean;
    }

    //配置一個web監控的Filter
    @Bean
    public FilterRegistrationBean wenStatFilter(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String,String> initParams=new HashMap<String, String>();

        //不攔截的請求
        initParams.put("exclusions","*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);
        //需要攔截的請求
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }

}
           

該類編寫好之後,可設定啟動端口之類的配置,不設定預設是8080

啟動xxxApplication.java啟動類。

成功啟動後在網頁上通路:http://localhost:8080/druid/index.html

然後會跳轉到下面的圖的網頁

springboot內建druid+jdbc+mybatis建立一個Springboot項目在項目中的pom.xml檔案中引入以下依賴再将以下配置,放在yml配置檔案中編寫Druid配置類:代碼如下內建jdbc+mybatis并通過接口通路資料庫最後需要在啟動類加上@MapperScan注解,用于掃描mapper類最後啟動,通過接口通路,便有如下效果:

使用者名和密碼均是上面的配置類設定的,輸入即可

內建jdbc+mybatis并通過接口通路資料庫

根據資料庫建立實體類(這裡是作展示,如果是正常開發,可以用逆向成功工具)

public class Test {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
           

編寫Mapper類

public interface TestMapper {
    Test selectEmpById(@Param("id")Integer id);
}
           

編寫業務層service

public interface TestService {
    Test selectEmpById(Integer id);
}
           

編寫Impl實作類

@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private TestMapper testMapper;

    @Override
    public Test selectEmpById(Integer id) {
        return testMapper.selectEmpById(id);
    }
}
           

編寫接口類

@RestController
public class TestCtrl {
    @Autowired
    private TestService testService;

    @RequestMapping("/selectEmpById/{id}")
    @ResponseBody
    public Test selectEmpById(@PathVariable("id")Integer id){
        return testService.selectEmpById(id);
    }
}
           

編寫映射mapper.xml,要與上面的yml檔案中mapper-locations屬性值的路徑保持一緻,

要保證能夠定位到這個*mapper.xml檔案

與上面舉例的程式對應的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.spring.jdbc.mapper.TestMapper">
      <select id="selectEmpById" resultType="com.spring.jdbc.entity.Test">
        select * from test where id=#{id}
      </select>
</mapper>
           

最後需要在啟動類加上@MapperScan注解,用于掃描mapper類

@MapperScan(value = "com.spring.jdbc.mapper")//value要求填寫mapper所在路徑
@SpringBootApplication
public class JdbcApplication {

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

}
           

最後啟動,通過接口通路,便有如下效果:

springboot內建druid+jdbc+mybatis建立一個Springboot項目在項目中的pom.xml檔案中引入以下依賴再将以下配置,放在yml配置檔案中編寫Druid配置類:代碼如下內建jdbc+mybatis并通過接口通路資料庫最後需要在啟動類加上@MapperScan注解,用于掃描mapper類最後啟動,通過接口通路,便有如下效果:

對應的資料庫表結構和資料如下:

springboot內建druid+jdbc+mybatis建立一個Springboot項目在項目中的pom.xml檔案中引入以下依賴再将以下配置,放在yml配置檔案中編寫Druid配置類:代碼如下內建jdbc+mybatis并通過接口通路資料庫最後需要在啟動類加上@MapperScan注解,用于掃描mapper類最後啟動,通過接口通路,便有如下效果:

最後補充:最後的一個項目結構如下:

springboot內建druid+jdbc+mybatis建立一個Springboot項目在項目中的pom.xml檔案中引入以下依賴再将以下配置,放在yml配置檔案中編寫Druid配置類:代碼如下內建jdbc+mybatis并通過接口通路資料庫最後需要在啟動類加上@MapperScan注解,用于掃描mapper類最後啟動,通過接口通路,便有如下效果:

文章格式不是太好,内容如果對于個人比較粗糙還請非喜勿噴,謝謝大家支援,大家一起進步!