java 整合SSM框架

2021-05-06 07:30

阅读:420

标签:over   ace   前端   url   图片   代码   技术   解决   xmlns   

一、搭建环境

     1、创建数据库表和表结构

create table account(
id INT identity(1,1) primary key,
name varchar(20),
[money] DECIMAL
)

   2、创建maven的工程SSM,在pom.xml文件引入依赖

技术图片技术图片
xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  modelVersion>4.0.0modelVersion>
  groupId>com.mingqigroupId>
  artifactId>SSMartifactId>
  version>1.0-SNAPSHOTversion>
  packaging>warpackaging>
  name>SSM Maven Webappname>
  
  url>http://www.example.comurl>
  properties>
    project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    maven.compiler.source>1.7maven.compiler.source>
    maven.compiler.target>1.7maven.compiler.target>
    spring.version>5.0.2.RELEASEspring.version>
    slf4j.version>1.6.6slf4j.version>
    log4j.version>1.2.12log4j.version>
    mysql.version>5.1.6mysql.version>
    mybatis.version>3.4.5mybatis.version>
  properties>
  dependencies>
    
    dependency>
      groupId>org.aspectjgroupId>
      artifactId>aspectjweaverartifactId>
      version>1.6.8version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-aopartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-contextartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webmvcartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-testartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-txartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-jdbcartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>junitgroupId>
      artifactId>junitartifactId>
      version>4.12version>
      scope>compilescope>
    dependency>
    dependency>
      groupId>com.microsoft.sqlservergroupId>
      artifactId>sqljdbc4artifactId>
      version>4.0version>
    dependency>
    dependency>
      groupId>javax.servletgroupId>
      artifactId>servlet-apiartifactId>
      version>2.5version>
      scope>providedscope>
    dependency>
    dependency>
      groupId>javax.servlet.jspgroupId>
      artifactId>jsp-apiartifactId>
      version>2.0version>
      scope>providedscope>
    dependency>
    dependency>
      groupId>jstlgroupId>
      artifactId>jstlartifactId>
      version>1.2version>
    dependency>
    
    dependency>
      groupId>log4jgroupId>
      artifactId>log4jartifactId>
      version>${log4j.version}version>
    dependency>
    dependency>
      groupId>org.slf4jgroupId>
      artifactId>slf4j-apiartifactId>
      version>${slf4j.version}version>
    dependency>
    dependency>
      groupId>org.slf4jgroupId>
      artifactId>slf4j-log4j12artifactId>
      version>${slf4j.version}version>
    dependency>
    
    dependency>
      groupId>org.mybatisgroupId>
      artifactId>mybatisartifactId>
      version>${mybatis.version}version>
    dependency>
    dependency>
      groupId>org.mybatisgroupId>
      artifactId>mybatis-springartifactId>
      version>1.3.0version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-txartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>c3p0groupId>
      artifactId>c3p0artifactId>
      version>0.9.1.2version>
      type>jartype>
      scope>compilescope>
    dependency>
  dependencies>
  build>
    finalName>SSMfinalName>
    pluginManagement>
      plugins>
        plugin>
          artifactId>maven-clean-pluginartifactId>
          version>3.1.0version>
        plugin>
        
        plugin>
          artifactId>maven-resources-pluginartifactId>
          version>3.0.2version>
        plugin>
        plugin>
          artifactId>maven-compiler-pluginartifactId>
          version>3.8.0version>
        plugin>
        plugin>
          artifactId>maven-surefire-pluginartifactId>
          version>2.22.1version>
        plugin>
        plugin>
          artifactId>maven-war-pluginartifactId>
          version>3.2.2version>
        plugin>
        plugin>
          artifactId>maven-install-pluginartifactId>
          version>2.5.2version>
        plugin>
        plugin>
          artifactId>maven-deploy-pluginartifactId>
          version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>
View Code

 3、部署ssm_web的项目,只要把ssm_web项目加入到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 +
                ‘}‘;
    }
}
View Code

 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框架代码的编写

    1. 搭建和测试Spring的开发环境

 在webapp项目中创建applicationContext.xml的配置文件,编写具体的配置信息。

技术图片技术图片
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
       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.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    context:component-scan base-package="com.mingqi">
        
        context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/>
        property name="username" value="sa"/>
        property name="password" value="123sa"/>
    bean>
    
    bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        property name="dataSource" ref="dataSource" />
    bean>
    
    bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        property name="basePackage" value="com.mingqi.dao"/>
    bean>
    
    
    bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        property name="dataSource" ref="dataSource" />
    bean>
    
    tx:advice id="txAdvice" transaction-manager="transactionManager">
    tx:attributes>
    tx:method name="find*" read-only="true"/>
    tx:method name="*" isolation="DEFAULT"/>
    tx:attributes>
    tx:advice>
    aop:config>
    aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.mingqi.service..ServiceImpl.*(..))"/>
    aop:config>
beans>
View Code

三、Spring整合SpringMVC框架

   1、搭建和测试SpringMVC的开发环境

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

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

web-app>
  display-name>Archetype Created Web Applicationdisplay-name>

  
  listener>
    listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  context-param>
    param-name>contextConfigLocationparam-name>
    param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  context-param>
    param-name/>
    param-value/>
  context-param>
  
  servlet>
    servlet-name>dispatcherServletservlet-name>
    servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    init-param>
      param-name>contextConfigLocationparam-name>
      param-value>classpath:springmvc.xmlparam-value>
    init-param>
    
    load-on-startup>1load-on-startup>
  servlet>
  servlet-mapping>
    servlet-name>dispatcherServletservlet-name>
    url-pattern>/url-pattern>
  servlet-mapping>
  
  filter>
    filter-name>characterEncodingFilterfilter-name>
    filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    init-param>
      param-name>encodingparam-name>
      param-value>UTF-8param-value>
    init-param>
  filter>
  filter-mapping>
    filter-name>characterEncodingFilterfilter-name>
    url-pattern>/*url-pattern>
  filter-mapping>

web-app>

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

     编写index.jsp和list.jsp编写,超链接

@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
body>
h2>查询所有!h2>
a href="account/findAll">查询所有a>
a href="account/findAll">测试查询a>
h3>测试包h3>

form action="account/save" method="post">
    姓名:input type="text" name="name" />br/>
    金额:input type="text" name="money" />br/>
    input type="submit" value="保存"/>br/>
form>
body>
html>
技术图片技术图片
@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
html>
head>
    title>Titletitle>
head>
body>
h3>查询所有的帐户h3>
c:forEach items="${list}" var="account">
    ${account.name}
c:forEach>
body>
html>
View Code

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;
    }
}
View Code

技术图片

 

 技术图片

 

java 整合SSM框架

标签:over   ace   前端   url   图片   代码   技术   解决   xmlns   

原文地址:https://www.cnblogs.com/mingqi-420/p/13190577.html


评论


亲,登录后才可以留言!