Struts2+maven+spring+mybatis整合实现注册功能实例

项目结构
在这里插入图片描述
在这里插入图片描述
1.pom.xml文件中导入依赖

<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.5.16</version>
		</dependency>

		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
			<version>3.1.15</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.6</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.5.0</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.6.0</version>
		</dependency>

		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>

		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.26-incubating</version>
		</dependency>

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.20.0-GA</version>
		</dependency>

2.web.xml文件配置

<!-- struts2设置 -->
  <filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.action.extension</param-name>
			<param-value>action</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

3.struts.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
	
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<constant name="struts.action.extension" value="action"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<package name="default" extends="struts-default" >
	
		<action name="register" class="ssm.action.RegisterAction" method="register">
			<result name="succeed">/RegisterSucceed.jsp</result>
		</action>
	
	</package>
</struts>

4.mybatis-member.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias alias="member" type="ssm.pojo.Member" />
	</typeAliases>
</configuration>

5.applicationContext-member.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 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.3.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
	default-autowire="byName" default-lazy-init="false">
	
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf8" />
		<property name="username" value="root" />
		<property name="password" value="111111" />
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-member.xml" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="ssm.mapper"></property>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

	<bean id="membersMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<property name="mapperInterface" value="ssm.mapper.MemberMapper" />
	</bean>
	
</beans>

6.Register.jsp注册页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ssm</title>
</head>
<body>
	<form action="register.action" method="post">
		<table>
			<tr>
				<td>会员名称:</td>
				<td><input type="text" id="name" name="name" /></td>
			</tr>
			<tr>
				<td>登录密码:</td>
				<td><input type="text" id="passWord" name="passWord" /></td>
			</tr>
			<tr>
				<td>确认密码:</td>
				<td><input type="text" id="conPassWord" name="conPassWord" /></td>
			</tr>
			<tr>
				<td><input type="submit" value="提交" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

7.注册成功后跳转的页面RegisterSucceed.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>恭喜您注册成功</title>
</head>
<body>

	<p>恭喜您注册成功</p>

</body>
</html>

8.加载spring配置文件Tools.java

package ssm.tools;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import ssm.mapper.MemberMapper;

public class Tools {
	
	public ClassPathXmlApplicationContext classPathXmlApplicationContext(){
		return new ClassPathXmlApplicationContext("classpath:/applicationContext-member.xml");
	}
	
	public MemberMapper getMember(){
		return (MemberMapper) classPathXmlApplicationContext().getBean("membersMapper");
	}

}

9.RegisterAction.java

package ssm.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import ssm.page.Register;
import ssm.service.MemberService;

public class RegisterAction extends ActionSupport implements ModelDriven<Register>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3044392009900738674L;
	Register register = new Register();
	MemberService memberService = new MemberService();
	
	public String register(){
		memberService.addMemberService(register.getPassWord(), register.getName());
		return "succeed";
	}

	@Override
	public Register getModel() {
		// TODO 自动生成的方法存根
		return register;
	}

}

10.MemberMapper.java

package ssm.mapper;

import ssm.pojo.Member;

public interface MemberMapper {
	
	public void addMember(Member member);
	
}

11.MemberMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ssm.mapper.MemberMapper">

	<insert id="addMember">
		insert into member (id,name,passWord,status,create_time,update_time) values (#{id},#{name},#{passWord},#{status},#{create_time},#{update_time})
	</insert>

</mapper>

12.Register.java

package ssm.page;

public class Register {
	
	private String name;
	private String passWord;
	private String conPassWord;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getConPassWord() {
		return conPassWord;
	}
	public void setConPassWord(String conPassWord) {
		this.conPassWord = conPassWord;
	}
	
	

}

13.Member.java

package ssm.pojo;

import java.util.Date;

public class Member {
	
	private Integer id;
	private String name;
	private String passWord;
	private String status;
	private Date create_time;
	private Date update_time;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Date getCreate_time() {
		return create_time;
	}
	public void setCreate_time(Date create_time) {
		this.create_time = create_time;
	}
	public Date getUpdate_time() {
		return update_time;
	}
	public void setUpdate_time(Date update_time) {
		this.update_time = update_time;
	}
}

14.MemberService.java

package ssm.service;

import java.util.Date;

import ssm.pojo.Member;
import ssm.tools.Tools;

public class MemberService {
	
	Member member = new Member();
	Tools tools = new Tools();

	public void addMemberService(String passWord,String name){
		member.setName(name);
		member.setStatus("1");
		member.setCreate_time(new Date());
		member.setUpdate_time(new Date());
		member.setPassWord(passWord);
		tools.getMember().addMember(member);
	}
	
}

15.DDL

create table member (
	id bigint(18) NOT NULL primary key AUTO_INCREMENT,
	name varchar(20) NOT NULL,
	passWord varchar(20) NOT NULL,
	status varchar(2) not null,
	create_time datetime NOT NULL,
	update_time datetime NOT NULL
);

16.系统实现
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值