java web框架 spring+spring mvc+mybatis/hibernate项目总结(1)

本人Android移动开发4年经验,因为最近一段时间想着换工作,但发现移动开发现如今的大环境不是太好,于是想着转其他开发岗位,由于自己以前学过后台开发,也曾经在公司维护过一段时间的后台代码,现如今想转回java web开发于是写了下面的文章将已经java web的项目复习一下希望能对找java开发的工作有所帮助。

为了能快速的将现阶段热门的java框架上手买了一本Spring+Mybatis企业级应用实战。我将此项目分成四个分支提交到了我的github上,

四个分支分别为:

(1)master:原项目,spring+mybatis+mysql注解方式

(2)hrm_xml:spring+mybatis+mysql xml配置方式

(3)hrm_oracle:spring+mybatis+oracle xml配置方式

(4)hrm_oracle:spring+hibernate+oracle 注解方式

github地址:https://github.com/jessear/hrm

下面我将对四个分支逐一来进行讲解


spring+mybatis+mysql注解方式:

目录结构:



sql语句:
/*
Navicat MySQL Data Transfer
Source Server         : localhost_3306
Source Server Version : 50717
Source Host           : localhost:3306
Source Database       : hrm_db
Target Server Type    : MYSQL
Target Server Version : 50717
File Encoding         : 65001
Date: 2017-06-21 19:38:11
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for dept_inf
-- ----------------------------
DROP TABLE IF EXISTS `dept_inf`;
CREATE TABLE `dept_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `remark` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of dept_inf
-- ----------------------------
INSERT INTO `dept_inf` VALUES ('1', '技术部', '技术部');
INSERT INTO `dept_inf` VALUES ('2', '运营部', '运营部');
INSERT INTO `dept_inf` VALUES ('3', '财务部', '财务部');
INSERT INTO `dept_inf` VALUES ('4', '总办公', '总办公');
INSERT INTO `dept_inf` VALUES ('5', '市场部', '市场部');
INSERT INTO `dept_inf` VALUES ('6', '教学部', '教学部');
-- ----------------------------
-- Table structure for document_inf
-- ----------------------------
DROP TABLE IF EXISTS `document_inf`;
CREATE TABLE `document_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `filename` varchar(300) NOT NULL,
  `remark` varchar(300) DEFAULT NULL,
  `create_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `document_inf_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_inf` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of document_inf
-- ----------------------------
-- ----------------------------
-- Table structure for employee_inf
-- ----------------------------
DROP TABLE IF EXISTS `employee_inf`;
CREATE TABLE `employee_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_id` int(11) NOT NULL,
  `job_id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `card_id` varchar(20) NOT NULL,
  `address` varchar(50) DEFAULT NULL,
  `post_code` varchar(50) DEFAULT NULL,
  `tel` varchar(16) DEFAULT NULL,
  `phone` varchar(11) NOT NULL,
  `qq_num` varchar(10) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `sex` int(11) DEFAULT '1',
  `party` varchar(10) DEFAULT NULL,
  `birthday` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `race` varchar(100) DEFAULT NULL,
  `education` varchar(10) DEFAULT NULL,
  `speciality` varchar(20) DEFAULT NULL,
  `hobby` varchar(100) DEFAULT NULL,
  `remark` varchar(500) DEFAULT NULL,
  `create_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `dept_id` (`dept_id`),
  KEY `job_id` (`job_id`),
  CONSTRAINT `employee_inf_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept_inf` (`id`),
  CONSTRAINT `employee_inf_ibfk_2` FOREIGN KEY (`job_id`) REFERENCES `job_inf` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of employee_inf
-- ----------------------------
INSERT INTO `employee_inf` VALUES ('1', '1', '8', '爱丽丝', '4328011988', '广州天河', '510000', '020-77777777', '13262623319', '664249862', '13262623319@163.com', '0', '党员', '2017-06-21 15:45:19', '满', '本科', '美声', '唱歌', '四大天王', '2017-06-21 15:45:16');
-- ----------------------------
-- Table structure for job_inf
-- ----------------------------
DROP TABLE IF EXISTS `job_inf`;
CREATE TABLE `job_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `remark` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of job_inf
-- ----------------------------
INSERT INTO `job_inf` VALUES ('1', '职员', '职员');
INSERT INTO `job_inf` VALUES ('2', 'java开发工程师', 'java开发工程师');
INSERT INTO `job_inf` VALUES ('3', 'java中级开发工程师', 'java中级开发工程师');
INSERT INTO `job_inf` VALUES ('4', 'java高级开发工程师', 'java高级开发工程师');
INSERT INTO `job_inf` VALUES ('5', '系统管理员', '系统管理员');
INSERT INTO `job_inf` VALUES ('6', '架构师', '架构师');
INSERT INTO `job_inf` VALUES ('7', '主管', '主管');
INSERT INTO `job_inf` VALUES ('8', '经理', '经理');
INSERT INTO `job_inf` VALUES ('9', '总经理', '总经理');
-- ----------------------------
-- Table structure for notice_inf
-- ----------------------------
DROP TABLE IF EXISTS `notice_inf`;
CREATE TABLE `notice_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `content` text NOT NULL,
  `create_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `notice_inf_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_inf` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of notice_inf
-- ----------------------------
-- ----------------------------
-- Table structure for user_inf
-- ----------------------------
DROP TABLE IF EXISTS `user_inf`;
CREATE TABLE `user_inf` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `loginname` varchar(20) NOT NULL,
  `password` varchar(16) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  `create_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  `username` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_inf
-- ----------------------------
INSERT INTO `user_inf` VALUES ('1', 'admin', '123456', '2', '2017-06-21 15:55:22', '超级管理员');

lib jar包:




web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
         xmlns=" http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <!-- 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml作为配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- contextConfigLocation参数用来指定Spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <!-- 定义Spring MVC的前端控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 让Spring MVC的前端控制器拦截所有请求 -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 编码过滤器 -->
    <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>
    <!-- jsp的配置 -->
    <jsp-config>
        <jsp-property-group>
            <!-- 配置拦截所有的jsp页面  -->
            <url-pattern>*.jsp</url-pattern>
            <!-- 可以使用el表达式  -->
            <el-ignored>false</el-ignored>
            <!-- 不能在页面使用java脚本 -->
            <scripting-invalid>true</scripting-invalid>
            <!-- 给所有的jsp页面导入要依赖的库,tablib.jsp就是一个全局的标签库文件  -->
            <include-prelude>/WEB-INF/jsp/taglib.jsp</include-prelude>
        </jsp-property-group>
    </jsp-config>
    <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
    </error-page>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans"
 xmlns:mybatis=" http://mybatis.org/schema/mybatis-spring"
 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"
 xmlns:mvc=" http://www.springframework.org/schema/mvc"
 xmlns:tx=" http://www.springframework.org/schema/tx"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-4.2.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
               http://mybatis.org/schema/mybatis-spring
               http://mybatis.org/schema/mybatis-spring.xsd ">
        
  <!-- mybatis:scan会扫描com.jesse.dao包里的所有接口当作Spring的bean配置,之后可以进行依赖注入-->
    <mybatis:scan base-package="com.jesse.dao"/>
      
  <!-- 扫描com.jesse包下面的java文件,有Spring的相关注解的类,则把这些类注册为Spring的bean -->
    <context:component-scan base-package="com.jesse"/>
   
 <!-- 使用PropertyOverrideConfigurer后处理器加载数据源参数 -->
 <context:property-override location="classpath:db.properties"/>
 <!-- 配置c3p0数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>
 
 <!-- 配置SqlSessionFactory,org.mybatis.spring.SqlSessionFactoryBean是Mybatis社区开发用于整合Spring的bean -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
     p:dataSource-ref="dataSource"/>
 <!-- spring与mybatis整合配置,扫描所有dao -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.jesse.dao"
    p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
 
 <!-- JDBC事务管理器 -->
 <bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
 
 <!-- 启用支持annotation注解方式事务管理 -->
 <tx:annotation-driven transaction-manager="transactionManager"/>
 
</beans>

springmvc-config.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:mvc=" http://www.springframework.org/schema/mvc"
    xmlns:context=" http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd    
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
       
    <!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller -->
    <context:component-scan base-package="com.jesse.controller"/>
    <!-- 设置默认配置方案 -->
    <mvc:annotation-driven/>
    <!-- 使用默认的Servlet来响应静态文件 -->
    <mvc:default-servlet-handler/>
   
    <!-- 定义Spring MVC的拦截器 -->
    <mvc:interceptors>
     <mvc:interceptor>
      <!-- 拦截所有请求 -->
      <mvc:mapping path="/*"/>
      <!-- 自定义判断用户权限的拦截类 --> 
       <bean class="com.jesse.interceptor.AuthorizedInterceptor"/>
     </mvc:interceptor>
    </mvc:interceptors>
   
   
    <!-- 视图解析器  -->
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <!-- 后缀 -->
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
   
     <bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
  <!-- 上传文件大小上限,单位为字节(10MB) -->
        <property name="maxUploadSize"> 
            <value>10485760</value> 
        </property> 
        <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
        <property name="defaultEncoding">
         <value>UTF-8</value>
        </property>
    </bean>
   
</beans>


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值