IntelliJ IDEA整合SSM框架

SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

1. 创建maven项目

1.1 File–>New Module,进入创建项目窗口

IntelliJ IDEA整合SSM框架_第1张图片

1.2 点击Next,填写GroupId、ArtifactId和Version

IntelliJ IDEA整合SSM框架_第2张图片

1.3 在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。

IntelliJ IDEA整合SSM框架_第3张图片

1.4 填写Module name

IntelliJ IDEA整合SSM框架_第4张图片

1.5 生成maven的项目骨架之后,我们还需要手动在 src/main 下创建 java目录。完整的项目结构如下图

IntelliJ IDEA整合SSM框架_第5张图片

1.6 这里的目录建好之后还需要设置一下,让idea识别目录作用,选择File-Project Structure

IntelliJ IDEA整合SSM框架_第6张图片

到此为止,项目的框架基本搭建完成

2. 项目配置文件

2.1 这里使用maven来引入项目所需要的jar包,所以也就不需要手动来管理jar包了

pom.xml


4.0.0
com.web
ssm
war
1.0-SNAPSHOT
ssm Maven Webapp
http://maven.apache.org


    
    UTF-8
    UTF-8
    
    4.3.5.RELEASE
    
    3.4.1




    
    
        javax
        javaee-api
        7.0
    

    
    
        junit
        junit
        4.12
    

    
    
        ch.qos.logback
        logback-classic
        1.2.2
    

    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.8.7
    


    
    
        mysql
        mysql-connector-java
        5.1.41
        runtime
    

    
    
        com.mchange
        c3p0
        0.9.5.2
    

    
    
        org.mybatis
        mybatis
        ${mybatis.version}
    

    
    
        org.mybatis
        mybatis-spring
        1.3.1
    

    
    
        org.springframework
        spring-core
        ${spring.version}
    
    
        org.springframework
        spring-beans
        ${spring.version}
    
    
        org.springframework
        spring-context
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-tx
        ${spring.version}
    
    
        org.springframework
        spring-web
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
        org.springframework
        spring-test
        ${spring.version}
    




    ssm
    
        
            org.apache.maven.plugins
            maven-compiler-plugin
            
                
                1.8
                1.8
            
        
    



2.2 这里可以控制输出格式和内容,有兴趣的可以自己设置

logback.xml



    
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        
    
    
        
    

2.3 新建jdbc.properties文件

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8&useSSL=false
#用户名
jdbc.username=root
#密码
jdbc.password=201314
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2

2.4 新建log4j配置文件

log4j.properties

log4j.rootLogger=INFO,Console,File  

#控制台日志  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH:mm:ss}][%C] - %m%n  

#普通文件日志  
log4j.appender.File=org.apache.log4j.RollingFileAppender  
log4j.appender.File.File=logs/ssm.log  
log4j.appender.File.MaxFileSize=10MB  
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志  
log4j.appender.File.Threshold=ALL  
log4j.appender.File.layout=org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH:mm:ss}][%C] - %m%n

2.5 新建spring和mybatis整合所需的配置文件

spring-mybatis.xml












    
    
    
    
    
    
    
    
    




    
    
    
    
    
    




    
    
    
    




    
    






2.6 新建springmvc配置文件

spring-mvc.xml















    
    
    



2.7 新建web.xml配置文件

web.xml




ssm
SSM_Alpha_0.0.1



    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        UTF-8
    


    encodingFilter
    /*




    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
        contextConfigLocation
        classpath:spring-*.xml
    
    1
    true


    SpringMVC
    
    /



    index.html



3.0 测试

3.1 创建数据库表

3.2 配置类

User

package com.ssm.model;

public class User {

private Integer id;
private String username;
private String password;


public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            '}';
}
}

IUserDao

package com.ssm.dao;

import com.ssm.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class IUserDaoTest {

    @Autowired
    private IUserDao dao;

    @Test
    public void testSelectUser(){
        Integer id = 1;
        User user = dao.selectUser(id);
        System.out.println(user);

    }
}

UserDao.xml










IUserService

package com.ssm.service;

import com.ssm.model.User;

public interface IUserService {

    User selectUser(Integer userId);
}

UserServiceImpl

package com.ssm.service.impl;

import com.ssm.dao.IUserDao;
import com.ssm.model.User;
import com.ssm.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements IUserService{
@Resource
private IUserDao userDao;

public User selectUser(Integer userId){
    return this.userDao.selectUser(1);
}

}

UserController

package com.ssm.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ssm.model.User;
import com.ssm.service.IUserService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

@Resource
private IUserService userService;

@RequestMapping("/showUser.do")
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    Integer userId = Integer.parseInt(request.getParameter("id"));
    User user = this.userService.selectUser(userId);
    ObjectMapper mapper = new ObjectMapper();
    response.getWriter().write(mapper.writeValueAsString(user));

}
}

IUserDaoTest(测试mybatis)

package com.ssm.dao;

import com.ssm.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class IUserDaoTest {

@Autowired
private IUserDao dao;

@Test
public void testSelectUser(){
    Integer id = 1;
    User user = dao.selectUser(id);
    System.out.println(user);

}
}

3.3 新建个页面来测试springmvc和mybatis

index.html





test



Hello World!

3 在服务器上部署项目

3.1 添加项目

File–>Project Structure–>Artifacts–> + –>Web Application:Exploded–>Form Modules
IntelliJ IDEA整合SSM框架_第7张图片

3.2 添加服务器

run–>Edit Configurations–> + –>TomcatServer
IntelliJ IDEA整合SSM框架_第8张图片

3.3 部署项目

IntelliJ IDEA整合SSM框架_第9张图片

4 运行项目

以DeBug模式运行项目

你可能感兴趣的:(后端)