博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于Maven构建整合SpringMVC+Mybtis+Druid
阅读量:6976 次
发布时间:2019-06-27

本文共 7946 字,大约阅读时间需要 26 分钟。

前几天趁空闲时间整合了下SpringMVC+Mybatis+Druid,这里小记录下,这个Demo是基于Maven构建的,数据源用的是阿里巴巴温少的开源项目Druid,数据库用的是Mysql。

由于Eclipse去安装Maven很不方便,也老出错,这里我使用的是Spring Tool Suite(STS,基于 Spring IDE ,提供了其它的一些特性,如 基于 Spring dm Server 的osgi 开发,及其它一些 Spring 项目的支持,如 Spring Roo , Spring Batch 等)。

这里是STS的下载地址(集成了Maven):

 

先看一下整体的项目结构:

  

 

一、相关JAR包

由于是基于Maven的项目,找起JAR包自然就方便了许多,我们只需要知道JAR的名字或者其中关键字就可以很轻松的把JAR包以及依赖JAR下载下来,需要多少下多少。

这里给出pom的配置文件。

pom.xml

1 
3
4.0.0
4
SpringMybatis
5
SpringMybatis
6
war
7
0.0.1-SNAPSHOT
8
SpringMybatis Maven Webapp
9
http://maven.apache.org
10
11
12
org.springframework
13
spring-core
14
3.2.10.RELEASE
15
16
17
org.springframework
18
spring-web
19
3.2.10.RELEASE
20
21
22
org.springframework
23
spring-webmvc
24
3.2.10.RELEASE
25
26
27
org.mybatis
28
mybatis
29
3.2.8
30
31
32
org.mybatis
33
mybatis-spring
34
1.1.1
35
36
37
mysql
38
mysql-connector-java
39
5.0.2
40
41
42
junit
43
junit
44
4.12
45
test
46
47
48
com.alibaba
49
druid
50
1.0.11
51
52
53
log4j
54
log4j
55
1.2.17
56
57
58
59
SpringMybatis
60
61

由于Maven会把JAR包所依赖的JAR包也一起下载下来,这里我们就不需要逐个去写Spring的相关JAR包。

这里用到了阿里巴巴温少的开源项目Druid的数据源,所以额外的多引入了一个Druid的JAR包。

关于JAR的扩展,如果有需要别的可以到: 去查找,然后复制到这个配置文件即可,Maven会帮我们自动下载添加。

 

二、相关配置

spring.xml

1 
2
13 14
15
16 17 18

 

mybatis-spring.xml

1 
2
13 14
15
16
17
18
19
20 21 22
23
24
25
26
27 28
29
30
31
32 33 34
35
37
38
39 40
41 42

 

springmvc.xml

1 
2
14 15
16
17 18
19
20 21
22
24 25
26
29 30 31

 

log4j.properties

1 # 2 #    Copyright 2009-2012 the original author or authors. 3 # 4 #    Licensed under the Apache License, Version 2.0 (the "License"); 5 #    you may not use this file except in compliance with the License. 6 #    You may obtain a copy of the License at 7 # 8 #       http://www.apache.org/licenses/LICENSE-2.0 9 #10 #    Unless required by applicable law or agreed to in writing, software11 #    distributed under the License is distributed on an "AS IS" BASIS,12 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 #    See the License for the specific language governing permissions and14 #    limitations under the License.15 #16 17 ### Global logging configuration18 log4j.rootLogger=DEBUG, stdout19 20 ### Uncomment for MyBatis logging21 log4j.logger.org.apache.ibatis=DEBUG22 23 ### Console output...24 log4j.appender.stdout=org.apache.log4j.ConsoleAppender25 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout26 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

三、项目演示

   

关于model、dao以及mapping的生成方式,在之前的文章《》有提到,这里就不再给出。

UserController.java

1 package lcw.controller; 2  3 import lcw.model.User; 4 import lcw.service.UserServiceI; 5  6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.RequestMapping;10 @Controller11 @RequestMapping("/userController")12 public class UserController {13     14     private UserServiceI userService;15     16     public UserServiceI getUserService() {17         return userService;18     }19     @Autowired20     public void setUserService(UserServiceI userService) {21         this.userService = userService;22     }23 24     @RequestMapping("/showUser")25     public String showUser(Model model){26         User user=userService.getUserById(1);27         model.addAttribute("user", user);28         return "showuser";29     }30 31 }

 

UserServiceI.java

1 package lcw.service;2 3 import lcw.model.User;4 5 public interface UserServiceI {6     7     public User  getUserById(int id);8 9 }

 

UserService.java

package lcw.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import lcw.dao.UserMapper;import lcw.model.User;@Service("userService")public class UserService implements UserServiceI {    private UserMapper userMapper;        public UserMapper getUserMapper() {        return userMapper;    }    @Autowired    public void setUserMapper(UserMapper userMapper) {        this.userMapper = userMapper;    }    @Override    public User getUserById(int id) {        return userMapper.selectByPrimaryKey(id);            }}

 

showuser.jsp

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2     pageEncoding="ISO-8859-1"%> 3  4  5  6 
7 Insert title here 8 9 10 Hello ${user.password} !!11 12

 

web.xml

1 
5 6 7
8
9
characterEncodingFilter
10
org.springframework.web.filter.CharacterEncodingFilter
11
12
encoding
13
UTF-8
14
15
16
17
characterEncodingFilter
18
/*
19
20 21
22
23
springDispatcherServlet
24
org.springframework.web.servlet.DispatcherServlet
25
26
contextConfigLocation
27
classpath:springmvc.xml
28
29
1
30
31 32
33
springDispatcherServlet
34
/
35
36 37 38
39
40
contextConfigLocation
41
classpath:spring.xml,classpath:mybatis-spring.xml
42
43
44
45
org.springframework.web.context.ContextLoaderListener
46
47 48 49 50 51 52

 

看下效果图:

 

好了,SpringMVC+Mybtis+Druid完美整合~

 

作者:

出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!

 

你可能感兴趣的文章
思科交换机各类型中字母的意思?
查看>>
linux基础命令
查看>>
我的友情链接
查看>>
Nutanix CE on Lenovo W520 初探
查看>>
make执行过程
查看>>
Ansible源码解析 Inventory组概念
查看>>
数据备份学习
查看>>
替换空格
查看>>
Linux中源码包的管理
查看>>
ASCII、Unicode、GBK和UTF-8字符编码的区别联系
查看>>
设计模式(行为型模式)——备忘录模式(Memento)
查看>>
[雪峰磁针石博客]kotlin书籍汇总
查看>>
Azure自动化部署运维浅谈
查看>>
浏览器是怎样工作的:渲染引擎,HTML解析
查看>>
centos下LAMP之源码编译安装httpd
查看>>
EBS form日历可选范围设置(calendar.setup )介绍
查看>>
myeclipse莫名其妙的问题
查看>>
iOS-UIWebView添加头部和尾部
查看>>
你最需要了解的H3C交换机端口安全模式
查看>>
常用Linux路由命令(route、ip、ifconfig等等)
查看>>