基于Spring + Spring MVC + Mybatis 高性能web构建实例详解

网友投稿 226 2023-05-23


基于Spring + Spring MVC + Mybatis 高性能web构建实例详解

一直想写这篇文章,前段时间痴迷于javascript、Nodejs、AngularJS,做了大量的研究,对前后端交互有了更深层次的认识。

今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详细的配置,详细的注释,看起来应该很容易懂。

用最合适的技术去实现,并不断追求最佳实践。这就是架构之道。

希望这篇文章能给你们带来一些帮助,同时希望你们可以为这个项目贡献你的想法。

源码地址:https://github.com/Eliteams/quick4j 点击打开

源码地址:https://github.com/Eliteams/quick4j 点击打开

源码地址:https://github.com/Eliteams/quick4j 点击打开

看我们的项目结构:

是一个典型的Maven 项目 :

src/main/java:存放java源文件

src/main/resources:存放程序资源、配置文件

src/test/java:存放测试代码文件

src/main/webapp:web根目录

pom.xml : maven项目配置文件,管理依赖,编译,打包

主要的后端架构:spring + Spring MVC + Mybatis + Apache Shiro

前端界面主要使用MetroNic 模板,

先看我们搭建完成,跑起来的效果,这样你才有兴趣看下去:

你可以 在github 上 checkout quick4j项目 查看 ,并跟下面步骤 来搭建:

强烈建议你,checkout  https://github.com/Eliteams/quick4j ,在本地跑起来,再试着自己搭建框架

1、首先创建 maven 项目 ,用 idea 、eclipse 或 mvn 命令行都行

2、配置 pom.xml ,添加框架依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.eliteams

quick4j

war

1.0.0

quick4j App

https://github.com/starzou/quick4j

quick4j

org.mybatis.generator

mybatis-generator-maven-plugin

${plugin.mybatis.generator}

${mybatis.generator.generatorConfig.xml}

true

true

org.apache.maven.plugins

maven-compiler-plugin

${plugin.maven-compiler}

${project.build.jdk}

${project.build.jdk}

${project.build.sourceEncoding}

src/main/resources

**/*.properties

**/*.xml

true

src/main/java

**/*.properties

**/*.xml

true

UTF-8

zh_CN

1.7

${basedir}/src/test/resources/generatorConfig.xml

file:///${basedir}/src/test/resources/generatorConfig.properties

1.3.1

3.1

4.11

4.0.2.RELEASE

3.2.2

1.2.2

5.1.30

9.1-901.jdbc4

1.6.6

1.2.12

4.1.2

1.9.13

0.9.1.2

1.0.5

7.0.53

1.2

1.0

3.1

1.2.3

1.3.1

1.9

3.3

1.6.12

4.0.18.Final

5.1.1.Final

junit

junit

${junit.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-oxm

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

${mybatis.spring.version}

mysql

mysql-connector-java

${mysql.connector.version}

com.alibaba

druid

${druid.version}

org.codehaus.jackson

jackson-mapper-asl

${jackson.version}

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

javax.servlet

javax.servlet-api

3.0.1

provided

javax.servlet

jstl

${jstl.version}

commons-fileupload

commons-fileupload

${commons.fileupload.version}

org.apache.httpcomponents

httpclient

${httpclient.version}

commons-codec

commons-codec

${commons.codec.version}

commons-net

commons-net

${commons.net.version}

commons-logging

commons-logging

1.1.3

commons-collections

commons-collections

3.2.1

com.google.collections

google-collections

${google.collections.version}

cglib

cglib-nodep

${cglib.version}

org.apache.shiro

shiro-spring

${shiro.version}

org.apache.shiro

shiro-ehcache

${shiro.version}

org.apache.shiro

shiro-core

${shiro.version}

org.apache.shiro

shiro-web

${shiro.version}

org.apache.shiro

shiro-quartz

${shiro.version}

org.aspectj

aspectjweaver

${aspectj.version}

org.aspectj

aspectjrt

${aspectj.version}

org.hibernate

hibernate-validator

${hibernate.validator.version}

io.netty

netty-all

${netty.version}

org.mybatis.generator

mybatis-generator-core

1.3.2

jar

test

3、配置web.xml

web.xml是一个项目的核心,看看它的一些配置:

配置 ContextLoaderListener 监听器

配置Spring字符编码过滤器

配置shiro 安全过滤器

配置Spring MVC 核心控制器 DispatcherServlet

配置一些页面

spring 和 apache shiro 是由一个 ContextLoaderListener 监听器 加载的配置文件,并初始化

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

contextConfigLocation

classpath*:applicationContext.xml

classpath*:applicationContext-shiro.xml

org.springframework.web.context.ContextLoaderListener

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

shiroFilter

org.springframework.web.filter.DelegatingFilterProxy

true

targetFilterLifecycle

true

shiroFilter

/*

log4jConfigLocation

classpath:log4j.properties

log4jRefreshInterval

60000

org.springframework.web.util.Log4jConfigListener

dispatcher

org.springframework.web.servlet.DispPEXcJHYatcherServlet

contextConfigLocation

classpath*:spring-mvc.xml

1

dispatcher

/rest/*

rest/index

404

/rest/page/404

500

/rest/page/500

org.apache.shiro.authz.AuthorizationException

/rest/page/401

4、spring配置:

applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p"

xmlns:util="http://springframework.org/schema/util" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc/spring-jdbc.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd">

classpath*:application.properties

p:configLocation="classpath:mybatis-config.xml"

p:mapperLocations="classpath:com/eliteams/quick4j/web/dao/*.xml"/>

p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

p:dataSource-ref="dataSource"/>

p:configLocation="classpath:ehcache.xml"/>

p:cacheManager-ref="ehCacheManagerFactory"/>

application.properties

##JDBC Global Setting

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/quick4j?useUnicode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=admin123

##DataSource Global Setting

#配置初始化大小、最小、最大

ds.initialSize=1

ds.minIdle=1

ds.maxActive=20

#配置获取连接等待超时的时间

ds.maxWait=60000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

ds.timeBetweenEvictionRunsMillis=60000

#配置一个连接在池中最小生存的时间,单位是毫秒

ds.minEvictableIdleTimeMillis=300000

ehcache.xml

overflowToDisk="true" maxEntriesLocalDisk="100000"/>

5、Apache Shiro 配置 : 要配置realms bean

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd">

apache shiro配置

/app/** = anon

/assets/** = anon

/rest/user/login = anon

/** = authc

ehcache-shiro.xml

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

overflowToDisk="false"

diskPersistent="false"

diskExpiryThreadIntervalSeconds="120"

/>

6、MyBatis 配置

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

7、Spring MVC 配置

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

classpath:messages

classpath:org/hibernate/validator/ValidationMessages

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

text/plain;charset=UTF-8

application/json;charset=UTF-8

p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>

messages.properties : hibernate-validator 配置文件,国际化资源文件

#user

user.username.null=用户名不能为空

user.password.null=密码不能为空

log4j.properties :

[plain] view plain copy print?在CODE上查看代码片派生到我的代码片

# DEBUG,INFO,WARN,ERROR,FATAL

LOG_LEVEL=INFO

log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.Encoding=utf-8

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

#log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n

log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

log4j.appender.FILE.File=${catalina.base}/logs/quick4j.log

log4j.appender.FILE.Encoding=utf-8

log4j.appender.FILE.DatePattern='.'yyyy-MM-dd

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

#log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout

log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n

quick4j.sql

/*

SQLyog 企业版 - MySQL GUI v8.14

MySQL - 5.5.27 : Database - quick4j

*********************************************************************

*/

/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

CREATE DATABASE /*!32312 IF NOT EXISTS*/`quick4j` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `quick4j`;

/*Table structure for table `permission` */

DROP TABLE IF EXISTS `permission`;

CREATE TABLE `permission` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '权限id',

`permission_name` varchar(32) DEFAULT NULL COMMENT '权限名',

`permission_sign` varchar(128) DEFAULT NULL COMMENT '权限标识,程序中判断使用,如"user:create"',

`description` varchar(256) DEFAULT NULL COMMENT '权限描述,UI界面显示使用',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='权限表';

/*Data for the table `permission` */

insert into `permission`(`id`,`permission_name`,`permission_sign`,`description`) values (1,'用户新增','user:create',NULL);

/*Table structure for table `role` */

DROP TABLE IF EXISTS `role`;

CREATE TABLE `role` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '角色id',

`role_name` varchar(32) DEFAULT NULL COMMENT '角色名',

`role_sign` varchar(128) DEFAULT NULL COMMENT '角色标识,程序中判断使用,如"admin"',

`description` varchar(256) DEFAULT NULL COMMENT '角色描述,UI界面显示使用',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='角色表';

/*Data for the table `role` */

insert into `role`(`id`,`role_name`,`role_sign`,`description`) values (1,'admin','admin','管理员');

/*Table structure for table `role_permission` */

DROP TABLE IF EXISTS `role_permission`;

CREATE TABLE `role_permission` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表id',

`role_id` bigint(20) unsigned DEFAULT NULL COMMENT '角色id',

`permission_id` bigint(20) unsigned DEFAULT NULL COMMENT '权限id',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='角色与权限关联表';

/*Data for the table `role_permission` */

insert into `role_permission`(`id`,`role_id`,`permission_id`) values (1,2,1);

/*Table structure for table `user` */

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id',

`username` varchar(50) DEFAULT NULL COMMENT '用户名',

`password` char(64) DEFAULT NULL COMMENT '密码',

`state` varchar(32) DEFAULT NULL COMMENT '状态',

`create_time` datetime DEFAULT NULL COMMENT '创建时间',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='用户表';

/*Data for the table `user` */

insert into `user`(`id`,`username`,`password`,`state`,`create_time`) values (1,'starzou','8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',NULL,'2014-07-17 12:59:08');

/*Table structure for table `user_role` */

DROP TABLE IF EXISTS `user_role`;

CREATE TABLE `user_role` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表id',

`user_id` bigint(20) unsigned DEFAULT NULL COMMENT '用户id',

`role_id` bigint(20) unsigned DEFAULT NULL COMMENT '角色id',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='用户与角色关联表';

/*Data for the table `user_role` */

insert into `user_role`(`id`,`user_id`,`role_id`) values (1,1,1);

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;

/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

以上所述是给大家介绍的基于Spring + Spring MVC + Mybatis 高性能web构建实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中IO流详解
下一篇:java SpringMVC学习使用详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~