Struts+Spring+Mybatis+Mysql

使用 SSM 三大框架 用Spring的注解形式  做一个注册的案例


1.ssm所需要的jar包

Struts+Spring+Mybatis+Mysql_第1张图片


2.web.xml的配置




    index.jsp




  
     org.springframework.web.context.ContextLoaderListener
  




   
       contextConfigLocation
   
   
     classpath*:applicationContext*.xml
   
  


   struts2
   
  	   org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   
    		
      config
      struts-default.xml,struts-plugin.xml,struts.xml
   	


  	struts2
  	/*


3.applicationContext.xml配置



         
            
           
            
            
         
            
          
          
         
         
         
			${jdbc.driverClassName}
		 
		 
			${jdbc.url}
		 
		 
			${jdbc.username}
		 
		 
			${jdbc.password}
		 
        
   
           
          
             
             
          
      
          
          
           
          
      
          
          
            
         
      

4.mybatis-config.xml的配置

  
  

  
  
        
  
 
  
  
   
  
  
  
    
  

5.jdbc-mysql.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456


6.Users的实体类

package com.ssm.demo.pojo;
import java.io.Serializable;

@SuppressWarnings("serial")
public class Users implements Serializable {
  private Integer id;
  private String name;
  private String pwd;

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 getPwd() {
	return pwd;
}
public void setPwd(String pwd) {
	this.pwd = pwd;
}
public Users(String name, String pwd) {
	super();
	this.name = name;
	this.pwd = pwd;
}
public Users() {
	super();
} 
}

7.IUsersMapper接口类的方法

package com.ssm.demo.persistence;

import com.ssm.demo.pojo.Users;

public interface IUsersMapper {
    //添加
 public void inser_users(Users users);
}

8.IUsersMapper.xml的配置






id="inser_users" parameterType="users" useGeneratedKeys="true">
    insert into users(id,name,pwd) values(null,#{name},#{pwd})



9,UsersDao类

@Repository("usersDao")
public class UsersDao  implements IUsersDao{

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate

      public void insert_Users(Users users) {
		System.out.println("Dao类的添加数据的方法");
		//IUsersMapper  iusersMapper=sqlSessionTemplate.getMapper(IUsersMapper.class);
		//iusersMapper.inser_users(users);          
               sqlSessionTemplate.insert("inser_users", users)
	}
}

10.UsersServlet类

@Service("usersServlet")
public class UsersServlet implements IUsersServlet {

	@Autowired
	private IUsersDao usersDao;
 	
	public void insert_Users(Users users) {	
	usersDao.insert_Users(users);
	}


11 .Add.action类

@Controller("Add")
public class Add_action {

	@Autowired
	private IUsersServlet usersServlet;
	private String name;//用户名
	private String pwd;//密码
	
	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 String execute(){

 		Users users=new Users(name, pwd);
 		usersServlet.insert_Users(users);
		 return "SCCE";
	 }
 }


 
  

12.struts.xml的配置




   
      
    
       /All.jsp
     
   

13.注册页面


   
用户名:
密 码:






你可能感兴趣的:(spring,mybatis,struts,数据库,框架)