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&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; } }
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
小鱿鱼 2020-06-26
qingjiuquan 2020-06-07
zhangdy0 2020-05-31
HappyHeng 2020-05-16
whbing 2020-04-11
小鱿鱼 2020-03-20
mendeliangyang 2020-02-20
方志朋 2020-02-14
牧场SZShepherd 2020-02-01
MicroBoy 2020-01-23
牧场SZShepherd 2020-01-12
melonjj 2020-01-05
melonjj 2020-01-05
玫瑰小妖 2020-07-18
dlutbob 2020-07-04