springboot,Java教程百度网盘

文章目录

      • 1.创建springboot项目

      • 2.整合mybatis与druid

        • 2.1依赖

        • 2.2 配置

      • 3.写一个demo试试

        • 3.1 表结构与实体

        • 3.2 mapper.xml编写

        • 3.3 dao编写

        • 3.4 service与controller编写

        • 3.4 测试

      • 4. druid starter整合

        • 4.1 druid starter

        • 4.2 配置


1.创建springboot项目

创建springboot项目不用多说,一般就用idea自带的spring initializer创建,当然你也可以去start.spring.io spring官网创建好,将项目下载下来导入到idea中(一般不这么干)

我们这里就用idea自带的spring initializer创建,勾勾看看选择哪个starter依赖,我这里就选了个web的starter。

springboot,Java教程百度网盘

这里就不详细介绍了,就是选starter,下一步的事情。

2.整合mybatis与druid

2.1依赖

整合mybatis与druid需要 mybatis的一个starter依赖,druid依赖,mysql驱动依赖,jdbc依赖,这些就够了

我这里把需要的依赖信息列出来


<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

<dependency>

   <groupId>org.mybatis.spring.boot</groupId>

   <artifactId>mybatis-spring-boot-starter</artifactId>

   <version>2.1.4</version>

</dependency>

<dependency>

   <groupId>mysql</groupId>

   <artifactId>mysql-connector-java</artifactId>

   <scope>runtime</scope>

</dependency>

<dependency>

   <groupId>com.alibaba</groupId>

   <artifactId>druid</artifactId>

   <version>1.1.6</version>

</dependency>

<dependency>

   <groupId>ognl</groupId>

   <artifactId>ognl</artifactId>

   <version>3.2.15</version>

   <scope>compile</scope>

   <optional>true</optional>

</dependency>

<dependency>

   <groupId>org.javassist</groupId>

   <artifactId>javassist</artifactId>

   <version>3.27.0-GA</version>

   <scope>compile</scope>

   <optional>true</optional>

</dependency> 

我这里springboot版本是最新版(2.5.2)。

2.2 配置

我这里mybatis的配置就一项,告诉系统我的mapper.xml在哪个位置


mybatis.mapper-locations=classpath:mapper/*.xml 

数据源配置


spring.datasource.type= com.alibaba.druid.pool.DruidDataSource

spring.datasource.url= jdbc:mysql://127.0.0.1:3306/mybatis_test?useUnicode=true&characterEncoding=utf-8

spring.datasource.username= root

spring.datasource.password= root

spring.datasource.driverClassName= com.mysql.jdbc.Driver

spring.datasource.initialSize= 1

spring.datasource.minIdle= 1

spring.datasource.maxActive= 50

spring.datasource.maxWait= 60000 

spring.datasource.timeBetweenEvictionRunsMillis= 60000  

spring.datasource.minEvictableIdleTimeMillis= 300000

spring.datasource.validationQuery= SELECT 1 FROM DUAL

spring.datasource.testWhileIdle=true

spring.datasource.testOnBorrow= false  

spring.datasource.testOnReturn= false  

spring.datasource.poolPreparedStatements= true 

spring.datasource.maxPoolPreparedStatementPerConnectionSize= 20

spring.datasource.filters= stat,wall,log4j

spring.datasource.connectionProperties= druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 

这里整合数据源的话,由于没有用druid的starter,需要自己手动创建下,也就是整个配置类


@Configuration

public class DruidDataSourceConfig {  

   

	/**

	 * druid数据库连接池配置

	 */

    @Value("${spring.datasource.url}")

    private String dbUrl;  

    @Value("${spring.datasource.username}")

    private String username;  

    @Value("${spring.datasource.password}")

    private String password;  

    @Value("${spring.datasource.driverClassName}")

    private String driverClassName;  

    @Value("${spring.datasource.initialSize}")

    private int initialSize;  

    @Value("${spring.datasource.minIdle}")

    private int minIdle;  

    @Value("${spring.datasource.maxActive}")

    private int maxActive;  

    @Value("${spring.datasource.maxWait}")

    private int maxWait;  

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")

    private int timeBetweenEvictionRunsMillis;  

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")

    private int minEvictableIdleTimeMillis;  

    @Value("${spring.datasource.validationQuery}")

    private String validationQuery;  

    @Value("${spring.datasource.testWhileIdle}")

    private boolean testWhileIdle;  

    @Value("${spring.datasource.testOnBorrow}")

    private boolean testOnBorrow;  

    @Value("${spring.datasource.testOnReturn}")

    private boolean testOnReturn;  

    @Value("${spring.datasource.poolPreparedStatements}")

    private boolean poolPreparedStatements;  

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")

    private int maxPoolPreparedStatementPerConnectionSize;  

    @Value("${spring.datasource.filters}")

    private String filters;  

    @Value("{spring.datasource.connectionProperties}")

    private String connectionProperties;  

    

    /**

     * 创建druid数据库连接池bean

     * @return

     */

    @Bean

    @Primary

    public DataSource dataSource(){  

        DruidDataSource datasource = new DruidDataSource();  

        datasource.setUrl(this.dbUrl);  

        datasource.setUsername(username);  

        datasource.setPassword(password);  

        datasource.setDriverClassName(driverClassName);  

        datasource.setInitialSize(initialSize);  

        datasource.setMinIdle(minIdle);  

        datasource.setMaxActive(maxActive);  

        datasource.setMaxWait(maxWait);          

        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  

        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  

        datasource.setValidationQuery(validationQuery);  

        datasource.setTestWhileIdle(testWhileIdle);  

        datasource.setTestOnBorrow(testOnBorrow);  

        datasource.setTestOnReturn(testOnReturn);  

        datasource.setPoolPreparedStatements(poolPreparedStatements);  

        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  

        

        try {  

            datasource.setFilters(filters);  

        } catch (SQLException e) {  

            e.printStackTrace();

        }  

        

        datasource.setConnectionProperties(connectionProperties);  

          

        return datasource;  

    }

    

} 

3.写一个demo试试

这里我们就写个demo来试试效果,随便整个user表

3.1 表结构与实体


CREATE TABLE `user` (

  `id` int(10) NOT NULL AUTO_INCREMENT,

  `username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

  `password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

public class UserEntity implements Serializable {

private Integer id;

private String userName;

private String password;

public Integer getId() {

    return id;

}

public void setId(Integer id) {

    this.id = id;

}

public String getUserName() {
上一篇:59、二叉搜索树的第K的结点


下一篇:剑指offer-二叉搜索树的第K个结点