ssm整合--环境搭建--Demo

1. 首先,创建一个maven工程,导入坐标




  4.0.0

  com.hao
  ssm
  1.0-SNAPSHOT
  war

  ssm Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    5.0.2.RELEASE
    1.6.6
    1.2.12
    5.1.6
    3.4.5
  

  
    
    
      org.aspectj
      aspectjweaver
      1.6.8
    

    
      org.springframework
      spring-aop
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
    

    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      junit
      junit
      4.12
      compile
    

    
      mysql
      mysql-connector-java
      ${mysql.version}
    

    
      javax.servlet
      servlet-api
      2.5
      provided
    

    
      javax.servlet.jsp
      jsp-api
      2.0
      provided
    

    
      jstl
      jstl
      1.2
    

    
    
      log4j
      log4j
      ${log4j.version}
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    

    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    

    
      org.mybatis
      mybatis-spring
      1.3.0
    

    
      c3p0
      c3p0
      0.9.1.2
      jar
      compile
    
  

  
    ssm
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

2.创建数据库

CREATE DATABASE ssm;
USE ssm;
CREATE TABLE account(
  id INT KEY AUTO_INCREMENT,
  NAME VARCHAR(25),
  money DOUBLE 
);

3.配置spring框架环境

     (1).创建包路径

                ssm整合--环境搭建--Demo_第1张图片

         (2)创建Accout实体类

          

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double money;

    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 Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

          (3)创建AccountService接口

import com.hao.domain.Account;

import java.util.List;

public interface AccountService {
    // 查询所有账户
    public List findAll();

    // 保存帐户信息
    public void saveAccount(Account account);
}

          (4)创建AccountService实现类

import com.hao.dao.AccountDao;
import com.hao.domain.Account;
import com.hao.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")//此时表明将此类交给了ioc管理
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public List findAll() {
        System.out.println("业务层:查询所有账户...");
        return accountDao.findAll();
    }

    public void saveAccount(Account account) {
        System.out.println("业务层:保存帐户...");
        accountDao.saveAccount(account);
    }
}

          (5)创建applicationContext.xml(最终实现)




    
    
        
        
    

    
    
    
        
        
        
        
    

    
    
        
    

    
    
        
    

    
    
    
        
    

    
    
        
            
            
        
    

    
    
        
    

4.配置mybatis框架环境

(1)创建AccountDao接口

import com.hao.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AccountDao {

    // 查询所有账户
    @Select("select * from account")
    public List findAll();

    // 保存帐户信息
    @Insert("insert into account (name,money) values (#{name},#{money})")
    public void saveAccount(Account account);

}

(2)Spring整合MyBatis框架(在application.xml实现)

5.配置springMVC框架环境

(1)创建springmvc.xml配置文件




    
    
        
    

    
    
        
        
    

    
    
    
    

    
    

(2) 创建AccountController类

import com.hao.domain.Account;
import com.hao.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
    /**
     * 帐户web
     */
    @Controller
    @RequestMapping("/account")
    public class AccountController {

        @Autowired
        private AccountService accountService;

        @RequestMapping("/findAll")
        public String findAll(Model model){
            System.out.println("表现层:查询所有账户...");
            // 调用service的方法
            List list = accountService.findAll();
            model.addAttribute("list",list);
            return "list";
        }

        /**
         * 保存
         * @return
         */
        @RequestMapping("/save")
        public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
            accountService.saveAccount(account);
            response.sendRedirect(request.getContextPath()+"/account/findAll");
            return;
        }
}

6.创建index.jsp测试

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


    Title



    测试查询

    

测试包

姓名:
金额:

7.创建web.xml




  Archetype Created Web Application

  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    
    
  

  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc.xml
    
    
    1
  
  
    dispatcherServlet
    /
  

  
  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    characterEncodingFilter
    /*
  



8.在WEB-INF下创建pages文件夹,创建list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title



    

查询所有的帐户

${account.name}

9 测试

ssm整合--环境搭建--Demo_第2张图片

ssm整合--环境搭建--Demo_第3张图片

至此  ssm整合环境搭建告一段落。

附:

补充:还有一个log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=H:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

 

 

 

 

你可能感兴趣的:(SSM)