Maven创建SSM项目

1、maven创建项目

参考Maven创建WEB项目

2、 项目的基本目录是这样婶的:

Maven创建SSM项目_第1张图片

3、spring注解方式搭建最简单项目框架

几个重要的配置文件:

web.xml

applicationContext.xml Spring的配置文件

jdbc.properties数据库配置文件

log4j.properties日志配置文件

dispatcher-servlet.xml spring MVC配置文件

下面给出几个配置文件的详细代码

web.xml:此文件装载spring、spring MVC、log4j及spring监听器、乱码处理



	
   
     
        contextConfigLocation 
         
        classpath:applicationContext.xml 
         
     
     
         
        org.springframework.web.context.ContextLoaderListener 
         
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    
     
     
        encodingFilter 
         
        org.springframework.web.filter.CharacterEncodingFilter 
         
         
            encoding 
            UTF-8 
         
     
     
        encodingFilter 
        /* 
    
     
     
        log4jConfigLocation 
         
        classpath:log4j.properties 
         
     
     
        log4jRefreshInterval 
        60000 
     
     
         
        org.springframework.web.util.Log4jConfigListener 
         
    
     
     
        dispatcher 
         
        org.springframework.web.servlet.DispatcherServlet 
         
         
           contextConfigLocation 
            
           classpath:dispatcher-servlet.xml 
            
           
        1 
     
     
        dispatcher 
        / 
    
  
  
    index.jsp
  
jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root
applicationContext.xml

 
 

     
     
         
     
    
     
     
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
     
    
    
        
    
    
    
    
        
    
    
	
	
    
        
    

    
    
    
 


dispatcher-servlet.xml

 


     
     
    
     
     
     
     
         

4、  Mybatis自动代码生成

5、 首先完成工作,然后更好的去理解工作,继续贴代码,以user为例

UserDaoImpl.java

package com.mycompany.web.dao.impl;

import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.mycompany.web.dao.IUserDao;
import com.mycompany.web.entity.User;

@Repository
public class UserDaoImpl extends SqlSessionDaoSupport implements IUserDao {
	
	@Resource  
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }  

	public int deleteByPrimaryKey(Long id) {
		return this.getSqlSession().delete("deleteByPrimaryKey", id);
	}

	public int insert(User record) {
		return this.getSqlSession().insert("insert", record);
	}

	public int insertSelective(User record) {
		return this.getSqlSession().insert("insertSelective", record);
	}

	public User selectByPrimaryKey(Long id) {
		return this.getSqlSession().selectOne("selectByPrimaryKey", id);
	}

	public int updateByPrimaryKey(User record) {
		return this.getSqlSession().update("updateByPrimaryKey", record);
	}

	public int updateByPrimaryKeySelective(User record) {
		return this.getSqlSession().update("updateByPrimaryKeySelective", record);
	}
}
UserServiceImpl.java

package com.mycompany.web.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mycompany.web.dao.IUserDao;
import com.mycompany.web.entity.User;
import com.mycompany.web.service.IUserService;

@Transactional
@Service("userService")
public class UserServiceImpl implements IUserService{
	
	@Resource
	private IUserDao userDaoImpl;	
	
	@Override
	public User getUserById(Long id){
		return this.userDaoImpl.selectByPrimaryKey(id);
	}
	
	@Override
	public void insertUser(List userList){
		for(int i=0;i
UserController.java

package com.mycompany.web.controller;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mycompany.web.entity.User;
import com.mycompany.web.service.IUserService;

@Controller
public class UserController {
	
	public static Log log = LogFactory.getLog(UserController.class); 

	@Resource
	private IUserService userService;
	
	@RequestMapping("/index")
    public String getUserById(Model model){
	    User user = userService.getUserById(1L);
	    model.addAttribute("user", user);
	    log.info("---------"+user.getName()+"-------】");
        return "MyJsp";
    }
}
UserMapper.java

不太清楚为什么要有这个类

package com.mycompany.web.entity.mapping;

import org.apache.ibatis.annotations.Param;

import com.mycompany.web.entity.User;

public interface UserMapper {
    public User selectByPrimaryKey(@Param("id") Long id);
}

UserMapper.xml

都是利用mybatis代码生成器自动生成的代码,只要修正namespace及type即可




  
    
    
    
    
    
  
  
    id, name, idcard, gender, age
  
  
  
  
  
    delete from user_info
    where id = #{id,jdbcType=BIGINT}
  
  
  
    insert into user_info (id, name, idcard, gender, age)
    values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR}, 
      		#{gender,jdbcType=CHAR}, #{age,jdbcType=INTEGER})
  
  
  
    insert into user_info
    
      
        id,
      
      
        name,
      
      
        idcard,
      
      
        gender,
      
      
        age,
      
    
    
      
        #{id,jdbcType=BIGINT},
      
      
        #{name,jdbcType=VARCHAR},
      
      
        #{idcard,jdbcType=VARCHAR},
      
      
        #{gender,jdbcType=CHAR},
      
      
        #{age,jdbcType=INTEGER},
      
    
  
  
  
    update user_info
    
      
        name = #{name,jdbcType=VARCHAR},
      
      
        idcard = #{idcard,jdbcType=VARCHAR},
      
      
        gender = #{gender,jdbcType=CHAR},
      
      
        age = #{age,jdbcType=INTEGER},
      
    
    where id = #{id,jdbcType=BIGINT}
  
  
  
    update user_info
    set name = #{name,jdbcType=VARCHAR},
     	idcard = #{idcard,jdbcType=VARCHAR},
      	gender = #{gender,jdbcType=CHAR},
      	age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=BIGINT}
  

简单说明一下访问页面路径的拼装:

比如访问路径为http://localhost:8080/ssmdemo/index

ssmdemo为项目名,/index及对应要访问的页面,那么页面在哪里对应的这个访问路径呢?在dispatcher-sevlet.xmlbean中配置

p:prefix="/jsp/user/"p:suffix=".jsp" />

/jsp/userjsp文件所在项目中的路径,.jsp为后缀,那么怎么找到指定的jsp页面呢?在UserController.java方法中,方法返回值即请求页面的名字。

6、 启动Tomcat,运行

启动成功后,访问http://localhost:8080/ssmdemo/index,会显示出user表中name字段的值,即项目搭建成功,继续下一步工作,请看下集。











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