首页 > 代码库 > 基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目【转】
基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目【转】
摘要: 对于现在主流的j2ee企业级开发而言,ssh(struts+hibernate+spring)依然是一个事实的标准。由struts充当的mvc调度控制;hibernate的orm持久化映射;spring的ioc和aop的容器环境近乎于完美的框架组合。 但是,在实际的开发工作中,由于程序猿对于技术、以及更加快速的解决方案的追求,我们会越来越发现ssh框架所存在的诸多问题和困扰。
基于REST风格的Spring3 MVC资源映射编程模型,编写的Code真的很优雅。那是相当的惊喜,编程之美。
MyEclipse项目清单
清单1. web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>mySpring3 and myBatis3 Project</display-name> <!-- 配置文件位置,默认为/WEB-INF/applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 字符集过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 上下文Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- servlet控制跳转 --> <servlet> <servlet-name>spring3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context-dispatcher.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 激活静态资源的默认配置,解决Rest风格兼容 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
清单2. 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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-init-method="init"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!--创建jdbc数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="http://www.mamicode.com/${driver}" /> <property name="url" value="http://www.mamicode.com/${url}" /> <property name="username" value="http://www.mamicode.com/${username}" /> <property name="password" value="http://www.mamicode.com/${password}" /> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis.cfg.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <!-- 可通过注解控制事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置事务的传播特性 --> <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> --> <!-- 配置事务的切入点 --> <!-- <aop:config> <aop:pointcut id="targetMethod" expression="execution(* com.matol.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethod" /> </aop:config> --> <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="http://www.mamicode.com/com.matol.mapper" /> </bean> <!-- 负责注册JSR-250 的注释生效 @Resource MapperScannerConfigurer配置会自动启用mapper注解,可省略当前配置 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> --> </beans>
清单3. context-dispatcher.xml
<?xml version="1.0" encoding="UTF-8"?><beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <mvc:annotation-driven /> <!-- annotation默认的方法映射适配器 mvc:annotation-driven注册后可以省略当前配置 <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> --> <!-- 探测注解的包,包括子集, 在JUnit测试的时候需要 --> <!-- 自动扫描bean,把作了注解的类转换为bean --> <context:component-scan base-package="com.matol" /> <!-- 加载组装所以配置文件 context:component-scan注册后可以省略当前配置 <context:annotation-config/> --> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 使用JSP页面进行输出 --> <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView" /> <!-- 指定了表示层的前缀 --> <!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 --> <property name="prefix" value="http://www.mamicode.com/" /> <!-- 指定了表示层的后缀 --> <property name="suffix" value="http://www.mamicode.com/.jsp"></property> </bean> <!-- 处理文件上传处理 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" /></beans>
清单4. jdbc.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/matolDBusername=rootpassword=root
清单5. mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="Student" type="com.matol.entity.User" /> </typeAliases> <mappers> <mapper resource="com/matol/mapper/UserMapper.xml" /> </mappers> </configuration>
清单6. matolDB.sql
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50141 Source Host : localhost:3306 Source Database : matoldb Target Server Type : MYSQL Target Server Version : 50141 File Encoding : 65001 Date: 2013-01-23 16:47:52 */SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for `sys_user`-- ----------------------------DROP TABLE IF EXISTS `sys_user`;CREATE TABLE `sys_user` ( `id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT, `name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `pass` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
清单7. User.java
package com.matol.entity;import java.io.Serializable;import org.springframework.stereotype.Repository;/** * * @author matol * @date 2013-01-23 16:38 */ @Repository(value = "user") public class User implements Serializable { private Integer id; private String name; private String pass; private Integer age; public User(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
清单7. UserMapper.java
package com.matol.mapper; import java.util.List; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import com.matol.entity.User;/** * * @author matol * @date 2013-01-23 16:38 */ @Repository(value = "userMapper") @Transactional public interface UserMapper { Integer create(User user); Integer delete(Integer id); Integer modify(User user); User findById(Integer id); User findByUser(User user); List<User> findAll(); List<User> findAll(User user); Integer count(); }
清单8. 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.matol.mapper.UserMapper"> <!-- 定义数据库字段与实体对象的映射关系 --> <resultMap type="com.matol.entity.User" id="resultUser"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="pass" column="pass"/> <result property="age" column="age"/> </resultMap> <!-- 定义参数模型 --> <parameterMap type="com.matol.entity.User" id="paramUser"> <parameter property="id"/> <parameter property="name"/> <parameter property="pass"/> <parameter property="age"/> </parameterMap> <!-- 定义要操纵的SQL语句 --> <insert id="create" parameterType="com.matol.entity.User" > INSERT INTO sys_user(name,pass,age) VALUES(#{name},#{pass},#{age}) </insert> <delete id="delete" parameterType="Integer" > DELETE FROM sys_user WHERE id=#{value} </delete> <update id="modify" parameterType="com.matol.entity.User" > UPDATE sys_user <set> <if test="name != null">name=#{name},</if> <if test="pass != null">pass=#{pass},</if> <if test="age != null">age=#{age},</if> </set> WHERE id=#{id} </update> <select id="findById" parameterType="Integer" resultMap="resultUser"> SELECT * FROM sys_user WHERE id=#{value} </select> <select id="findAll" resultType="list" resultMap="resultUser"> SELECT * FROM sys_user </select> <select id="count" resultType="Integer"> SELECT count(*) FROM sys_user </select></mapper>
清单9. UserService.java
package com.matol.service;import java.util.List;import org.springframework.stereotype.Repository;import com.matol.entity.User;/** * * @author matol * @date 2013-01-23 16:38 */ @Repository(value = "userService")public interface UserService { Integer create(User user); Integer delete(Integer id); Integer modify(User user); User findById(Integer id); User findByUser(User user); List<User> findAll(); List<User> findAll(User user); Integer count(); }
lib的jar清单
由于只是一个相对简单的使用范例,所以没有非常标准的注入DAO,以及DAO接口,只是通过Mapper代替过程。不过,道理都是一样,无非就是新添加一层注入而已。
核心技术:Maven,Springmvc mybatis shiro, Druid, Restful,
Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx
1. 项目核心代码结构截图
项目模块依赖
特别提醒:开发人员在开发的时候可以将自己的业务REST服务化或者Dubbo服务化
2. 项目依赖介绍
2.1 后台管理系统、Rest服务系统、Scheculer定时调度系统依赖如下图:
2.2 Dubbo独立服务项目依赖如下图:
3. 项目功能部分截图:
zookeeper、dubbo服务启动
dubbo管控台
REST服务平台
基于全注解的Spring3.1 mvc、myBatis3.1、Mysql的轻量级项目【转】