Struts2+spring+Mybatis框架整合

一.准备工作jar包

   首先去找struts的.  http://struts.apache.org/ 下载最新的struts 2.3.8.

    然后再去找spring的.  http://www.springsource.org/spring-framework 下载spring-framework-3.2.9.RELEASE-dist

  最后自然就是MyBatis3.0的  http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DMyBatis  下载MyBatis的-3.2.2

二.提取需要的jar包

这里我就直接将我的ssm整合项目需要的jar包写出来;

struts2需要的jar包有:Struts2+spring+Mybatis框架整合_第1张图片spring需要的jar包有:Struts2+spring+Mybatis框架整合_第2张图片

mybatis需要的jar包有:Struts2+spring+Mybatis框架整合_第3张图片还需要apache的jar包有Struts2+spring+Mybatis框架整合_第4张图片

另外我使用的开发环境 myeclips+tomcate+Mysql所以还需要的是mysql的包mysql-connector-java-5.1.37-bin.jar另外我是初学者所以没有用mybatis的插件建议自己手动配置这些文件。

三.新建一个web项目

将上述的jar包导入到web项目中的lib包中,接着开始做关联配置;首先配置web.xmlStruts2+spring+Mybatis框架整合_第5张图片

2.然后配置mybatis.xml



3.spring-base.xml的配置

4.struts.xml的配置

5.UserMapper.xml的配置




username,password,gender,email,photo,delstates

	insert into users () values(#{username},#{password},#{gender},#{email},#{photo},#{delstates})

6.所有的配置文件已经配置好了,现在就是把框架搭建好,三层架构dao,service,web.action,实体层pojo我这里是通过一个用户类来实现用户注册的功能;

实体类User

public class User {
	
	private Integer id;
	private String username;
	private String password;
	private String gender;
	private String photo;
	private String email;
	private String delstates;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDelstates() {
		return delstates;
	}
	public void setDelstates(String delstates) {
		this.delstates = delstates;
	}
	public User() {
		super();
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", gender=" + gender + ", photo=" + photo
				+ ", email=" + email + ", delstates=" + delstates + "]";
	}
	


7.创建UserMapper类
package com.oyp.mapper;
import java.util.List;

import com.oyp.entity.User;

public interface UserMapper {
	void add(User user);
	List findAll();
}

8.创建接口IUserDao

package com.oyp.dao;

import java.util.List;

import com.oyp.entity.User;

public interface UserDao {
	void add(User user);

	List findAll();

}

9.创建接口IUserService


package com.oyp.service;

import java.util.List;

import com.oyp.entity.User;

public interface UserService {
	void register(User user);

	List findAll();
}

10.创建UserAction


package com.oyp.web.action;


import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import com.oyp.entity.User;
import com.oyp.service.UserService;

public class UserAction extends BaseAction {
	private UserService userService;
	private String username;
	private String password;
	private String gender;
	private String photo;
	private String email;
	private String delstates;
	
	private File file;
    private String fileContentType;
	private String fileFileName;
	
	private List users;
	
	public String register(){
		User user=new User();
		user.setDelstates(delstates);
		user.setEmail(email);
		user.setGender(gender);
		user.setPassword(password);
		user.setPhoto(photo);
		user.setUsername(username);
		System.out.println(username);
		String dir=application.getRealPath("/uploads");
		File file1=new File(dir,fileFileName);
		try
		{
			FileUtils.copyFile(file, file1);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		user.setPhoto("/uploads/"+fileFileName);
		userService.register(user);
		return SUCCESS;
	}
	
	public String findAll(){
		users=userService.findAll();
		request.setAttribute("users", users);
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDelstates() {
		return delstates;
	}

	public void setDelstates(String delstates) {
		this.delstates = delstates;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List getUsers() {
		return users;
	}

	public void setUsers(List users) {
		this.users = users;
	}

	

	
	
}

11.新建了三个jsp页面success.jsp,register.jsp以及显示所有用户信息的页面List.jsp

12.最后就是部署项目到tomcat上启动这个项目就可以了。




你可能感兴趣的:(spring,struts2,mybatis框架)