前面我们学习了如何使用maven,今天我们就使用maven来搭建一个基础的开发环境:根据项目模块进行拆分与依赖、根据maven整合ssm。
为了在项目开发时更加简洁与快速,我们会对整个项目进行拆分,拆分的方式有两种:横线拆分与纵向拆分,横向拆分是指根据功能将整个项目拆分成可独立运行的功能模块(web项目),但是这样一来各个功能模块之间想要进行调用就会比较麻烦,我们指定web项目之间是没法进行依赖的,所以还需要使用webservice进行web模块间通信;纵向拆分就简单很多了,就是将之前项目工程中的分包变为分模块。
我们的项目怎么拆分呢?横向纵向一起使用来进行拆分,将整个众筹系统分为前台(面向用户)与后台(面向管理员)两个单独的功能模块(横向),然后在这个单独的功能模块中对分包操作进行纵向拆分
好了 不多说了,我们开始构建环境,首先在很多公司中都会一个父工程(pom形式),这个工程是公司长时间积累下来的 ,作用是让别的工程(即整个公司的项目)进行继承,别的工程的所有jar的版本都是父工程来统一,所以我们先来创建一个这样父工程;
首先创建一个我们众筹项目的工作集,然后新建一个maven项目
这个父工程的pom.xml文件中会有很多很多的依赖,这样便于使用和整体的版本控制;
4.0.0
com.gome
project-parent
0.0.1-SNAPSHOT
pom
2.5
3.6
1.10
1.9.3
3.2.2
3.6.1
1.3.2
1.4
4.12
2.9.9
4.5.3
3.16
2.2.3
1.1.0
5.1.42
4.3.8.RELEASE
3.3.1
1.3.1
5.0.3
2.7.4
1.3.5
1.2.17
1.7.6
2.5
2.2
1.2
5.22.0
5.22.0
commons-io
commons-io
${commons-io.version}
org.apache.commons
commons-lang3
${commons-lang3.version}
commons-codec
commons-codec
${commons-codec.version}
commons-beanutils
commons-beanutils
${commons-beanutils.version}
commons-collections
commons-collections
${commons-collections.version}
org.apache.commons
commons-math3
${commons-math3.version}
commons-fileupload
commons-fileupload
${commons.fileupload}
org.apache.commons
commons-email
${commons-email.version}
junit
junit
${junit.version}
test
joda-time
joda-time
${joda-time.version}
org.apache.httpcomponents
httpclient
${httpclient.version}
org.apache.poi
poi
${poi.version}
org.quartz-scheduler
quartz
${quartz.version}
com.alibaba
druid
${druid.version}
mysql
mysql-connector-java
${mysql.connector}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-webmvc-portlet
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.springframework
spring-context-support
${spring.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
org.mybatis
mybatis
${mybatis.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
org.mybatis.generator
mybatis-generator-core
${mbg.version}
org.activiti
activiti-engine
${activiti.version}
org.activiti
activiti-spring
${activiti.spring.version}
log4j
log4j
${log4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.slf4j
slf4j-api
${slf4j.version}
javax.servlet.jsp
jsp-api
${jsp-api.version}
provided
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jstl
${jstl.version}
公司中除了有一个公共的父工程外,可能还会有一个公共的依赖库(jar包的形式),其中包含很多工具类供大家来使用 ,别的项目想要使用,只需要添加依赖这个jar包就可以了;
我们顺便来创建一个工具类,供我们测试时使用;
这个公共依赖库很可能继承与公共的父工程,为了达到版本控制的效果,所以他的pom.xml文件中的依赖都可能来自于 公共父工程中的pom.xml文件中,因为是继承来的,所以不需要填写版本号,只需要说明id即可;
4.0.0
com.gome
project-parent
0.0.1-SNAPSHOT
com.gome
project-commons
0.0.1-SNAPSHOT
commons-io
commons-io
org.apache.commons
commons-lang3
commons-codec
commons-codec
commons-beanutils
commons-beanutils
commons-collections
commons-collections
org.apache.commons
commons-math3
commons-fileupload
commons-fileupload
org.apache.commons
commons-email
到这里,我们已经创建了(很多公司都已经提供了)一个公共的父工程和一个公共依赖库,现在我们来创建我们众筹项目的工程;
因为整个众筹项目我们拆分出来两个功能模块,一个前台模块(面向用户),一个后台模块(面向管理员),所以我们的整个众筹项目应该是一个父工程,这个父工程中有两个能够独立运行的子模块,并且这个父工程继承与公司的公共父工程,先来创建这个众筹项目的父工程;
再来创建其中的manager模块(后台模块),manager模块是一个可以独立运行的子模块,是不是觉得它应该是一个war包呢?但是因为我们还需要将manager拆分出几个小模块,所以它也只能是个pom啦。。。
好,只后我们一次为manager添加pojo模块(jar包供别人使用)、dao模块(jar包供别人使用)、service模块(jar包供别人使用)与web模块(war包可以运行的web工程);
pojo
dao
service
web
为web模块添加web.xml文件
先取消勾选、应用、再勾选、
就会出现下图
修改生成web.xml的路径,点击ok
这样我们的manager模块就聚合了pojo、dao、service、web这四个模块;然后我们为这四个模块添加依赖关系,依赖关系为web依赖service、service依赖dao、dao依赖pojo;
为web模块添加springMVC,做一个简单的测试;
导包
4.0.0
com.gome.scw
scw-manager
0.0.1-SNAPSHOT
manager-web
war
com.gome.scw
managet-service
0.0.1-SNAPSHOT
junit
junit
${junit.version}
test
com.alibaba
druid
mysql
mysql-connector-java
org.springframework
spring-beans
org.springframework
spring-tx
org.springframework
spring-core
org.springframework
spring-context
org.springframework
spring-jdbc
org.springframework
spring-aspects
org.springframework
spring-orm
org.springframework
spring-webmvc
org.springframework
spring-webmvc-portlet
org.springframework
spring-test
test
org.springframework
spring-context-support
org.mybatis
mybatis-spring
org.mybatis
mybatis
com.github.pagehelper
pagehelper
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
org.mybatis.generator
mybatis-generator-core
log4j
log4j
org.slf4j
slf4j-log4j12
org.slf4j
slf4j-api
javax.servlet.jsp
jsp-api
provided
javax.servlet
servlet-api
provided
javax.servlet
jstl
写配置
新建一个springmvc.xml
编写web.xml
添加Person类和HelloController类
添加index.jsp页面与success.jsp页面
运行
当当当当。成功!!!!
再贴一个结构图
好了,我们项目的分包在上边已经做好了,只是在上边导入三大框架与一些其他的基础包时,建议还是按需要导入,不同的模块导入不同的包,不要全部都放在一起,会比较乱不容易查看。下面整合开始,因为之前有过ssm的整合,所以不清楚的地方我们可以查看一下之前的文章。
mybatis的整合比较简单,所有需要的包都已经在上边导入过来,只写配件文件就可以了,配置文件的内容也很简单,基本上只需要写
首先,创建数据库表,这里创建了多张数据库表,为了便于理解大家只看其中的一个就可以了。
复制sql语句直接在mysql中创建就可以啦
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2017/6/12 21:44:22 */
/*==============================================================*/
drop table if exists t_account_type_cert;
drop table if exists t_advertisement;
drop table if exists t_cert;
drop table if exists t_dictionary;
drop table if exists t_member;
drop table if exists t_member_address;
drop table if exists t_member_cert;
drop table if exists t_member_project_follow;
drop table if exists t_message;
drop table if exists t_order;
drop table if exists t_param;
drop table if exists t_permission;
drop table if exists t_project;
drop table if exists t_project_tag;
drop table if exists t_project_type;
drop table if exists t_return;
drop table if exists t_role;
drop table if exists t_role_permission;
drop table if exists t_tag;
drop table if exists t_type;
drop table if exists t_user;
drop table if exists t_user_role;
/*==============================================================*/
/* Table: t_account_type_cert */
/*==============================================================*/
create table t_account_type_cert
(
id int(11) not null auto_increment,
accttype char(1),
certid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_advertisement */
/*==============================================================*/
create table t_advertisement
(
id int(11) not null auto_increment,
name varchar(255),
iconpath varchar(255),
status char(1),
url varchar(255),
userid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_cert */
/*==============================================================*/
create table t_cert
(
id int(11) not null auto_increment,
name varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_dictionary */
/*==============================================================*/
create table t_dictionary
(
id int(11) not null auto_increment,
name varchar(255),
code varchar(255),
subcode varchar(255),
val varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_member */
/*==============================================================*/
create table t_member
(
id int(11) not null auto_increment,
loginacct varchar(255) not null,
userpswd char(32) not null,
username varchar(255) not null,
email varchar(255) not null,
authstatus char(1) not null,
usertype char(1) not null,
realname varchar(255),
cardnum varchar(255),
accttype char(1),
primary key (id)
);
/*==============================================================*/
/* Table: t_member_address */
/*==============================================================*/
create table t_member_address
(
id int(11) not null auto_increment,
memberid int(11),
address varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_member_cert */
/*==============================================================*/
create table t_member_cert
(
id int(11) not null auto_increment,
memberid int(11),
certid int(11),
iconpath varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_member_project_follow */
/*==============================================================*/
create table t_member_project_follow
(
id int(11) not null auto_increment,
projectid int(11),
memberid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_message */
/*==============================================================*/
create table t_message
(
id int(11) not null auto_increment,
memberid int(11),
content varchar(255),
senddate char(19),
primary key (id)
);
/*==============================================================*/
/* Table: t_order */
/*==============================================================*/
create table t_order
(
id int(11) not null auto_increment,
memberid int(11),
projectid int(11),
returnid int(11),
ordernum varchar(255),
createdate char(19),
money int(11),
rtncount int(11),
status char(1),
address varchar(255),
invoice char(1),
invoictitle varchar(255),
remark varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_param */
/*==============================================================*/
create table t_param
(
id int(11) not null auto_increment,
name varchar(255),
code varchar(255),
val varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_permission */
/*==============================================================*/
create table t_permission
(
id int(11) not null auto_increment,
pid int(11),
name varchar(255),
icon varchar(255),
url varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_project */
/*==============================================================*/
create table t_project
(
id int(11) not null auto_increment,
name varchar(255),
remark varchar(255),
money bigint (11),
day int(11),
status char(1),
deploydate char(10),
supportmoney bigint(11),
supporter int(11),
completion int(3),
memberid int(11),
createdate char(19),
follower int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_project_tag */
/*==============================================================*/
create table t_project_tag
(
id int(11) not null auto_increment,
projectid int(11),
tagid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_project_type */
/*==============================================================*/
create table t_project_type
(
id int not null auto_increment,
projectid int(11),
typeid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_return */
/*==============================================================*/
create table t_return
(
id int(11) not null auto_increment,
projectid int(11),
type char(1),
supportmoney int(11),
content varchar(255),
count int(11),
signalpurchase int(11),
purchase int(11),
freight int(11),
invoice char(1),
rtndate int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_role */
/*==============================================================*/
create table t_role
(
id int(11) not null,
name varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_role_permission */
/*==============================================================*/
create table t_role_permission
(
id int(11) not null auto_increment,
roleid int(11),
permissionid int(11),
primary key (id)
);
/*==============================================================*/
/* Table: t_tag */
/*==============================================================*/
create table t_tag
(
id int(11) not null auto_increment,
pid int(11),
name varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_type */
/*==============================================================*/
create table t_type
(
id int(11) not null auto_increment,
name varchar(255),
primary key (id)
);
/*==============================================================*/
/* Table: t_user */
/*==============================================================*/
create table t_user
(
id int not null auto_increment,
loginacct varchar(255) not null,
userpswd char(32) not null,
username varchar(255) not null,
email varchar(255) not null,
createtime char(19),
primary key (id)
);
/*==============================================================*/
/* Table: t_user_role */
/*==============================================================*/
create table t_user_role
(
id int(11) not null auto_increment,
userid int(11),
roleid int(11),
primary key (id)
);
alter table t_project_tag add constraint FK_Reference_7 foreign key (projectid)
references t_project (id) on delete restrict on update restrict;
alter table t_project_tag add constraint FK_Reference_8 foreign key (tagid)
references t_tag (id) on delete restrict on update restrict;
alter table t_project_type add constraint FK_Reference_5 foreign key (projectid)
references t_project (id) on delete restrict on update restrict;
alter table t_project_type add constraint FK_Reference_6 foreign key (typeid)
references t_type (id) on delete restrict on update restrict;
alter table t_role_permission add constraint FK_Reference_3 foreign key (roleid)
references t_role (id) on delete restrict on update restrict;
alter table t_role_permission add constraint FK_Reference_4 foreign key (permissionid)
references t_permission (id) on delete restrict on update restrict;
alter table t_user_role add constraint FK_Reference_1 foreign key (userid)
references t_user (id) on delete restrict on update restrict;
alter table t_user_role add constraint FK_Reference_2 foreign key (roleid)
references t_role (id) on delete restrict on update restrict;
编写mbg配置文件
我们选择使用java程序运行mbg生成pojo和dao,创建一个运行mbg的java并运行
package com.gome.scw.manager.dao.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MBGText {
public static void main(String[] args) throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
//代码生成
myBatisGenerator.generate(null);
System.out.println("生成ok了!");
}
}
好了,逆向工程生成pojo和dao完成,废了这么大的功夫现在终于可以编写mybatis的配置文件了.xml。
这样mybatis的部分就配置好了,只需要在spring中添加就可以了。
由于spring需要配置的东西比较多,为了直观与更好的阅读性,我们把spring的配置分为三个xml,在web模块中添加spring的配置文件spring-beans.xml、spring-mybatis.xml和spring-tx.xml三个配置文件,spring-beans.xml用于包扫描与添加组件、spring-mybatis.xml用于添加mybatis与其整合、spring-tx.xml用于事务控制;
好了,spring的配置文件写好了,只需要在web.xml中加入spring的配置文件就可以了;
编写springmvc.xml配置文件
最后修改web.xml,在其中添加字符编码等等。。
contextConfigLocation
classpath:spring-*.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceRequestEncoding
true
forceResponseEncoding
true
characterEncodingFilter
/*
hiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
hiddenHttpMethodFilter
/*
好了,整合完毕。
现在我们来进行测试。
在service模块添加代码 UserService.java和UserServiceImpl.java
package com.gome.scw.manager.service;
import com.gome.scw.manager.bean.User;
public interface UserService {
public User getUserById(Integer id);
}
package com.gome.scw.manager.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gome.scw.manager.bean.User;
import com.gome.scw.manager.dao.UserMapper;
import com.gome.scw.manager.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User getUserById(Integer id) {
// TODO Auto-generated method stub
return userMapper.selectByPrimaryKey(id);
}
}
修改web模块中的HelloController.java文件
package com.gome.scw.manager.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gome.scw.manager.bean.User;
import com.gome.scw.manager.bean.person;
import com.gome.scw.manager.service.UserService;
@Controller
public class HelloController {
@Autowired
UserService userService;
@RequestMapping(value="/hello")
public String hello(@RequestParam(value="id",defaultValue="1")
Integer id, Model dModel) {
User user = userService.getUserById(id);
dModel.addAttribute("user", user);
return "success";
}
}
接着写个web模块中的success.jsp页面,直接输出user的值,需要在pojo模块的user.java中重写tostring()方法;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
你好!!!!
${user }
在数据库中添加测试数据
运行。。:
当当当当。。。。。。
成功!!!! 搞定。
最后特别提示:
druid数据库连接池的名字不要学错了哦
还有配置MVC时,加载controller包的时候 ,关闭默认加载也容易大意出错。。
因为一时大意,这两个问题 我足足查了一整天才查到,发现问题时我都要疯啦,犯了这么低级的问题也是没谁了,呵呵呵。。。
最后把我做的工程地址放在这里以备需要时查看https://download.csdn.net/download/qq_25106373/11022543