本文旨在用最通俗的语言讲述最枯燥的基本知识
创新互联主营云县网站建设的网络公司,主营网站建设方案,成都app开发,云县h5微信小程序搭建,云县网站营销推广欢迎云县等地区企业咨询
最近身边的程序员掀起了学习springboot的热潮,说什么学会了springboot在大街上就可以横着走、什么有了springboot妈妈再也不担心我的编程了、什么BAT都喜欢的框架…听得作者那个心痒痒的,于是找了个时间,下载了个idea来玩一波springboot,对了…用springboot最好用idea,如果你还在用eclipse,删了吧。
在这里解释一下为什么是springboot+mybatis+druid,是因为作者认为但凡任何一个有灵魂的项目,都少不了数据库,作者不喜欢用JPA那种混SQL的语法,因此选了mybatis,而Druid是阿里系(真香~)的一种数据库连接池框架,在上一个项目作者用的屡试不爽,因此打算继续用,为啥屡试不爽?看文末吧。
文章提纲:
创建springboot工程
配置pom.xml
配置数据源
设置mybatis
hello world
设置Druid监控配置
1. 创建springboot工程
只要你有idea,创建一个springboot工程,就跟捏死一个蚂蚁一样简单,因为idea里深度集成了对springboot项目的支持,你直接不停的next到最后,它就会帮你创建出一个springboot工程。
- 首先打开idea->Create New project->选择项目类型:这里选择spring initializr,然后next。  
- 创建项目,填写项目名称,注意:不要能驼峰写法,可以用中横线,填写完毕后继续next。 
- 选择依赖,所谓依赖就是你在设计这个项目的框架时,分析这个项目需要用到哪些jar或者组件,比如缓存要用到redis,数据库要用到MySQL等…这里因为演示项目,就不选择太多其它乱七八糟的依赖了。 
- 选好依赖之后,又是一路狂点next,直到最后finish一下,一个springboot项目就创建好了。 
2. 配置pom.xml
想想,我们需要哪些jar?
数据库要用到mybatis,数据库连接池要用到Druid、MySQL桥接器要用到mysql-connector,因此要maven仓库(点我去仓库)中找到搜索这些pom加进去。注意,mybatis要用mybatis-spring-boot-starter。
 1
       
 2
        
<
dependency
>
 3
            
<
groupId
>
mysql
groupId
>
 4
            
<
artifactId
>
mysql-connector-java
artifactId
>
 5
            
<
version
>
5.1.6
version
>
 6
        
dependency
>
 7
        
 8
        
<
dependency
>
 9
            
<
groupId
>
com.alibaba
groupId
>
10
            
<
artifactId
>
druid
artifactId
>
11
            
<
version
>
1.1.10
version
>
12
        
dependency
>
13
        
14
        
<
dependency
>
15
            
<
groupId
>
org.mybatis.spring.boot
groupId
>
16
            
<
artifactId
>
mybatis-spring-boot-starter
artifactId
>
17
            
<
version
>
1.3.2
version
>
18
        
dependency
>
把上面这些pom放到pom.xml的dependencies中
细心的老铁会发现,MySQL的version里的内容是红色的,这是什么原因呢?
这是因为我们引入pom时,这些版本的jar在本地maven仓库还没有,而Druid的pom里的version没有显示红色,是因为之前的项目用到了这个版本的Druid,已经被下载到本地Maven仓库里了。
因此我们需要把本地没有的jar下载到本地仓库,右键pom.xml弹出菜单,选择Maven,弹出菜单选择reimport
Reimport过程中再idea底部会有进度条显示,等进度条消失,在观察pom.xml,红色已经消失,说明依赖已经装备完成。
配置数据源
接下来就是要多springboot项目做一个全局配置,默认会在src->main->resource目录下生产空白文件application.properties,作者喜欢用yml因此直接改名成yml即可。
首先是数据源的配置,下面是一份数据源的配置,每个参数的解释都写了注释,因此读者可以直接复制一下内容进去,只需要改一下url、username、password
 1
spring:
 2
  #profiles: dev
 3
  messages:
 4
    basename: i18n/Messages,i18n/Pages
 5
  datasource:
 6
    type: com.alibaba.druid.pool.DruidDataSource    # 配置当前要使用的数据源的操作类型
 7
    driver-class-name: org.gjt.mm.mysql.Driver        # 配置MySQL的驱动程序类
 8
    url: jdbc:mysql://localhost:3306/wkt_stat           # 数据库连接地址
 9
    username: root                                  # 数据库用户名
10
    password: root                            # 数据库连接密码
11
    dbcp2:                                          # 进行数据库连接池的配置
12
      min-idle: 5                                   # 数据库连接池的最小维持连接数
13
      initial-size: 5                               # 初始化提供的连接数
14
      max-total: 5                                  # 最大的连接数
15
      max-wait-millis: 200                          # 等待连接获取的最大超时时间
4. 设置mybatis
继续在application.yml中设置mybatis,mybatis的配置也简单,
主要是为了设置mybatis的配置文件已经mapper文件所在。
- 首先在resource目录下创建一个mybatis-config.xml文件,文件内容为: 
1
2
3
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
<
configuration
>
6
    
<
mappers
>
7
    
mappers
>
8
configuration
>
- 在main包中的根目录下创建一个存放mapper实体的资源文件,在resource文件下创建一个文件夹mapper用来存放mapper的xml文件。 
- 配置好资源文件路径之后,就可以在application.yml中加入mybatis的配置了,如下是一个mybatis的配置内容: 
1
#mybatis的mapper配置文件
2
mybatis:
3
  config-location: classpath:mybatis-config.xml  # mybatis配置文件所在路径
4
  #mapper-locations: classpath:mapper/*.xml   # 所有的mapper映射文件
5
  type-aliases-package: com.becl.dao.mapper # 定义所有操作类的别名所在包
6
debug: true
最终application.yml的内容如下图:
- 此时需要有一个数据库表来做测试,我们在数据库创建一个表,并且插入一条数据: 
1
CREATE TABLE Memeber (
2
`id`  int(11) NOT NULL AUTO_INCREMENT ,
3
`name`  varchar(255) NULL ,
4
PRIMARY KEY (`id`)
5
);
6
7
INSERT INTO memeber VALUES(1,"jas")
- 在mapper包中创建Memeber的mapper接口: 
1
public
 
interface
 
MemeberMapper
 
{
2
    
/**
3
     * 根据ID获取记录
4
     * 
@param
 id
5
     * 
@return
6
     */
7
    
public
 Map 
findObjectById
(Integer id)
;
8
}
- 在resource中的mapper文件夹创建memberMapper.xml,并且在mapper中增加一个findObjectById的SQL查询语句。 
 1
 2
 3
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5
 6
<
mapper
 
namespace
=
"com.example.mybatisanddruid.mapper.MemeberMapper"
>
 7
#根据ID查询记录
 8
<
select
 
id
=
"findObjectById"
 
parameterType
=
"Integer"
 
resultType
=
"Map"
>
 9
        select * from memeber where id = #{value}
10
    
select
>
11
mapper
>
5. hello world
走到这一步,基本上已经是大功告成了,我们来写一个测试类试试,在根目录创建一个controller的包,在包中创建一个Java类,如下:
 1
@Controller
 2
@RequestMapping
(
"/test"
)
 3
public
 
class
 
TestController
 
{
 4
    
@Resource
 5
    
private
 MemeberMapper memeberMapper=
null
;
 6
 7
    
@RequestMapping
(
"/one"
)
 8
    
@ResponseBody
 9
    
public
 Map 
testdb
()
{
10
        
return
  memeberMapper.findObjectById(
1
);
11
    }
12
}
创建完之后,我们运行项目,找到启动类MybatisAndDruidApplication右键run,发现报错,提示没有扫描到mapper包,为什么呢?那是mapper需要手动在启动类中加入:
1
@MapperScan
(
"com.example.mybatisanddruid.mapper"
)
这样启动类就变成:
1
@SpringBootApplication
2
@MapperScan
(
"com.example.mybatisanddruid.mapper"
)
3
public
 
class
 
MybatisAndDruidApplication
 
{
4
5
    
public
 
static
 
void
 
main
(String[] args)
 
{
6
        SpringApplication.run(MybatisAndDruidApplication.class, args);
7
    }
8
}
再次运行,没有报错,在浏览器输入:http://localhost:8888/test/one
输出了ID为1的记录:
1
{"name":"jas","id":1}
由此可见,springboot-mybatis已经搭建成功,此时有人会问,那Druid呢?
设置Druid监控配置
druid的使用需要做一些配置,现在我们来在根目录下创建一个包config,在config包中间创建一个叫做DruidConfig.java,并且在里写入下面的内容:
 1
@Configuration
 2
public
 
class
 
DruidConfig
 
{
 3
    
@Bean
 4
    
public
 ServletRegistrationBean 
druidServlet
()
 
{ 
// 主要实现WEB监控的配置处理
 5
        ServletRegistrationBean servletRegistrationBean = 
new
 ServletRegistrationBean(
new
 StatViewServlet(), 
"/druid/*"
); 
// 进行druid监控的配置处理操作
 6
        servletRegistrationBean.addInitParameter(
"allow"
,
 7
                
"127.0.0.1,192.168.1.159"
); 
// 白名单
 8
        servletRegistrationBean.addInitParameter(
"deny"
, 
"192.168.1.200"
); 
// 黑名单
 9
        servletRegistrationBean.addInitParameter(
"loginUsername"
, 
"stat"
); 
// 用户名
10
        servletRegistrationBean.addInitParameter(
"loginPassword"
, 
"Wkt_sTat_1031"
); 
// 密码
11
        servletRegistrationBean.addInitParameter(
"resetEnable"
, 
"false"
); 
// 是否可以重置数据源
12
        
return
 servletRegistrationBean ;
13
    }
14
    
@Bean
15
    
public
 FilterRegistrationBean 
filterRegistrationBean
()
 
{
16
        FilterRegistrationBean filterRegistrationBean = 
new
 FilterRegistrationBean() ;
17
        filterRegistrationBean.setFilter(
new
 WebStatFilter());
18
19
        filterRegistrationBean.addUrlPatterns(
"/*"
); 
// 所有请求进行监控处理
20
        filterRegistrationBean.addInitParameter(
"exclusions"
, 
"*.js,*.gif,*.jpg,*.css,/druid/*"
);
21
        
return
 filterRegistrationBean ;
22
    }
23
    
@Bean
24
    
@ConfigurationProperties
(prefix = 
"spring.datasource"
)
25
    
public
 DataSource 
druidDataSource
()
 
{
26
        
return
 
new
 DruidDataSource();
27
    }
28
29
}
配置中的内容就不一一细讲, 有兴趣的直接百度一下druid就出来很多答案了,现在重新运行一下项目,运行成功之后,在浏览器中输入:http://localhost:8888/druid
这时候,druid监控平台就出现了
此时我们输入在DruidConfig中设置的loginUsername和loginPassword点击登录,一个完整的druid监控管理平台就呈现在我们啦~

Druid非常强大,在这里你可以查看SQL的执行情况、慢SQL、API请求情况等,根据这些可以做一些性能的调优,至于详细的用法,就靠大家自行学习啦~
如果有老铁需要项目源码,请加我:sisi-ceo。
觉得本文对你有帮助?请分享给更多人
关注「编程无界」,提升装逼技能
当前名称:单手撸了个springboot+mybatis+druid
网页网址:http://www.scyingshan.cn/article/gdjoie.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 
