Eclipse Meaven Spring SpringMVC Mybaits整合
本示例是在:Ubuntu15上实现的;Windows上安装Maven将不太相同。
Maven Install
- Run command
sudo apt-get install maven, to install the latest Apache Maven. - Run command
mvn -version to verifyyour installation. - Where is Maven installed?
The commandapt-getinstall the Maven in/usr/share/maven
The Maven configuration files are stored in/etc/maven
Eclipse Maven Plugin - m2e
- open Eclipse -> Help -> click "Install New Software" -> click "add"
- Name:m2e
- Location:http://download.eclipse.org/technology/m2e/releases
- click "ok" -> click "Maven Integration for Eclipse" -> click "Next"
- restrat Eclipse
- config m2e -> Window -> Preferences -> Maven -> Installations -> click "Add…" -> select Maven
Create a Maven Project
- File -> New -> New Maven project
- select "Use default Workspace location"
- select "maven-archetype-j2ee-simple"
- input info -> Finish
- 选中项目右键菜单中选择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/javaee
http://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 -->
<bean
class="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 {
@Resource
private InfoDao infoDao;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Test
public void testQueryById() throws Exception {
long id = 1;
Info info = infoDao.queryById(id);
logger.info(info.toString());
}
}
参考文章:
Eclipse Meaven Spring SpringMVC Mybaits整合的更多相关文章
- SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)
SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...
- 框架篇:Spring+SpringMVC+hibernate整合开发
前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...
- spring+springmvc+hibernate整合遇到的问题
spring+springmvc+hibernate整合遇到的问题2016年10月20日 23:24:03 守望dfdfdf 阅读数:702 标签: ssh学习经历的异常exception异常框架更多 ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- ssm之spring+springmvc+mybatis整合初探
1.基本目录如下 2.首先是向lib中加入相应的jar包 3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...
- springmvc框架(Spring SpringMVC, Hibernate整合)
直接干货 model 考虑给用户展示什么.关注支撑业务的信息构成.构建成模型. control 调用业务逻辑产生合适的数据以及传递数据给视图用于呈献: view怎样对数据进行布局,以一种优美的方式展示 ...
- 用 eclipse 创建一个简单的 meaven spring springMvc mybatis 项目
下面是整体步骤: 1: 先创建一个Maven 项目: 选择跳过骨架: 因为要搭建的是 web 项目 所以这个地方选择 war 包; 点击完成 这样就完成 Maven项目的搭建: 接下俩 先把 Mav ...
随机推荐
- 以向VS 程序打包集成自动写入注册表功能为例,介绍如何实现自由控制安装过程
最近由于项目部署时需要更灵活的控制程序安装的流程以及自定义安装行为,特意研究了一下VS程序打包,把解决办法和大家分享一下. 以VS2010为例: 这是一个已经设置好最基本的Visual Studio ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- http status code
属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- mysql查询性能优化
mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...
- PHP安装
工具 http://www.cnblogs.com/xiwang6428/p/4315049.html http://www.iteye.com/news/22672 1 安装:sudo apt-ge ...
- 多用多学之Java中的Set,List,Map
很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...
- Raspkate - 基于.NET的可运行于树莓派的轻量型Web服务器
最近在业余时间玩玩树莓派,刚开始的时候在树莓派里写一些基于wiringPi库的C语言程序来控制树莓派的GPIO引脚,从而控制LED发光二极管的闪烁,后来觉得,是不是可以使用HTML5+jQuery等流 ...
- 如何使用RobotFramework编写好的测试用例
如何使用Robot Framework编写优秀的测试用例 概述 命名 测试套件命名 测试用例命名 关键字命名 setup和teardown的命名 文档 测试套件文档 测试用例文档 用户关键字文档 测试 ...
- 【完全开源】知乎日报UWP版(上篇):界面设计、官方API分析
目录 说明 使用Fiddler分析android版API 部分效果图 关于源码 说明 在做博客园UWP版的时候其实就有做知乎日报的打算了,前段时间一直出差,在酒店里用Fiddler简单的分析了一下An ...