Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)

其实idea和eclipse搭建ssm框架基本上相同,但是还是有些许的区别

  • 首选我们建立一个maven项目
    Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)_第1张图片
    maven项目名称等信息,自己填写好就行

  • 来看看我们的项目结构,你可以像我一样建立起文件夹和文件的结构
    Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)_第2张图片
    结构很清晰,我们慢慢搭建我们的ssm项目

  • 我们打开项目设置在web项里,点击右边加号,让它获取我们的web.xml位置
    Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)_第3张图片
    现在我们来写上面建立的文件里面的代码

  • pom.xml



    4.0.0

    top.maniy
    ssm_maven_demo
    1.0-SNAPSHOT
    war

    
        
            com.alibaba
            druid
            1.0.9
        
        
            mysql
            mysql-connector-java
            5.1.32
        
        
            org.springframework
            spring-webmvc
            4.1.3.RELEASE
        
        
            org.springframework
            spring-context
            4.1.3.RELEASE
        
        
            org.springframework
            spring-beans
            4.1.3.RELEASE
        

        
            org.springframework
            spring-jdbc
            4.1.3.RELEASE
        
        
            org.springframework
            spring-aspects
            4.1.3.RELEASE
        
        
            org.springframework
            spring-jms
            4.1.3.RELEASE
        
        
            org.springframework
            spring-context-support
            4.1.3.RELEASE
        
        
        
            org.mybatis
            mybatis
            3.2.8
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
        
            com.github.miemiedev
            mybatis-paginator
            1.2.15
        
        
            com.github.pagehelper
            pagehelper
            3.2.1
        
        
        
            org.apache.commons
            commons-lang3
            3.3.2
        
        
            org.apache.commons
            commons-io
            1.3.2
        
        
            commons-net
            commons-net
            3.3
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.4.2
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.slf4j
            slf4j-log4j12
            1.6.4
        
        
        
            jstl
            jstl
            1.2
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
            javax.servlet
            jsp-api
            2.0
            provided
        
    

    
        ${project.artifactId}
        
                       
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
        
        


    

  • applicationContext-dao.xml


	
	
	
		
	
	
		
		
		
		
	
	
	
	
		
		
		
		
		
		
        
        

    

	
	
        
        

        
		

	


    
    
        
        
    

    
    



  • applicationContext-service.xml


	
	
	



  • springmvc.xml


	
	

	
	
	
	
	

	

	
	
		

		
		
		
		
	

  • jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
  • SqlMapConfig.xml



	
	
		
	

  • web.xml



    

        index

    
    
    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

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


    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        
        
        
        1
    
    
        SpringMVC
        
        /
    
    
    
        SpringMVC
        
        /index
    


  • CustomerMapper.java
package top.maniy.demo.mapper;

import java.util.List;


import top.maniy.demo.entity.Customer;

public interface CustomerMapper {



	//获取全部用户
	public List findAllCustomer();
}

  • CustomerMapper.xml






	
	


  • CustomerService.java
package top.maniy.demo.service;

import top.maniy.demo.entity.Customer;

import java.util.List;



public interface CustomerService {

	


		//获取全部用户
		public List findAllCustomer();
}

  • CustomerServiceImpl.java
package top.maniy.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.maniy.demo.entity.Customer;
import top.maniy.demo.mapper.CustomerMapper;

/**
 * 客户管理
 * @author Admin
 *
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
		
	@Autowired
	private CustomerMapper customerMapper;
	
	//获取全部用户
	public List findAllCustomer() {
		return customerMapper.findAllCustomer();
	}
	
}

  • Customer.java
package top.maniy.demo.entity;

import java.util.Date;

public class Customer {

	private Long cust_id;
	private String cust_name;
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
}
  • CustomerController.java
package top.maniy.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.maniy.demo.entity.Customer;
import top.maniy.demo.service.CustomerService;

@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	@RequestMapping(value ="index")
	   public String index() {	
			return "index";
	   }
   @RequestMapping(value ="/customer/list")
   @ResponseBody
   public List list() {	
		return customerService.findAllCustomer();
   } 
}

  1. 代码就这么多了,还有一个数据库我想看实体类和jdbc.properties应该可以直接弄好

  2. 然后我们添加tomcat
    Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)_第4张图片

最后我们运行看看效果吧

完美运行(这个是表字段没有删之前的)。
Intellij idea通过maven搭建ssm框架(实现获取数据库数据显示在页面中)_第5张图片

如果要写junit单元测试的话要加上

@RunWith(SpringJUnit4ClassRunner.class)//帮我们创建容器
//指定创建容器是使用那些配置文件
@ContextConfiguration(locations = {"classpath*:/spring/applicationContext-dao.xml","classpath*:/spring/applicationContext-service.xml"})

  1. 项目源代码及sql文件
    https://download.csdn.net/download/imaniy/10582400

你可能感兴趣的:(ssm框架,IntelliJ,IDEA)