Eclipse Meaven Spring SpringMVC Mybaits整合

本示例是在:Ubuntu15上实现的;Windows上安装Maven将不太相同。

Maven Install

  1. Run command sudo apt-get install maven, to install the latest Apache Maven.
  2. Run command mvn -version to verify your installation.
  3. Where is Maven installed?
    The command apt-get install the Maven in /usr/share/maven
    The Maven configuration files are stored in /etc/maven

Eclipse Maven Plugin - m2e

  1. open Eclipse -> Help -> click "Install New Software" -> click "add"
  • Name:m2e
  • Location:http://download.eclipse.org/technology/m2e/releases
  1. click "ok" -> click "Maven Integration for Eclipse" -> click "Next"
  2. restrat Eclipse
  3. config m2e -> Window -> Preferences -> Maven -> Installations -> click "Add…" -> select Maven

Create a Maven Project

  1. File -> New -> New Maven project
  2. select "Use default Workspace location"
  3. select "maven-archetype-j2ee-simple"
  4. input info -> Finish
  5. 选中项目右键菜单中选择Properties -> Project Facets -> select "Dynamic Web Module" Version "3.1"

Tips:

  • 如果在Project Facets选择版本时“can not change”,可以在项目目录下手动修改.settings/org.eclipse.wst.common.project.facet.core.xml文件配置
  • 项目自动生成的web.xml版本较低,手动修改
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1" metadata-complete="true">
</web-app>
  • 项目结构
├── src├── main|   ├── java //java源代码|   ├── resources //配置资源文件|   └── webapp //web文件| └── test└── java //junit测试

pom.xml Config

Github-maven-mybatis-spring-springMVC【pom.xml】

<!-- junit4 -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope>
</dependency>
<!-- 日志 -->
<!-- 实现slf4j接口整合 -->
<!-- JDBC MySQL Driver -->
<!-- DAO框架 mybatis -->
<!-- Servlet API -->
<!-- 1 Spring 核心依赖 -->
<!-- 2 Spring DAO依赖 -->
<!-- 3 Spring web相关依赖 -->
<!-- 4 Spring Test相关依赖 -->

logback.xml Config

<configuration><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --><encoder><pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern></encoder></appender><root level="info"><appender-ref ref="STDOUT" /></root>
</configuration>

Mybatis Config

Github-maven-mybatis-spring-springMVC【mybatis-config.xml】

<configuration><settings><!-- 使用jdbc的getGeneratedKays 获取数据库自增主键 --><setting name="useGeneratedKeys" value="true" /><!-- 使用列别名替换列名 --><setting name="useColumnLabel" value="true" /><!-- 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。 --><setting name="mapUnderscoreToCamelCase" value="true" /></settings>
</configuration>    

Spring Config

Github-maven-mybatis-spring-springMVC【spring】

Spring-DAO Config

    <!-- 1 数据库配置文件位置 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 2 数据库连接池 --><!-- Employee DB data source. --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.dburl}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- c3p0连接池 私有属性 --><property name="maxPoolSize" value="${jdbc.maxPoolSize}" /><property name="minPoolSize" value="${jdbc.minPoolSize}" /><!-- 关闭连接后不自动commit --><property name="autoCommitOnClose" value="false" /><!-- 获取连接超时时间 --><property name="checkoutTimeout" value="1000" /><!-- 获取连接失败重试次数 --><property name="acquireRetryAttempts" value="2" /></bean><!-- 设计原则:约定大于配置 --><!-- 3 配置 SqlSessionFactory 对象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 配置mybitis 全局配置文件 --><property name="configLocation" value="classpath:mybatis-config.xml" /><!-- 扫描entity包 使用别名 --><property name="typeAliasesPackage" value="com.moma.dmv.entity" /><!-- 扫描sql配置文件 mapper 需要的xml --><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean><!-- 4 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 注入sqlsessionFactory --><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><!-- 给出需要扫描Dao接口包 --><property name="basePackage" value="com.moma.dmv.dao" /></bean>

Spring-Service Config

<!-- 扫描service包下 所有使用注解的类型 --><context:component-scan base-package="com.moma.dmv.service" /><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 配置基于注解的声明式事务 --><tx:annotation-driven transaction-manager="transactionManager" /><!-- 使用注解控制事务方法的优点1:开发团队达成一致约定,明确标注事务方法的编程风格 2:保证事务方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求或者剥离到事务方法外部3:不是所有的方法都需要事务,比如只有一条修改操作,只读操作不需要事务控制-->

Spring-Web Config

    <!-- 1:开启springMVC 注解模式 --><mvc:annotation-driven /><!-- 2 静态资源默认servlet配置 1 加入对静态资源的处理 js gif png 2 允许使用“/”做整体映射 --><mvc:default-servlet-handler /><!-- 3:配置jsp 显示ViewResolver --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 决定视图类型,如果添加了jstl支持(即有jstl.jar),那么默认就是解析为jstl视图 --><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><!-- 视图前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 视图后缀 --><property name="suffix" value=".jsp" /></bean><mvc:resources location="/resources/" mapping="/resources/**" /><!-- 4:扫描web相关的bean --><context:component-scan base-package="com.moma.dmv.web" />

DAO Mapper Example

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moma.dmv.dao.InfoDao"><select id="queryById" resultType="Info" parameterType="long"><![CDATA[select id,`key`,`value` from info where id = #{id}]]></select><select id="queryAll" resultType="Info"><![CDATA[select id,key,value from info limit #{offset},#{limit}]]></select>  </mapper>

web.xml Config

    <servlet><servlet-name>dmv-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置springMVC需要加载的配置文件 spring-dao.xml spring-service.xml spring-web.xml mybatis -> spring -> springMVC --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-*.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>dmv-dispatcher</servlet-name><!-- 默认匹配所有的请求 --><url-pattern>/</url-pattern></servlet-mapping>

Junit Example


import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.moma.dmv.dao.InfoDao;
import com.moma.dmv.entity.Info;RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations = { "classpath:spring/spring-dao.xml" })
public class InfoDaoTest {@Resourceprivate InfoDao infoDao;private Logger logger = LoggerFactory.getLogger(this.getClass());@Testpublic void testQueryById() throws Exception {long id = 1;Info info = infoDao.queryById(id);logger.info(info.toString());}
}

参考文章:

  1. MKyong-How to install Maven on Ubuntu
  2. Java之道-使用Eclipse构建Maven项目 (step-by-step)

转载于:https://www.cnblogs.com/wave-gbt/p/5833022.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372379.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

抽象类和抽象函数

1.抽象函数的语法特征 什么是抽象函数&#xff1f; 只有函数的定义,没有函数体的函数被称为抽象函数&#xff1b; Abstract void fun(); 如果一个类拥有一个或一个以上的抽象函数&#xff0c;那么这个类必须被定义为抽象类 2.抽象类的语法特征 使用abstract定义的类被称之…

并发–执行程序和Spring集成

基于线程池/执行器的实现 比原始线程版本更好的方法是基于线程池的线程池&#xff0c;其中基于运行任务的系统定义了适当的线程池大小– CPU数量/&#xff08;任务的1-Blocking Coefficient&#xff09;。 Venkat Subramaniams书中有更多详细信息&#xff1a; 首先&#xff0c…

后面的参数_英特尔I系列CPU大家都知道,后面的参数你有没有了解过

嗨&#xff01;大家好&#xff0c;我是伟仔&#xff0c;今天主要是和大家聊下CPU。大多数人买笔记本或台式电脑对CPU的要求就知道I5或者I7之类的。像是I7一定比I5要好&#xff0c;I3很LOU这样的&#xff0c;当然这样子的观点是不正确的&#xff0c;今天我会告诉大家&#xff0c…

設置Linux保留物理內存並使用 (1)

在Linux系統中可以通過memblock來設置系統保留物理內存&#xff0c;防止這些內存被內存管理系統分配出去。 作者&#xff1a; 彭東林 郵箱&#xff1a; pengdonglin137163.com 平臺 硬件平臺&#xff1a; TQ2440 Linux版本&#xff1a;Linux 3.14.45 說明 1. 在tq2440上&#x…

移动端

http://www.w3cplus.com/mobile/lib-flexible-for-html5-layout.html 移动端手淘使用方案 移动端px自动转换rem插件 CSSREM Flexible 转载于:https://www.cnblogs.com/yuruiweb/p/6723580.html

OutOfMemoryError:Java堆空间–分析和解决方法

java.lang.OutOfMemoryError&#xff1a;Java堆问题是在支持或开发复杂的Java EE应用程序时可能会遇到的最复杂的问题之一。 这篇简短的文章将为您提供此JVM HotSpot OutOfMemoryError错误消息的描述&#xff0c;以及在解决该问题之前应如何解决此问题。 有关如何确定要处理的O…

函数伪代码_Excel常用函数

欢迎大家在此收看任我行office教程系列&#xff0c;这一期我来为大家讲什么内容呢&#xff0c;那就是几个office的几个常用函数了&#xff0c;如果您不会这些函数和函数嵌套那么您的Excel电子表格也就别玩了哈&#xff0c;那么他们分别是什么函数呢。咱们现在隆重有请这几位函数…

阻止Ajax多次提交

1、Ajax的abort() xhr $.ajax({})if (xhr){xhr.abort(); } 2、通过在Ajax的beforeSend()方法以及complete()方法添加删除类&#xff0c;对类进行判断&#xff0c;对于两者来回切换的时候&#xff0c;对类的设置不好进行操作上的时候&#xff0c;可以通过使用一个input框&#…

POJ3675 Telescope 圆和多边形的交

POJ3675 用三角剖分可以轻松搞定&#xff0c;数据也小 随便AC。 #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<vector> usi…

windows搭建python开发环境方法_04 Windows下搭建 Python 开发环境 - Python 入门教程

前面两个小节中我们已经学习了在 MacOS 和 Ubuntu 中安装 Python 的开发环境。当然&#xff0c;作为用户基数最多的 Windows 操作系统&#xff0c;我们当然不会忘记&#xff0c;这节课我们就来学习下如何在 Windows 下搭建 Python 的开发环境。1. 下载 Python1.1 Python 2 与 P…

消除view旋转后边缘有锯齿的情况

view的layer中有个属性叫 allowsEdgeAntialiasing&#xff1b; 在形变后有边缘有锯齿的话 可以 view.layer.allowsEdgeAntialiasing YES; 消除锯齿 如果直接在*-Info.plist配置 Renders with edge antialiasing YES 会导致UIAlertView显示有问题。转载于:https://www.cnblogs…

Google AppEngine:任务队列API

任务队列 com.google.appengine.api.taskqueue 使用任务队列&#xff0c;用户可以发起一个请求&#xff0c;以使应用程序执行此请求之外的工作。 它们是进行后台工作的强大工具。 此外&#xff0c;您可以将工作组织成小的离散单元&#xff08;任务&#xff09;。 然后&#xf…

打印5列五颗星_55组“数学顺口溜” 大九九乘法口诀表!孩子想学好数学必须背熟...

小学数学需要记住的知识点还是比较多的&#xff0c;看到这些知识点&#xff0c;很多孩子都觉得枯燥&#xff0c;不愿意用心去记。今天&#xff0c;我们给孩子们汇总了55组“数学顺口溜”和大九九乘法口诀&#xff0c;让孩子们在轻松有趣的氛围中学到知识&#xff01;55组“顺口…

C++学习48 对ASCII文件的读写操作

如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件)。程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符。 对ASCII文件的读写操作可以用以下两种方法&#xff1a;1) 用流插入运算符“<<”和流提取…

文献综述写作之“结构内容”

综述&#xff1a; 又称文献综述&#xff0c;英文名为review。它是利用已发表的文献资料为原始素材撰写的&#xff0c;通过对已发表材料的组织、综合和评价&#xff0c;以及对当前研究进展的考察来澄清问题。在某种意义上&#xff0c;综述论文具有一定的指导性&#xff0c;包括以…

NetBeans 7.2 beta:更快,更有用

NetBeans 7.2的beta版本引起了极大的兴奋。 在本文中&#xff0c;我将简要介绍一下此版本令人兴奋的原因&#xff08;包括更好的性能&#xff0c;提供更多的提示以及集成FindBugs&#xff09;。 NetBeans 7.2 beta在典型的下载捆绑软件中可用&#xff0c;从较小的Java SE&#…

地铁闸门会夹伤人吗_家长们注意啦!又有孩子被地铁闸机夹翻

原标题&#xff1a;家长们注意啦&#xff01;又有孩子被地铁闸机夹翻现代快报讯(通讯员狄公宣记者顾元森)家长带着孩子通过地铁站闸机&#xff0c;这件事情看似简单&#xff0c;却隐藏着风险。近日&#xff0c;南京地铁又发生了一起儿童被闸机夹翻的事&#xff0c;所幸孩子并无…

WPF DevExpress 设置雷达图Radar样式

DevExpress中定义的ChartControl很不错&#xff0c;很多项目直接使用这种控件。 本节讲述雷达图的样式设置 <Grid><Grid.Resources><DataTemplate x:Key"LabelItemDataTemplate" DataType"dxc:SeriesLabelItem"><Border CornerRadius…

mxnet系列教程之1-第一个例子

第一个例子当然是mnist的例子 假设已经成功安装了mxnet 例子的代码如下&#xff1a; cd mxnet/example/image-classification python train_mnist.py这样就会运行下去 train_mnist.py的代码为 """ Train mnist, see more explanation at http://mxnet.io/tutori…

Apache Shiro第3部分–密码学

除了保护网页和管理访问权限外&#xff0c; Apache Shiro还执行基本的加密任务。 该框架能够&#xff1a; 加密和解密数据&#xff0c; 哈希数据&#xff0c; 生成随机数。 Shiro没有实现任何加密算法。 所有计算都委托给Java密码学扩展&#xff08;JCE&#xff09;API。 使…