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 verify
your installation. - Where is Maven installed?
The commandapt-get
install 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 ...
随机推荐
- 笔记:Memory Notification: Library Cache Object loaded into SGA
笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...
- Windows cmd 长时间不输出新内容 直到按下ctrl + c 取消或者回车的解决办法
换了一台新电脑, 在使用 ant 拷贝大量文件的时候 cmd 窗口过了很久没有继续输出新的内容,远远超过平时的耗时, 以为已经卡死 按下 ctrl + c 取消, 这时并没有取消, 而是输出了新内容, ...
- 在 Linux 上使用 Jexus + Mono 建立 Asp.Net 网站.
godaddy 买了个net空间,一点也不好用. 几个G的数据, 上传数据只有几kb , 想用 ssh 登录上去用 wget 下载,也不行 windows的主机貌似没有 ssh 功能... 后来实在忍 ...
- 【初探Spring】------Spring IOC(一)
IOC:Inversion of Control(控制反转).IOC它所体现的并不是一种技术,而是一种思想,一种将设计好的对象交给容器来管理的思想.IOC的核心思想就体现在控制.反转这两个词上面,要理 ...
- SQL Server数据库损坏、检测以及简单的修复办法
简介 在一个理想的世界中,不会存在任何数据库的损坏,就像我们不会将一些严重意外情况列入我们生活中的日常一样,而一旦这类事情发生,一定会对我们的生活造成非常显著的影响,在SQL Server中也 ...
- Android Secret Code
我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系 ...
- ABP框架 - 实体
文档目录 本节内容: 实体类 聚合根类 领域事件 约定的接口 审计 软删除 活跃/消极 实体 实体变化事件 IEntity 接口 实体是DDD一个核心的概念.Eric Evans是这么描述的:“一个对 ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- FastCgi与PHP-fpm之间的关系
web server(比如说nginx)只是内容的分发者.比如,如果请求/index.html,那么web server会去文件系统中找到这个文件,发送给浏览器,这里分发的是静态数据.好了,如果现在请 ...
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...