`
芝加哥09
  • 浏览: 60814 次
社区版块
存档分类
最新评论

Spring + SpringMVC + Mybatis + Maven 搭建Web工程

 
阅读更多

我个人觉得以前“如日中天”SSH框架搭建Web工程,并不是最好搭配。就拿Hibernate来说,对于很大一部分程序员根本用不好它,而且当网站越大后,Hibernate也就显得很无力。

 

运用大工厂Spring强大的注入功能和运用轻量和SQL明确的Mybatis作为持久层,同时用Maven统一协调管理好JAR包。搭建这样的Web也是非常快速与简便的。

 

现在我把重要的配置都贴出来,供大家参考。

(本例子的视图层是用JSON表示,因为该例子是用于作为Android工程API使用的,当然要换成其它视图层形式,例如JSP,也是很容易的。)

 

pom.xml

在该文件当中,我也把Jetty配置好了。

 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cloud.android.spring</groupId>
  <artifactId>android-api</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>android-api Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>  
      <artifactId>spring-webmvc</artifactId>  
      <version>3.1.1.RELEASE</version>  
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.3</version>
    </dependency>
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-lgpl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-lgpl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
  </dependencies>
<build>
    <finalName>android-api</finalName>
    <plugins>
            <plugin>   
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
         <configuration>
            <contextPath>/api</contextPath>      
            <connectors>      
                <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">  
                    <port>8080</port>      
                </connector>      
            </connectors>
            <scanIntervalSeconds>3</scanIntervalSeconds>
         </configuration>   
    </plugin>  
    </plugins>
</build>
</project>

 

 

applicationContext.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/android_data?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/cloud/android/spring/model/*Mapper.xml" />
    </bean>

    <!-- Dao -->
    <bean id="baseDao" class="com.cloud.android.spring.dao.impl.BaseDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="userDao" class="com.cloud.android.spring.dao.impl.UserDaoImpl" parent="baseDao" />

    <bean id="userService" class="com.cloud.android.spring.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao" />
    </bean>
</beans>

 

 

spring-mvc.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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">

    <mvc:annotation-driven />
    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
    <context:component-scan base-package="com.cloud.android.spring.controller" />
</beans>

 

 

web.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <display-name>android-api</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:*Context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

 

实体层代码:

User.java

 

 

package com.cloud.android.spring.model;

public class User {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(int id) {
        this.id = id;
    }

    public User(String name) {
        this.name = name;
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }
}

 

 

UserMapper.xml

 

 

<?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.cloud.android.spring.model.User">
    <resultMap id="UserMapper" type="com.cloud.android.spring.model.User">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="password" property="password"/>
    </resultMap>

    <select id="get" resultMap="UserMapper" parameterType="com.cloud.android.spring.model.User">
        SELECT * FROM user 
        <where>
            <if test="id != 0">
                id = #{id}
            </if>
            <if test="name != null">
                AND name = #{name}
            </if>
        </where>
    </select>

    <insert id="add" parameterType="com.cloud.android.spring.model.User">
        INSERT INTO user(`name`, `password`) VALUES (#{name}, #{password})
    </insert>
</mapper>

 

 

Dao层代码:

BaseDao.java

 

 

package com.cloud.android.spring.dao;

import java.util.List;

public interface BaseDao<T> {
    public T get(String classMethod, T entry);
    public List<T> getAll(String classMethod);
    public boolean add(String classMethod, T entry);
    public boolean edit(String classMethod, T entry);
    public boolean remove(String classMethod, T entry);
}

 

 

BaseDaoImpl.java

 

 

package com.cloud.android.spring.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.cloud.android.spring.dao.BaseDao;

public class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> {

    @Override
    public T get(String classMethod, T entry) {
        T result = null;
        result = this.getSqlSession().selectOne(classMethod, entry);
        return result;
    }

    @Override
    public List<T> getAll(String classMethod) {
        List<T> results = new ArrayList<T>();
        results = this.getSqlSession().selectList(classMethod);
        return results;
    }

    @Override
    public boolean add(String classMethod, T entry) {
        boolean flag = false;
        flag = this.getSqlSession().insert(classMethod, entry) > 0 ? true : false;
        return flag;
    }

    @Override
    public boolean edit(String classMethod, T entry) {
        boolean flag = false;
        flag = this.getSqlSession().update(classMethod, entry) > 0 ? true : false;
        return flag;
    }

    @Override
    public boolean remove(String classMethod, T entry) {
        boolean flag = false;
        flag = this.getSqlSession().delete(classMethod, entry) > 0 ? true : false;
        return flag;
    }
}

 

 

UserDao.java

 

 

package com.cloud.android.spring.dao;

import com.cloud.android.spring.model.User;

public interface UserDao extends BaseDao<User> {

}

 

 

UserDaoImpl.java

 

package com.cloud.android.spring.dao.impl;

import com.cloud.android.spring.dao.UserDao;
import com.cloud.android.spring.model.User;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

}

 

Service层代码:

UserService.java

 

package com.cloud.android.spring.service;

import com.cloud.android.spring.model.User;


public interface UserService {
    public User getUserById(int id);
    public User getUserByName(String name);
    public boolean addUser(String name, String password);
}

 

UserServiceImpl.java

 

package com.cloud.android.spring.service.impl;

import com.cloud.android.spring.constant.Constants;
import com.cloud.android.spring.dao.UserDao;
import com.cloud.android.spring.model.User;
import com.cloud.android.spring.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private String className = User.class.getName();

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User getUserById(int id) {
        User user = new User(id);
        return this.userDao.get(className + Constants.GET, user);
    }

    @Override
    public User getUserByName(String name) {
        User user = new User(name);
        return this.userDao.get(className + Constants.GET, user);
    }

    @Override
    public boolean addUser(String name, String password) {
        boolean flag = false;
        User user = new User(name, password);
        flag = this.userDao.add(className + Constants.ADD, user);
        return flag;
    }

    
}

 

Controller层代码:

UserController.java

 

package com.cloud.android.spring.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.android.spring.constant.Constants;
import com.cloud.android.spring.model.Message;
import com.cloud.android.spring.model.User;
import com.cloud.android.spring.service.UserService;
import com.cloud.android.spring.util.StringUtil;

@Controller
@RequestMapping(value = "/user", method = RequestMethod.GET)
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ResponseBody
    public Object getUser(String id, String name, HttpServletRequest request, HttpServletResponse response) {
        User user = new User();
        if (StringUtil.isEmpty(id)) {
            if (StringUtil.isEmpty(name)) {
                return Message.noContent(null);
            }
            user = userService.getUserByName(name.trim());
        } else {
            try {
                int idInt = Integer.parseInt(id);
                user = userService.getUserById(idInt);
            } catch (Exception e) {
                return Message.error(Constants.INCORRECT_PARAMETER);
            }
        }
        if (null == user) {
            return Message.noContent(user);
        }
        return Message.ok(user);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public Object addUser(String name, String password, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtil.isEmpty(name) || StringUtil.isEmpty(password)) {
            return Message.error(Constants.INCORRECT_PARAMETER);
        }
        boolean status = userService.addUser(name, password);
        if (status) {
            return Message.ok(Constants.SUCCESS);
        }
        return Message.noContent(Constants.FAILED);
    }
}

 

常量类:

Constants.java

 

package com.cloud.android.spring.constant;

public class Constants {
    // The parameters
    public static String ID = "id";
    public static String NAME = "name";

    // String value
    public static String EMPTY = "";
    public static String INCORRECT_PARAMETER = "Incorrect parameter.";

    // Sql flag
    public static String ADD = ".add";
    public static String EDIT = ".edit";
    public static String REMOVE = ".remove";
    public static String GET = ".get";
    public static String GET_ALL = ".getAll";

    // Operate status
    public static String SUCCESS = "success";
    public static String FAILED = "failed";
}

 

工具类:

StringUtil.java

 

package com.cloud.android.spring.util;

import com.cloud.android.spring.constant.Constants;

public class StringUtil {

    public static boolean isEmpty(String str) {
        if (null == str || Constants.EMPTY.equals(str)) {
            return true;
        }
        return false;
    }
}

 

还有一个用于包装JSON的类:

Message.java

 

package com.cloud.android.spring.model;

import java.util.HashMap;
import java.util.Map;

public class Message {
    private static String DATA = "data";
    private static String STATUS = "status";

    public static Object ok(Object obj) {
        Object result = dealWithMessage(obj, 200);
        return result;
    }

    public static Object error(Object obj) {
        Object result = dealWithMessage(obj, 500);
        return result;
    }

    public static Object noContent(Object obj) {
        Object result = dealWithMessage(obj, 204);
        return result;
    }

    private static Object dealWithMessage(Object obj, int status) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(DATA, obj);
        result.put(STATUS, status);
        return result;
    }
}

 

 

 

 

分享到:
评论

相关推荐

    Spring+SpringMVC+Mybatis+Velocity+Maven demo

    Spring、SpringMVC、Mybatis、Velocity和Maven是Java Web开发中常用的一组技术栈,它们各自在软件开发的不同层面发挥着重要作用。这个压缩包文件的标题和描述表明,它提供了一个集成这些技术的演示项目,下面我们将...

    SSM框架(IDEA+Spring+SpringMVC+Maven+Mybatis+MySQL).zip

    SSM框架是Java Web开发中常用的三大框架——Spring、SpringMVC和Mybatis的组合,它们协同工作,构建高效、灵活的Web应用。IDEA作为Java开发的主流集成开发环境,Maven则作为项目管理和构建工具,而MySQL是常见的关系...

    SpringMvc+Spring+Mybatis+Maven整合.part10

    通过SpringMvc+Spring+Mybatis+Maven整合,学习用maven搭建框架

    SpringMvc+Spring+Mybatis+Maven+注解方式=整合

    "SpringMvc+Spring+Mybatis+Maven+注解方式"是一个经典的Java后端技术栈,它整合了四个关键组件,为开发人员提供了强大的工具和框架支持。下面将详细讲解这四个组件及其整合方式。 1. **Spring Framework**: ...

    springMVC+Spring+Mybatis+Maven整合代码案例

    4、搭建spring和mybatis的配置:spring-mybatis.xm(扫描dao)) 5、测试Spring+mybatis的框架搭建,写单元测试JUnit,测试事务配置等:model--&gt;dao(mapper)--&gt;service--&gt;test 6、映入SpringMVC:配置...

    Spring+SpringMVC+Mybatis+Maven+bootstrap+ajax+jQuery整合开发简单的员工后台管理系统

    在本项目中,"Spring+SpringMVC+Mybatis+Maven+bootstrap+ajax+jQuery整合开发简单的员工后台管理系统",我们看到一个基于Java技术栈的Web应用开发实例。这个系统利用了多个核心技术来构建一个功能完备的员工管理...

    maven+springmvc+redis+mybatis整合

    本项目以“maven+springmvc+redis+mybatis整合”为主题,旨在提供一个基于这些技术的集成框架,特别强调了利用Redis作为缓存来提升应用性能。下面将详细阐述这个框架中的各个组成部分以及它们之间的协作。 首先,...

    基于IDEA+Spring+SpringMVC+Mybatis+Redis+Shiro+Maven实现的教务管理系统+源码

    基于IDEA+Spring+SpringMVC+Mybatis+Redis+Shiro+Maven实现的教务管理系统+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 基于IDEA+Spring+SpringMVC+...

    整合Spring+SpringMVC+Mybatis+Maven+Mysql项目实例

    整合搭建Spring+SpringMVC+Mybatis+Maven+Mysql开发实例

    maven+spring+springMVC+mybatis

    maven+spring+springMVC+mybatis 框架搭建 Maven 是一个优秀的项目管理和构建工具,Spring 是一个广泛使用的 Java 框架,SpringMVC 是基于 Spring 的一个 Web 框架,MyBatis 是一个持久层框架。在这个项目中,我们...

    基于SpringMVC+Spring+MyBatis+Maven项目案例.zip

    基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis...

    SSM框架(Spring+SpringMVC+MyBatis)+shiro+Maven.zip

    ● 实现方法: 使用MySQL 建立数据库。服务端使用SSM框架(Spring+SpringMVC+MyBatis)+shiro+Maven,使用Myeclipse进行开发。前端使用MUi和HUI框架和vue(与后台交互模板)和Html5+css3来实现移动端App的开发。

    spring+springmvc+mybatis+mysql+maven+eclipse 工程实例

    这是一个基于Java技术栈的Web应用开发实例,涵盖了Spring、SpringMVC、MyBatis、MySQL数据库和Maven构建工具,以及Eclipse IDE的使用。这个工程实例为初学者提供了一个全面的学习平台,让我们逐一深入探讨这些关键...

    Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven实现的通用权限管理系统

    Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...

    基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip

    基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+...

    spring+springMVC+myBatis+maven项目整合

    【标题】"spring+springMVC+myBatis+maven项目整合"是一个常见的Java Web开发技术栈,这个项目集成了Spring框架(用于依赖注入和管理)、SpringMVC(用于处理HTTP请求和视图渲染)、MyBatis(作为持久层框架)以及...

    使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis

    【标题】:“使用IntelliJ IDEA搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)项目” 【正文】 在Java开发领域,Spring、SpringMVC和MyBatis框架的组合,简称SSM,是构建Web应用程序的常用技术栈。IntelliJ IDEA作为...

    spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven 项目框架搭建 1.1版本

    本项目框架搭建基于"spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven",这是一个经典且广泛使用的Java开发配置。下面将详细阐述这些组件的功能、相互关系以及如何进行集成。 1. **Spring 5.x**: Spring是...

    Spring+springMVC+Mybatis+Maven项目

    【Spring+SpringMVC+Mybatis+Maven项目】是一个典型的Java Web开发框架组合,用于构建高效、可扩展的企业级应用程序。在这个项目中,Spring作为核心容器管理组件,SpringMVC处理HTTP请求和响应,Mybatis则负责数据库...

Global site tag (gtag.js) - Google Analytics