/

Eclipse Maven Spring SpringMVC Mybatis 整合

新项目自己撘框架,想着用点新的。看慕课网 Java 高并发秒杀 API 的系列课程时很受益。所以想着仿着来使用:Mavan-Spring-SpringMVC-Mybatis 的架构。框架整合的代码我已上传到我的 Github:maven-mybatis-spring-springmvc

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

Maven Install

2016-09-10 更新:较新版 Eclipse 都有集成 Maven,所以并不需要安装

  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

2016-09-10 更新:较新版 Eclipse 都有集成 Maven,所以并不需要安装

  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/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


<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>









logback.xml Config

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

<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>

<setting name="useGeneratedKeys" value="true" />

<setting name="useColumnLabel" value="true" />

<setting name="mapUnderscoreToCamelCase" value="true" />
settings>
configuration>

Spring Config

Github-maven-mybatis-spring-springmvc spring

Spring-DAO Config


<context:property-placeholder location="classpath:jdbc.properties" />



<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}" />


<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />

<property name="autoCommitOnClose" value="false" />

<property name="checkoutTimeout" value="1000" />

<property name="acquireRetryAttempts" value="2" />
bean>




<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="configLocation" value="classpath:mybatis-config.xml" />

<property name="typeAliasesPackage" value="com.moma.dmv.entity" />

<property name="mapperLocations" value="classpath:mapper/*.xml" />
bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

<property name="basePackage" value="com.moma.dmv.dao" />
bean>

Spring-Service Config


<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" />


Spring-Web Config


<mvc:annotation-driven />


<mvc:default-servlet-handler />


<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<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/**" />


<context:component-scan base-package="com.moma.dmv.web" />

DAO Mapper Example



"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moma.dmv.dao.InfoDao">

<select id="queryById" resultType="Info" parameterType="long">

select id,`key`,`value` from info where id = #{id}
]]>
select>


<select id="queryAll" resultType="Info">

select id,key,value
from info
limit #{offset},#{limit}
]]>
select>


mapper>

web.xml Config

<servlet>
<servlet-name>dmv-dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/spring-*.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>dmv-dispatcherservlet-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());
}
}

Reference: