基于Maven构建整合SpringMVC+Mybtis+Druid 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

前几天趁空闲时间整合了下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 举报,一经查实,本站将立刻删除。

相关推荐


开发过程中是不可避免地会出现各种异常情况的,例如网络连接异常、数据格式异常、空指针异常等等。异常的出现可能导致程序的运行出现问题,甚至直接导致程序崩溃。因此,在开发过程中,合理处理异常、避免异常产生、以及对异常进行有效的调试是非常重要的。 对于异常的处理,一般分为两种方式: 编程式异常处理:是指在代
说明:使用注解方式实现AOP切面。 什么是AOP? 面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。 通俗描述:不通过修改源代码方式,在主干功能里面添加新功能。 AOP底层使用动态代理。 AOP术语 连接点
Spring MVC中的拦截器是一种可以在请求处理过程中对请求进行拦截和处理的机制。 拦截器可以用于执行一些公共的操作,例如日志记录、权限验证、数据转换等。在Spring MVC中,可以通过实现HandlerInterceptor接口来创建自定义的拦截器,并通过配置来指定拦截器的应用范围和顺序。 S
在 JavaWeb 中,共享域指的是在 Servlet 中存储数据,以便在同一 Web 应用程序的多个组件中进行共享和访问。常见的共享域有四种:ServletContext、HttpSession、HttpServletRequest、PageContext。 ServletContext 共享域:
文件上传 说明: 使用maven构建web工程。 使用Thymeleaf技术进行服务器页面渲染。 使用ResponseEntity实现下载文件的功能。 @Controller public class FileDownloadAndUpload { @GetMapping(&quot;/file/d
创建初始化类,替换web.xml 在Servlet3.0环境中,Web容器(Tomcat)会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,如果找到的话就用它来配置Servlet容器。 Spring提供了这个接口的实现,名为SpringS
在 Web 应用的三层架构中,确保在表述层(Presentation Layer)对数据进行检查和校验是非常重要的。正确的数据校验可以确保业务逻辑层(Business Logic Layer)基于有效和合法的数据进行处理,同时将错误的数据隔离在业务逻辑层之外。这有助于提高系统的健壮性、安全性和可维护
什么是事务? 事务(Transaction)是数据库操作最基本单元,逻辑上一组操作,要么都成功,要么都失败,如果操作之间有一个失败所有操作都失败 。 事务四个特性(ACID) 原子性 一组操作要么都成功,要么都失败。 一致性 一组数据从事务1合法状态转为事务2的另一种合法状态,就是一致。 隔离性 事
什么是JdbcTemplate? Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作。 准备工作 引入jdbcTemplate的相关依赖: 案例实操 创建jdbc.properties文件,配置数据库信息 jdbc.driver=com.mysql.cj.
SpringMVC1.MVC架构MVC是模型(Model)、视图(View)、控制器(Controller)的简写,是一种软件设计规范是将业务逻辑、数据、显示分离的方法来写代码MVC主要作用是:降低了视图和业务逻辑之间的双向耦合MVC是一个架构模型,不是一种设计模式。1.model(模型)数据模型,提供要展示的数据,因此包
SpringMVC学习笔记1.SpringMVC应用1.1SpringMVC简介​SpringMVC全名叫SpringWebMVC,是⼀种基于Java的实现MVC设计模型的请求驱动类型的轻量级Web框架,属于SpringFrameWork的后续产品。​MVC全名是ModelViewController,是模型(model)-视图(view)-控制器(co
11.1数据回显基本用法数据回显就是当用户数据提交失败时,自动填充好已经输入的数据。一般来说,如果使用Ajax来做数据提交,基本上是没有数据回显这个需求的,但是如果是通过表单做数据提交,那么数据回显就非常有必要了。11.1.1简单数据类型简单数据类型,实际上框架在这里没有
一、SpringMVC简介1、SpringMVC中重要组件DispatcherServlet:前端控制器,接收所有请求(如果配置/不包含jsp)HandlerMapping:解析请求格式的.判断希望要执行哪个具体的方法.HandlerAdapter:负责调用具体的方法.ViewResovler:视图解析器.解析结果,准备跳转到具体的物
1.它们主要负责的模块Spring主要应用于业务逻辑层。SpringMVC主要应用于表现层。MyBatis主要应用于持久层。2.它们的核心Spring有三大核心,分别是IOC(控制反转),DI(依赖注入)和AOP(面向切面编程)。SpringMVC的核心是DispatcherServlet(前端控制器)。MyBatis的核心是ORM(对
3.注解开发Springmvc1.使用注解开发要注意开启注解支持,2.注解简化了,处理映射器和处理适配器,只用去管视图解析器即可案例代码:1.web.xml,基本不变可以直接拿去用<!--调用DispatcherServlet--><servlet><servlet-name>springmvc</servlet-name>
拦截器概述SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。**过滤器与拦截器的区别:**拦截器是AOP思想的具体应用。过滤器servlet规范中的一部分,任何javaweb工程都可以使用
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="
学习内容:1、SSH&SSM2、Spring3、Struts2&SpringMVC4、Hibernate&MyBatis学习产出:1.SSH和SSM都是有Spring框架的,他们两个差不多。2.Spring分为四个模块,持久层,表示层,检测层,还有核心层,核心层分为2个关键核心功能。分别为,控制反转(IOC),依赖注入(DI),和面向切面编程
一、SpringMVC项目无法引入js,css的问题具体原因是css和js等被SpringMVC拦截了:解决方案:在spring-mvc.xml中配置<mvc:default-servlet-handler/><?xmlversion="1.0"encoding="UTF-8"?><beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
开发环境:Eclipse/MyEclipse、Tomcat8、Jdk1.8数据库:MySQL前端:JavaScript、jQuery、bootstrap4、particles.js后端:maven、SpringMVC、MyBatis、ajax、mysql读写分离、mybatis分页适用于:课程设计,毕业设计,学习等等系统介绍