前几天趁空闲时间整合了下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</ 4 groupId>SpringMybatis 5 artifactId 6 packaging>war 7 version>0.0.1-SNAPSHOT 8 name>SpringMybatis Maven Webapp 9 url>http://maven.apache.org10 dependencies11 dependency12 >org.springframework13 >spring-core14 >3.2.10.RELEASE15 16 17 18 >spring-web19 20 21 22 23 >spring-webmvc24 25 26 27 >org.mybatis28 >mybatis29 >3.2.830 31 32 33 >mybatis-spring34 >1.1.135 36 37 >mysql38 >mysql-connector-java39 >5.0.240 41 42 >junit43 44 >4.1245 scope>test46 47 48 >com.alibaba49 >druid50 >1.0.1151 52 53 >log4j54 55 >1.2.1756 57 58 build59 finalName60 61 project>
由于Maven会把JAR包所依赖的JAR包也一起下载下来,这里我们就不需要逐个去写Spring的相关JAR包。
这里用到了阿里巴巴温少的开源项目Druid的数据源,所以额外的多引入了一个Druid的JAR包。
关于JAR的扩展,如果有需要别的可以到:http://search.maven.org/ 去查找,然后复制到这个配置文件即可,Maven会帮我们自动下载添加。
二、相关配置
spring.xml
<?xml version="1.0" encoding="UTF-8"?> 2 beans ="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 ="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/aop 10 http://www.springframework.org/schema/aop/spring-aop.xsd 11 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
配置数据源 bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"property ="url" value="jdbc:mysql:///test"></property17 ="username"="root"18 ="password"19 bean20 21 22 Mybatis文件 23 ="sqlSessionFactory"="org.mybatis.spring.SqlSessionFactoryBean"24 ref="dataSource"="mapperLocations" value="classpath:lcw/mapping/*.xml"26 27 28 class="org.mybatis.spring.mapper.MapperScannerConfigurer"29 ="basePackage"="lcw.dao"="sqlSessionFactoryBeanName" value ="sqlSessionFactory"31 32 33 34 事务管理器 35 ="transactionManager" 36 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"37 38 39 40 tx:annotation-driven transaction-manager="transactionManager" 41 42 >
springmvc.xml
xmlns:p="http://www.springframework.org/schema/p" xmlns:context xmlns:mvc="http://www.springframework.org/schema/mvc" 6 =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 13 "14 启用spring mvc 注解 16 context:annotation-config 18 设置使用注解的类所在的jar包 ="lcw.controller"context:component-scan20 21 完成请求和注解POJO的映射 bean 23 ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" 24 25 对转向页面的路径解析。prefix:前缀, suffix:后缀 27 ="org.springframework.web.servlet.view.InternalResourceViewResolver" 28 p:prefix="/" p:suffix=".jsp" 29 30 31 >
log4j.properties
# # 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. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing,software # distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied. # See the License for the specific language governing permissions and 14 # limitations under the License. 15 17 ### Global logging configuration log4j.rootLogger=DEBUG,stdout 19 20 ### Uncomment for MyBatis logging 21 log4j.logger.org.apache.ibatis=DEBUG 22 23 ### Console output... 24 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 25 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 26 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
三、项目演示
关于model、dao以及mapping的生成方式,在之前的文章《使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件》有提到,这里就不再给出。
UserController.java
package lcw.controller; 2 import lcw.model.User; lcw.service.UserServiceI; 5 org.springframework.beans.factory.annotation.Autowired; org.springframework.stereotype.Controller; org.springframework.ui.Model; org.springframework.web.bind.annotation.RequestMapping; @Controller 11 @RequestMapping("/userController") public UserController { 13 private UserServiceI userService; 15 public UserServiceI getUserService() { return userService; } 19 @Autowired 20 void setUserService(UserServiceI userService) { this.userService =22 23 24 @RequestMapping("/showUser" String showUser(Model model){ 26 User user=userService.getUserById(1); 27 model.addAttribute("user",user); 28 return "showuser"; 29 31 }
UserServiceI.java
1 lcw.service; 2 3 4 5 interface UserServiceI { 6 7 public User getUserById(int id); 8 9 }
UserService.java
lcw.service; org.springframework.stereotype.Service; lcw.dao.UserMapper; lcw.model.User; @Service("userService"class UserService implements UserServiceI { UserMapper userMapper; UserMapper getUserMapper() { userMapper; } @Autowired setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public User getUserById( id) { userMapper.selectByPrimaryKey(id); } }
showuser.jsp
1 <%@ page language="java contentTypetext/html; charset=ISO-8859-1" 2 pageEncodingISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"htmlheadmeta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"title>Insert title herebody Hello ${user.password} !! >
web.xml
web-app xmlns:xsi xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" 6 解决工程编码过滤器 filter 9 filter-name>characterEncodingFilter10 filter-class>org.springframework.web.filter.CharacterEncodingFilterinit-paramparam-name>encodingparam-value>UTF-814 filter-mappingurl-pattern>/* SpringMVC配置 servlet23 servlet-name>springDispatcherServletservlet-class>org.springframework.web.servlet.DispatcherServlet26 >contextConfigLocation>classpath:springmvc.xmlload-on-startup>130 31 32 servlet-mapping33 34 >/36 37 配置文件所在位置 39 context-param>classpath:spring.xml,classpath:mybatis-spring.xml42 43 Spring配置(监听器) 44 listener45 listener-class>org.springframework.web.context.ContextLoaderListener46 47 48 49 50 51 52 web-app>
看下效果图:
好了,SpringMVC+Mybtis+Druid完美整合~
作者:Balla_兔子
出处:http://www.cnblogs.com/lichenwei/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。