麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

基于Maven構建整合SpringMVC+Mybtis+Druid

2019-11-14 22:08:15
字體:
來源:轉載
供稿:網友
基于Maven構建整合SPRingMVC+Mybtis+Druid

前幾天趁空閑時間整合了下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):http://spring.io/tools/sts/all

先看一下整體的項目結構:

  

一、相關JAR包

由于是基于Maven的項目,找起JAR包自然就方便了許多,我們只需要知道JAR的名字或者其中關鍵字就可以很輕松的把JAR包以及依賴JAR下載下來,需要多少下多少。

這里給出pom的配置文件。

pom.xml

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3     <modelVersion>4.0.0</modelVersion> 4     <groupId>SpringMybatis</groupId> 5     <artifactId>SpringMybatis</artifactId> 6     <packaging>war</packaging> 7     <version>0.0.1-SNAPSHOT</version> 8     <name>SpringMybatis Maven Webapp</name> 9     <url>http://maven.apache.org</url>10     <dependencies>11         <dependency>12             <groupId>org.springframework</groupId>13             <artifactId>spring-core</artifactId>14             <version>3.2.10.RELEASE</version>15         </dependency>16         <dependency>17             <groupId>org.springframework</groupId>18             <artifactId>spring-web</artifactId>19             <version>3.2.10.RELEASE</version>20         </dependency>21         <dependency>22             <groupId>org.springframework</groupId>23             <artifactId>spring-webmvc</artifactId>24             <version>3.2.10.RELEASE</version>25         </dependency>26         <dependency>27             <groupId>org.mybatis</groupId>28             <artifactId>mybatis</artifactId>29             <version>3.2.8</version>30         </dependency>31         <dependency>32             <groupId>org.mybatis</groupId>33             <artifactId>mybatis-spring</artifactId>34             <version>1.1.1</version>35         </dependency>36         <dependency>37             <groupId>mysql</groupId>38             <artifactId>mysql-connector-java</artifactId>39             <version>5.0.2</version>40         </dependency>41         <dependency>42             <groupId>junit</groupId>43             <artifactId>junit</artifactId>44             <version>4.12</version>45             <scope>test</scope>46         </dependency>47         <dependency>48             <groupId>com.alibaba</groupId>49             <artifactId>druid</artifactId>50             <version>1.0.11</version>51         </dependency>52         <dependency>53             <groupId>log4j</groupId>54             <artifactId>log4j</artifactId>55             <version>1.2.17</version>56         </dependency>57     </dependencies>58     <build>59         <finalName>SpringMybatis</finalName>60     </build>61 </project>

由于Maven會把JAR包所依賴的JAR包也一起下載下來,這里我們就不需要逐個去寫Spring的相關JAR包。

這里用到了阿里巴巴溫少的開源項目Druid的數據源,所以額外的多引入了一個Druid的JAR包。

關于JAR的擴展,如果有需要別的可以到:http://search.maven.org/去查找,然后復制到這個配置文件即可,Maven會幫我們自動下載添加。

二、相關配置

spring.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  6     http://www.springframework.org/schema/beans/spring-beans.xsd 7     http://www.springframework.org/schema/context 8     http://www.springframework.org/schema/context/spring-context.xsd 9     http://www.springframework.org/schema/aop10     http://www.springframework.org/schema/aop/spring-aop.xsd11     http://www.springframework.org/schema/tx 12     http://www.springframework.org/schema/tx/spring-tx.xsd">13 14     <!-- 自動注入 -->15     <context:component-scan base-package="lcw/service"/>16 17 18 </beans>

mybatis-spring.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  6     http://www.springframework.org/schema/beans/spring-beans.xsd 7     http://www.springframework.org/schema/context 8     http://www.springframework.org/schema/context/spring-context.xsd 9     http://www.springframework.org/schema/aop10     http://www.springframework.org/schema/aop/spring-aop.xsd11     http://www.springframework.org/schema/tx 12     http://www.springframework.org/schema/tx/spring-tx.xsd">13 14     <!-- 配置數據源 -->15     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">16         <property name="url" value="jdbc:mysql:///test"></property>17         <property name="username" value="root"></property>18         <property name="passWord" value="root"></property>19     </bean>20     21     22     <!-- Mybatis文件 -->23     <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">24         <property name="dataSource" ref="dataSource"></property>25         <property name="mapperLocations"  value="classpath:lcw/mapping/*.xml"></property>26     </bean>27     28     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">29         <property name="basePackage" value="lcw.dao"></property>30         <property name="sqlSessionFactoryBeanName" value    ="sqlSessionFactory"></property>31     </bean>32 33 34     <!-- 事務管理器 -->35     <bean id="transactionManager"36         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">37         <property name="dataSource" ref="dataSource"></property>38     </bean>39 40     <tx:annotation-driven transaction-manager="transactionManager" />41 42 </beans>

springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4     xmlns:context="http://www.springframework.org/schema/context" 5     xmlns:mvc="http://www.springframework.org/schema/mvc" 6     xsi:schemaLocation="   7     http://www.springframework.org/schema/beans    8     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   9     http://www.springframework.org/schema/context  10     http://www.springframework.org/schema/context/spring-context-3.0.xsd  11     http://www.springframework.org/schema/mvc  12     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  13     ">14 15     <!-- 啟用spring mvc 注解 -->16     <context:annotation-config />17 18     <!-- 設置使用注解的類所在的jar包 -->19     <context:component-scan base-package="lcw.controller"></context:component-scan>20 21     <!-- 完成請求和注解POJO的映射 -->22     <bean23         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />24 25     <!-- 對轉向頁面的路徑解析。prefix:前綴, suffix:后綴 -->26     <bean27         class="org.springframework.web.servlet.view.InternalResourceViewResolver"28         p:prefix="/" p:suffix=".jsp" />29 30 31 </beans>

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的生成方式,在之前的文章《使用Mybatis-Generator自動生成Dao、Model、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 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body>10     Hello ${user.password} !!11 </body>12 </html>

web.xml

 1 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 3     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4     id="WebApp_ID" version="2.5"> 5  6  7     <!-- 解決工程編碼過濾器 --> 8     <filter> 9         <filter-name>characterEncodingFilter</filter-name>10         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>11         <init-param>12             <param-name>encoding</param-name>13             <param-value>UTF-8</param-value>14         </init-param>15     </filter>16     <filter-mapping>17         <filter-name>characterEncodingFilter</filter-name>18         <url-pattern>/*</url-pattern>19     </filter-mapping>20 21     <!-- SpringMVC配置 -->22     <servlet>23         <servlet-name>springDispatcherServlet</servlet-name>24         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>25         <init-param>26             <param-name>contextConfigLocation</param-name>27             <param-value>classpath:springmvc.xml</param-value>28         </init-param>29         <load-on-startup>1</load-on-startup>30     </servlet>31 32     <servlet-mapping>33         <servlet-name>springDispatcherServlet</servlet-name>34         <url-pattern>/</url-pattern>35     </servlet-mapping>36 37 38     <!-- 配置文件所在位置 -->39     <context-param>40         <param-name>contextConfigLocation</param-name>41         <param-value>classpath:spring.xml,classpath:mybatis-spring.xml</param-value>42     </context-param>43     <!-- Spring配置(監聽器) -->44     <listener>45         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>46     </listener>47 48 49 50 51 52 </web-app>

看下效果圖:

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

作者:Balla_兔子出處:http://www.companysz.com/lichenwei/本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,日后必有一番作為!旁邊有“推薦”二字,你就順手把它點了吧,相得準,我分文不收;相不準,你也好回來找我!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 在线视频观看国产 | 蜜桃麻豆视频 | 欧美成人做爰高潮片免费视频 | 欧美a∨一区二区三区久久黄 | 黄色成人小视频 | 国产日韩a| 色妞欧美 | 国产精品久久久久久影院8一贰佰 | 狠狠操精品视频 | 一区二区三区视频播放 | 国产一区二区午夜 | 国产精品www | 国产精品成人一区二区三区电影毛片 | 91,视频免费看 | 亚洲免费在线看 | 国产精品区一区二区三区 | 国产午夜免费福利 | 日韩a毛片免费观看 | 欧美一级高清片在线 | 久久99精品久久久久久园产越南 | 日本一级黄色大片 | 国产一精品一av一免费爽爽 | 久久久国产精品免费观看 | 日产精品久久久一区二区开放时间 | 成人在线视频网 | 136福利视频 | 久久97视频 | 91午夜理伦私人影院 | 亚洲第一男人天堂 | 亚洲天堂岛国片 | 亚洲小视频网站 | 成人激情视频网 | 国产精品自在线拍 | 成人不卡在线观看 | 久草在线资源福利站 | 成人h精品动漫一区二区三区 | 欧美精品成人 | 中文字幕国产一区 | 欧美精品免费一区二区三区 | 欧美成人精品一区二区三区 | 黄网站在线免费 |