java开发框架之SSM整合框架

一、搭建环境
1、创建数据库表和表结构
create table account(
id INT identity(1,1) primary key,
name varchar(20),
[money] DECIMAL
)
2、创建maven的工程SSM,在pom.xml文件引入依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m...d">
4.0.0
com.mingqi
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


  com.microsoft.sqlserver
  sqljdbc4
  4.0


  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


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


  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
    
  


3、部署ssm_web的项目,只要把ssm_web项目加​​java培训​​入到tomcat服务器中即可
4、编写实体类
package com.mingqi.domain;
import java.io.Serializable;
public class Account implements Serializable {

public void setId(Integer id) {
    this.id = id;
}
public void setName(String name) {
    this.name = name;
}
public void setMoney(Double money) {
    this.money = money;
}
public Integer getId() {
    return id;
}
public String getName() {
    return name;
}
public Double getMoney() {
    return money;
}
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
    return "Account{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", money=" + money +
            '}';
}

}

5、编写dao接口
package com.mingqi.dao;
import com.mingqi.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IAccountDao {

@Insert(value="insert into account (name,money) values (#{name},#{money})")
public void  saveAccount(Account account);
@Select("select * from account")
public List findAll();

}6、编写service接口和实现类
package com.mingqi.service;
import com.mingqi.domain.Account;
import java.util.List;
public interface IAccountService {

public void  saveAccount(Account account);
public List findAll();

}package com.mingqi.service.impl;
import com.mingqi.dao.IAccountDao;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountImpl implements IAccountService {

@Autowired
private IAccountDao accountDao;
@Override
public void saveAccount(Account account) {
    accountDao.saveAccount(account);
}
@Override
public List findAll() {
    System.out.println("业务层:查询所有账户...");
    List accounts=accountDao.findAll();
    return accounts;
    //return null;
}

}

二、Spring框架代码的编写
java开发框架之SSM整合框架_第1张图片

  1. 搭建和测试Spring的开发环境
    在webapp项目中创建applicationContext.xml的配置文件,编写具体的配置信息。

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.or...
    http://www.springframework.or...
    http://www.springframework.or...
    http://www.springframework.or...
    http://www.springframework.or...
    http://www.springframework.or...
    http://www.springframework.or...d">

     
     



       class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     
     
     
     



     



     




     













    三、Spring整合SpringMVC框架
    1、搭建和测试SpringMVC的开发环境

  2. 在web.xml中配置DispatcherServlet前端控制器

     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    


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
/*


2、测试SpringMVC的框架搭建是否成功

  1. 编写index.jsp和list.jsp编写,超链接
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>


    查询所有!


    查询所有
    测试查询

    测试包

姓名:
金额:



<%@ 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}




2、创建AccountController类,编写方法,进行测试
package com.mingqi.controller;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
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;
@Controller
@RequestMapping("/account")
public class AccountController {

@Autowired
private IAccountService accountService;
/**
 * 查询所有数据
 * @return
 */


@RequestMapping("/findAll")
public String findAll(Model model)
{


    System.out.println("表现层查询所有");
    accountService.findAll();
    List list = accountService.findAll();
    model.addAttribute("list",list);
    return "list";
}
@RequestMapping("/save")
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException
{
    accountService.saveAccount(account);
    response.sendRedirect(request.getContextPath()+"/account/findAll");
    return;
}

}
配置tomcat,启动项目,进行测试
java开发框架之SSM整合框架_第2张图片

你可能感兴趣的:(javassm)