springMVC+spring+MyBatis(SSM)的简单配置

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架。

    其中:

    Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。

    SpringMVC分离了模型对象(Model)、视图(View)、控制器(Controller),这种分离让它们更容易进行定制。

    MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。  

    今天,就带领大家见证基础,进行SSM的学习。 

   在此之前呢,首先要引入的是系统分层如何分层呢

   表示层(UI):数据展现 / 操作界面,请求分发(总所周知的MVC 是表示层的架构思想)。

   持久层(服务层):封装业务逻辑处理。

   持久层(数据访问层):封装数据访问逻辑。

   而各层之间的关系呢,表示层通过接口调用业务层,业务层通过接口调用持久层。之所以引入系统分层的概念,主要的原因是:当下一层的实现发生改变的时候,并不会影响上一层。

   叙述的就是这些了,开始我们的项目了

   首先创建一个maven项目,格局如下:

                         springMVC+spring+MyBatis(SSM)的简单配置_第1张图片

1.pom.xml


  4.0.0
  com.ninemax
  springMVC-test2
  0.0.1-SNAPSHOT
  war
   
    
        UTF-8
        1.8
    
   
   
  
       
          org.springframework
          spring-webmvc
          4.1.2.RELEASE
      
      
        
            com.oracle
            ojdbc14
            10.2.0.3.0
        

        
            org.springframework
            spring-jdbc
            4.3.5.RELEASE
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            org.mybatis
            mybatis
            3.2.8
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
        
          javax.servlet
          jstl
          1.2
      
        
            junit
            junit
            3.8.2
        
   
View Code

2.User.java

package com.nine.entity;

public class User {

    private String name;
    private Integer age;
    private String gender;
    private Double salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", gender=" + gender + ", salary=" + salary + "]";
    }

}
View Code

3.UserDao.java

package com.nine.dao;

import java.util.List;

import com.nine.entity.User;

public interface UserDao {
    List findAll();
}
View Code

4.UserMapper.xml

  



   
   

View Code

5.UserService.java

package com.nine.service;

import java.util.List;

import com.nine.entity.User;

public interface UserService {

    List userList();
}
View Code

6.UserServiceImpl

package com.nine.service;

import java.util.List;

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

import com.nine.dao.UserDao;
import com.nine.entity.User;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public List userList() {
        List list = userDao.findAll();
        return list;
    }

}
View Code

7.UserController

package com.nine.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.nine.entity.User;
import com.nine.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/hello")
    public String hello(HttpServletRequest request) {
        List list = new ArrayList();
        try {
            list = userService.userList();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        request.setAttribute("list", list);
        return "hello";
    }
}
View Code

8.application.properties

spring.datasource.url=jdbc:oracle:thin:@10.21*.4.2*:1521:orcl
spring.datasource.username=*
spring.datasource.password=*
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

9.spring-mvc.xml   (这是springMVC重要的配置文件,千万不能忽略)


 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
        
    
    package="com.nine"/>
    
    
    
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
    
    
     class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        
        
        
        
     
    
     
     
     
      class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        
      
      
      
       class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          
          
       
View Code

10.记住了!还有一个地方 web.xml,不要忘了。



  springMVC-test2
 
        springmvc
        class>org.springframework.web.servlet.DispatcherServletclass>
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    

    
        springmvc
        /
    
    
View Code

接下来就是简单的一些静态页面了,这里我没有做太好的描述,只有一些简单的页面供大家参考

hello.jsp


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

    
        list
        
        
    
    
        
                    

列表

姓名 年龄 性别 薪资
${e.name} ${e.age } ${e.gender } ${e.salary}
View Code

error.jsp

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

   
   
                           系统繁忙,稍后重试
   
View Code

做到这里,基本上就算做完了,不过首先呢,在完成了之后,最好是先做一个测试类,看是否能够找出结果

                                       springMVC+spring+MyBatis(SSM)的简单配置_第2张图片

UserTest.java

package test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nine.dao.UserDao;
import com.nine.entity.User;

public class UserTest {

    @Test
    public void test1() {

        @SuppressWarnings("resource")
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
        UserDao dao = ac.getBean("userDao", UserDao.class);
        List list = dao.findAll();
        System.out.println(list);

    }
}
View Code

然后运行这个测试类,看是否正确,如果在控制台出现结果,恭喜你你已经完成了!

然后运行Tomcat,输入http://localhost:8081/springMVC-test2/hello

springMVC+spring+MyBatis(SSM)的简单配置_第3张图片

 

不知道大家完成的怎么样了,如果出现问题,可以在下面进行留言,我会为大家进行解答.

转载于:https://www.cnblogs.com/baiyp/p/7865827.html

你可能感兴趣的:(java,测试,数据库)