maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea

第一步:新建项目,maven项目

一.新建maven项目

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第1张图片


1,选择maven

2,选择project sdk 安装的是1.7版的jdk,如果是第一次安装idea 需要配置本地jdk

3,可以选择生成相应的项目结构,本次不作选择,直接下一步


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第2张图片

maven项目必须填写GroupId 和ArtifactId,填写后直接下一步

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第3张图片

1.填写项目名称 填写完成后点击完成

2.项目名称,目录地址


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第4张图片

1.新建项目的目录结构

2.maven的pom文件,文件名称是项目名称,

3.groupId和artifactId ,这两个id命名通常是不同的有一定规则,这里为了方便,简单命名


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第5张图片

1.maven project 可以看到有关maven 的功能项

2lifecyle 点击clean会将项目解除依赖 ,install会添加依赖

3.maven项目如果出现jar包更新,运行,必须进行点击clean,重新install。

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第6张图片


二.配置tomcat

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第7张图片


进入到配置界面

1.点击加号添加tomcat server 如果没有tomcat 选项要在idea中安装tomcat插件具体在seting中。

2.选择local

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第8张图片

1.填写名字

2.点击下拉框选择本地tomcat目录进行配置

3.部署

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第9张图片

点击artifact进行部署,这里部署方式是将war包进行发布,如果新建项目没有war包可以点击右下方Fix进行生成,本次是使用maven进行打war包,具体在pom文件中进行配置。

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第10张图片


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第11张图片

war包发布后 点击Server,可以进行相关配置

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第12张图片




至此新建maven项目完毕,剩下就应该是整合框架。

第二步:搭建Spring

1.加入jar包,在pom文件中写入spring所用jar包

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第13张图片



		
			org.springframework
			spring-core
			3.2.3.RELEASE
		
		
			org.springframework
			spring-beans
			3.2.3.RELEASE
		
		
			org.springframework
			spring-web
			3.2.3.RELEASE
		
		
			org.springframework
			spring-context
			3.2.3.RELEASE
		
		
			org.springframework
			spring-jdbc
			3.2.3.RELEASE
		
		
			org.springframework
			spring-webmvc
			3.2.3.RELEASE
		
		
			org.springframework
			spring-webmvc-portlet
			3.2.3.RELEASE
		

2.创建spring配置文件

1.在项目目录中创建spring.xml配置文件,idea工具可以自动生成配置文件,在项目根目录上右键添加框架支持。

2.初始spring配置文件,只要写入自动扫描包即可,在项目启动时spring会自动去扫描。

3.本项目采用注解方式,不再配置文件中在写相关配置。

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第14张图片




	


三.测试spring

1.在service层创建接口,在impl文件夹中写实现类


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第15张图片

1.编写service层和service实现类

2.注解方式,在spring启动的时会根据这个注解将它加入到上下文中,相当于在spring配置文件中加


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第16张图片


因为在spring配置文件中写了自动扫描包,只要service文件能被扫描到就可以。

1.在test文件目录下建测试文件。

2.获取spring上下文

3.JUnit测试方法,不需要写main方法。

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第17张图片


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第18张图片

四.spring配置web.xml

要让spring在web项目中应用,所以要在web.xml文件中配置。

1.指定spring配置文件的位置

2.指定一个监听器,只有指定了监听器,才能在web项目启动中把spring上下文启动。

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第19张图片



第三步:搭建struts2

一.加入jar包

1.在pom文件中加入struts2核心包,struts2的注解包,因为要和spring做整合,所以要加入struts2-spring-plugin。

2.idea中如果有个功能未自动导入jar包,就是在pom文件中写完jar包后会自动导入,右下方eventlog

3.点击enable,就可以自动导入jar包了

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第20张图片

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第21张图片


二.创建配置文件

1.创建config目录,struts配置文件struts.xml,

2.web目录,web.xml文件


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第22张图片


struts2基础配置文件struts.xml基本配置




	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	

	


三.sturts配置web.xml文件

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第23张图片



四.编写测试类进行测试,项目目录

因为struts2-spring-plugin 包spring就会自动找@Action将它加入spring进行管理

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第24张图片

在浏览器中输入 http://localhost:8081/userAction!test.action 因为部署时没有写项目名称所以地址不用写项目名称

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第25张图片

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第26张图片

第四步:搭建mybatis

一.加入mybatis 的jar包,和mybatis和spring整合jar包

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第27张图片

二.创建mybatis的配置文件,

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第28张图片






	
	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		  
		 
		
	

	
	
		
	




在spring中配置数据库连接池采用的是druid,数据库为oracle。

添加jar包

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第29张图片

三.配置datasource

在spring中配置连接池。这里另外建立了一个spring-dataSource文件,在spring-config文件中加载这个文件即可,durid配置连接池为固定格式。将jdbc文件放在指定目录下载spring-datasource文件中指定即可


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第30张图片

四.配置事物

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第31张图片

五.配置oap切面

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第32张图片

spring-datasource文件



	
	
	
		
	
	
		
		
		
		
		
		
		
		
		
		
	

	
	
	
	
	
	
	
	
	
	
	
	

	 
     
         
     
     
         
        
      		
             
             
             
			
         
     
     
         
              
         
     



在spring-config.xml文件中引入 spring-datasource.xml文件

六.配置sqlSessionFactory

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第33张图片


七.jdbc配置文件

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第34张图片


8.目录结构

建立dao层和Mapper层目录结构,mybatis有很多方式实现mapper和dao层对应,这里采用在Mapper中配置命名空间


maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第35张图片

9.UserDao.java

package com.ztx.dao;

import com.ztx.entity.User;

/**
 * Created by Think on 2016/10/19.
 */
public interface UserDao {

	 User selectById(String id);
}

10.UserMapper.xml




	
	
		
		
		
	

	
	

11.在spring-config.xml中配置Dao

每添加一个dao,就要在spring-config中配置

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第36张图片


第五步:项目整体

目录结构

maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea_第37张图片

1.userAction.java

package com.ztx.action;

import com.ztx.entity.User;
import com.ztx.service.UserService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;

@ParentPackage("struts-default")
@Namespace("/") //命名空间为根目录
@Action(value = "userAction")
public class userAction {

	@Resource(name = "userService")
	private UserService userService;

	public void test(){
		System.out.println("进入了Action");
		String id = "盖伦";
		User u = userService.SelestPwdById(id);
		System.out.println("pwd:"+u.getPwd());
	}

}

2.UserService.java

package com.ztx.service;

import com.ztx.entity.User;

/**
 * Created by Think on 2017/1/22.
 */
public interface UserService {

	public void test();

	public User SelestPwdById(String id);
}

3.UserServiceImpl.java

package com.ztx.service.impl;

import com.ztx.dao.UserDao;
import com.ztx.entity.User;
import com.ztx.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service(value = "userService")
public class UserServiceImpl implements UserService {

	@Resource(name="userDao")
	private UserDao userDao;

	@Override
	public void test() {
		System.out.println("测试spring");

	}

	@Override
	public User SelestPwdById(String id) {
		return userDao.selectById(id);
	}
}

4.UserDao.java

package com.ztx.dao;

import com.ztx.entity.User;

/**
 * Created by Think on 2016/10/19.
 */
public interface UserDao {

	 User selectById(String id);
}

5.实体类User.java

package com.ztx.entity;

import java.util.Date;

public class User {
	private  String  id;
	private  String  name;
	private  String  pwd;
	private  Date    createDateName;
	private  Date    modifyDateTime;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public Date getCreateDateName() {
		return createDateName;
	}

	public void setCreateDateName(Date createDateName) {
		this.createDateName = createDateName;
	}

	public Date getModifyDateTime() {
		return modifyDateTime;
	}

	public void setModifyDateTime(Date modifyDateTime) {
		this.modifyDateTime = modifyDateTime;
	}
}



6.UserMapper.xml




	
	
		
		
		
	

	
	
TsetSpring.java

/*
import com.ztx.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestSpring {

	@Test
	public void test(){
		//获得这个spring-config。
		ApplicationContext ac = new ClassPathXmlApplicationContext
				(new String[]{"classpath:\\com\\ztx\\config\\spring\\spring-config.xml"});
		UserService userService= (UserService) ac.getBean("userService");   //调用bean 强转
		userService.test();
	}

}
*/


配置文件

7.spring-config.xml



	

	
	
	
		

		
		
		
		
	

	
		
		
	


8.spring-dataSource.xml



	
	
	
		
	
	
		
		
		
		
		
		
		
		
		
		
	

	
	
	
	
	
	
	
	
	
	
	
	

	 
     
         
     
     
         
        
      		
             
             
             
			
         
     
     
         
              
         
     

9.struts.xml




	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	


10.MybatisConfig.xml





	
	

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		  
		 
		
	

	
	
	
		
	
	



11.jdbc.properties

driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:xe
username= xhy
password= xhy

12.web.xml



	
	
		contextConfigLocation
		classpath:com/ztx/config/spring/spring-config.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		struts
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts
		*.action
	
	
		/index.jsp
	



13.pom.xml



	4.0.0
	import
	import
	war
	1.0-SNAPSHOT
	import Maven Webapp
	http://maven.apache.org
	
		
			org.apache.poi
			poi
			3.12
		
		
			org.apache.poi
			poi-ooxml
			3.12
		
		
			org.eclipse.birt.runtime.3_7_1
			org.apache.xerces
			2.9.0
		
		
			org.apache.directory.studio
			org.dom4j.dom4j
			1.6.1
		
		
			net.sourceforge.javacsv
			javacsv
			2.0
		
		
			commons-net
			commons-net
			3.3
		
		
			org.jodd
			jodd-props
			3.6.6
		
		
			org.slf4j
			slf4j-api
			1.7.12
		
		
			org.slf4j
			slf4j-log4j12
			1.7.12
		
		
			com.jcraft
			jsch
			0.1.53
		
		
			javax.servlet
			javax.servlet-api
			3.1.0
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.1
			provided
		
		
			com.google.code.gson
			gson
			2.5
		
		
			commons-collections
			commons-collections
			3.2.1
		
		
			junit
			junit
			4.12
		
		
			org.aspectj
			aspectjweaver
			1.8.4
		
		
			aopalliance
			aopalliance
			1.0
		
		
			cglib
			cglib-nodep
			2.2.2
		

		
		
			org.springframework
			spring-core
			3.2.3.RELEASE
		
		
			org.springframework
			spring-beans
			3.2.3.RELEASE
		
		
			org.springframework
			spring-web
			3.2.3.RELEASE
		
		
			org.springframework
			spring-context
			3.2.3.RELEASE
		
		
			org.springframework
			spring-jdbc
			3.2.3.RELEASE
		
		
			org.springframework
			spring-webmvc
			3.2.3.RELEASE
		
		
			org.springframework
			spring-webmvc-portlet
			3.2.3.RELEASE
		
	
	
		org.apache.struts
		struts2-core
		2.3.4.1
	
	
	
		org.apache.struts
		struts2-convention-plugin
		2.3.4.1
	
		
		
			org.apache.struts
			struts2-spring-plugin
			2.3.4.1
		
		
			junit
			junit
			4.12
			test
		
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
			org.mybatis
			mybatis
			3.1.1
		
		
			com.oracle
			ojdbc6
			1.0
		
		
			com.alibaba
			druid
			1.0.14
		
		
			aopalliance
			aopalliance
			1.0
		
		
			cglib
			cglib-nodep
			2.2.2
		
	

	
		import
		
			
				src/main/java
				
					**/*.sql
					**/*.xml
				
				false
			
			
				src/main/resources
				
					**/*.properties
					**/*.xml
					**/*.props
				
				false
			
		
		
			
				maven-compiler-plugin
				2.3.2
				
					1.7
					1.7
				
			
			
				maven-war-plugin
				2.2
				
					
					false
				
			
		
	













你可能感兴趣的:(ssm框架搭建)