SSM框架整合(使用步骤)

1.导入SSM整合需要的jar包

2.准备配置文件和属性文件

(1)db.properties:设定连接数据库的属性:

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=utf8
mysql.username=root
mysql.password=root
validationQuery=SELECT 1
validationQuery用于验证查询,用于验证数据库是否能进行操作。

(2)log4j.properties:设置日志需要显示的对应信息

log4j.rootLogger=ERROR, stdout
log4j.logger.com.study.mapper.UsersMapper=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
(3)配置web.xml文件(结合web.xml文件的流程配置springMVC、Spring、Mybatis的配置文件):

a.web.xml中配置SpringMVC的前端控制器:


	
		SpringMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		
		1
		
		true
	
	
		SpringMVC
		*.action
	

这里由于配置springMVC的配置需要映射springmvc.xml,所以可以先去创建SpringMVC的配置文件并配置:



	
	 
	
	
	
	
	
	
		
		
		
			.jsp
		
	


b. web.xml配置Spring


		contextConfigLocation
		
		classpath:applicationContext*.xml
	
	
		
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		org.springframework.web.util.IntrospectorCleanupListener
	

这里需要映射Spring的配置文件:applicationContext*.xml(*表示通配符,以这个 applicationContext开头的xml文件):

applicationContext.xml:

  
 


	
		
			classpath:db.properties
		
	









	

       
 

   

	
	
	
   

mybatis-config.xml:mybatis的配置文件



 


	
	
		
		
	

applicationContext-druid.xml:配置数据源,这里用到的是阿里巴巴的druid(德鲁伊)数据源




	
	
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

	
	
	

	
	
	
	

	
		
			
				com.study.service.*
				com.study.mapper.*
			
		
	

	
		
	




applicationContext-transaction.xml:配置事务增强和事务切面



	
	
		
		
		
			
			
			
			
			
			
			
			
			
			
			

			
			
			
			
			

			
		
	
	
	
		
		
	

	
	
这里Spring的配置文件和MyBatis的配置文件就配置完成了。

c.在web.xml中配置druid数据源Filter:在b项中需要用druid数据源,所以需要配置


	
		DruidWebStatFilter
		com.alibaba.druid.support.http.WebStatFilter
		
		
			exclusions
			*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
		
	
	
		DruidWebStatFilter
		/*
	
	
	
		DruidStatView
		com.alibaba.druid.support.http.StatViewServlet
	
	
		DruidStatView
		/druid/*
	

d.在xml中配置编码过滤器:


	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		true
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	
到这里需要配置的文件基本配置完成了。

3.使用MyBatis工具生成的mapper,mapper.xml,po如下:详见下一篇文章:使用mybatis工具逆向生成mapper等文件

po:Users.java

package com.study.po;

public class Users {
    private Integer id;

    private String username;

    private String password;

    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 == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

	@Override
	public String toString() {
		return "Users [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
    
}

mapper:UsersMapper.java

package com.study.mapper;

import com.study.po.Users;

public interface UsersMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Users record);

    int insertSelective(Users record);

    Users selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Users record);

    int updateByPrimaryKey(Users record);
}

mapper.xml:UsersMapper.xml




  
    
    
    
  
  
    id, username, password
  
  
  
    delete from users
    where id = #{id,jdbcType=INTEGER}
  
  
    insert into users (id, username, password
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  
  
    insert into users
    
      
        id,
      
      
        username,
      
      
        password,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
    
  
  
    update users
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        password = #{password,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update users
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  


4.编写controller,service,jsp:

UsersController.java:

package com.study.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.study.po.Users;
import com.study.service.UsersService;

@Controller
public class UserController {
	
	@Autowired//自动装配
	private UsersService usersService;

	@RequestMapping(value="/{fromName}")
	public String index(@PathVariable String fromName){
		return fromName;
	}
	
	@RequestMapping(value="/login")
	public String login(HttpServletRequest request,Model model){
		int id = Integer.parseInt(request.getParameter("id"));
		Users users = usersService.selectById(id);
		model.addAttribute("users", users);
		return "login";
		
	}
}
UsersService.java:

package com.study.service;

import com.study.po.Users;

public interface UsersService {

	Users selectById(Integer id);
}

UsersServiceImp.java:UsersService接口的实现类

package com.study.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.mapper.UsersMapper;
import com.study.po.Users;
import com.study.service.UsersService;

@Service
public class UsersServiceImp implements UsersService{
	@Autowired
	private UsersMapper usersMapper;

	@Override
	public Users selectById(Integer id) {
		Users user = usersMapper.selectByPrimaryKey(id);
		return user;
	}

}

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>





Insert title here


${requestScope.users }


















你可能感兴趣的:(JavaEE)