写点什么

springboot+mybatis+druid 整合笔记,java 程序设计案例教程课后答案

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:3255 字

    阅读完需:约 11 分钟

文章目录



1.创建 springboot 项目

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


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



这里就不详细介绍了,就是选 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 usern


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ame;


@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() {


return userName;


}


public void setUserName(String userName) {


this.userName = userName;


}


public String getPassword() {


return password;


}


public void setPassword(String password) {


this.password = password;


}


}

3.2 mapper.xml 编写

UserMapper.xml 文件。放到 resource 目录下的 mapper 目录下, 正好对应我们上面配置的 mapper 文件位置


<?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.example.testcanal.dao.UserDao">


<sql id="userField">


id,username,password


</sql>


<insert id="addUser" useGeneratedKeys="true" keyProperty="id">


insert into user (username,password) values (#{user.userName},#{user.password})

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
springboot+mybatis+druid整合笔记,java程序设计案例教程课后答案