通过gradle搭建SSM框架+slf4j 以Gradle实现

1、eclipse 安装完gradle插件后,启动后提示 gradle安装插件教程
2、创建gradle项目
这里写图片描述
3、下一步
这里写图片描述
4、输入项目名,下一步
这里写图片描述
5、选择自己Windows系统的gradle路径( windows 下载、配置gradle),其他不填,下一步
这里写图片描述
6、最后finish。这里写图片描述
7、项目结构图
这里写图片描述

8、修改build.gradle 添加项目依赖的jar包

group 'com.pigge'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'idea'

sourceCompatibility = 1.7

repositories {
    mavenLocal()
    maven{
        url "http://maven.aliyun.com/nexus/content/groups/public/"
    }
    //mavenCentral()
}
//让resource文件自动加载到class路径下
idea{
    module{
        inheritOutputDirs=true;
    }
}
dependencies {
    def springVersion = "4.3.8.RELEASE"
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile("org.springframework:spring-aop:$springVersion",
            "org.springframework:spring-orm:$springVersion",
            "org.springframework:spring-jdbc:$springVersion",
            "org.springframework:spring-core:$springVersion",
            "org.springframework:spring-context:$springVersion",
            "org.springframework:spring-beans:$springVersion",
            "org.springframework:spring-tx:$springVersion",
            "org.springframework:spring-web:$springVersion",
            "org.springframework:spring-webmvc:$springVersion",
            "org.slf4j:slf4j-api:1.7.21",)
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.40'
    compile(group: 'org.mybatis', name: 'mybatis', version: '3.4.5',)
    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
    compile group: 'javax', name: 'javaee-api', version: '7.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.2'
    compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.2'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.10'
    compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.2'
}

这里提供gradle相关依赖包引入和配置相关文章:
Gradle2.0用户指南翻译——第七章. Java 快速入门
9、两种方法去gradle构建项目

第一种:右键build.gradle 刷新整个gradle项目,下载所需的jar包
这里写图片描述

第二种:进入eclipse工作环境SSM项目目录下 通过gradle eclipse 命令去构建项目
gradle eclipse命令是由于在build.gradle文件中添加了

apply plugin: ‘eclipse’

语句,即可使用gradle eclipse 构建方式
这里写图片描述
12.至此一个gradle项目构建完毕
13.修改gradle项目的文件目录结构,并修改为javaWeb项目
目录结构
修改为javaWeb项目
右键项目——Properties设置Project Facts
这里写图片描述
这里写图片描述
OK保存
14. 在resources下新建database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123
#定义初始连接数  
initialSize=1 
#定义最大连接数  
maxActive=20 
#定义最大空闲  
maxIdle=20
#定义最小空闲  
minIdle=1  
#定义最长等待时间  
maxWait=60000 

15.再新建spring-config.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"
       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">
    <!--配置Spring的扫描路径-->
    <context:component-scan base-package="com.pigge.controller"/>
    <context:component-scan base-package="com.pigge.service"/>
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:database.properties"/>
    <!--配置springmvc解析式-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--配置mybatis的sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/pigge/mapper/*.xml"/>
    </bean>
    <!--使用spring自动装配mapper类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.pigge.mapper"/>
    </bean>
</beans>

16.再在resource下新建log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off" monitorInterval="1800">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        </Console>
        <RollingFile name="RollingFileInfo" fileName="${web:rootDir}/WEB-INF/logs/info.log"
                     filePattern="${web:rootDir}/WEB-INF/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="debugger" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <root level="debugger" includeLocation="true">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
        </root>
    </Loggers>
</Configuration>

17.再修改webapp.WEB-INF下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--配置log4j2,是它可以识别${web:rootDir}-->
    <context-param>
        <param-name>log4jContextName</param-name>
        <param-value>star</param-value>
    </context-param>
    <!--加载spring-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param><!--加载配置文件-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-config.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

19.在entity包下建立实体类Student.java

package com.pigge.entity;

import java.util.Date;

public class Student {
     private Integer id;
        private String name;
        private String sex;
        private Date birthday;

        public Integer getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

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

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", birthday=" + birthday +
                    '}';
        }
}

20.在mapper包下建立StudentMapper.java(dao层)

package com.pigge.mapper;

import org.apache.ibatis.annotations.Select;

import com.pigge.entity.Student;

public interface StudentMapper {
    @Select("select * from student where id = #{id}")
    Student selectOne(Integer id);
}

21.在resource文件夹下建立与mapper包相同的路径,再新建StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pigge.mapper.StudentMapper">

</mapper>

22.在service下新建StudentService

package com.pigge.service;

import com.pigge.entity.Student;

public interface StudentService {
    Student selectOne(Integer id);
}

23.再在service下的impl包新建StudentServiceImpl

package com.pigge.service.impl;

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

import com.pigge.entity.Student;
import com.pigge.mapper.StudentMapper;
import com.pigge.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {

     private StudentMapper studentMapper;

        public StudentMapper getStudentMapper() {
            return studentMapper;
        }

        @Autowired
        public void setStudentMapper(StudentMapper studentMapper) {
            this.studentMapper = studentMapper;
        }

        @Override
        public Student selectOne(Integer id) {
            return studentMapper.selectOne(1);
        }
}

24.在controller下建立StudentController.java

package com.pigge.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pigge.service.StudentService;

@Controller
public class StudentController {

     private StudentService studentService;
     private static final org.slf4j.Logger logger = LoggerFactory.getLogger(StudentController.class);
        @RequestMapping("show")
        public String show(ModelMap map,HttpServletRequest request){
            map.put("student",studentService.selectOne(1));
            System.out.println("student={}"+studentService.selectOne(1));
            logger.info("student={}",studentService.selectOne(1));
            return "success";
        }

        public StudentService getStudentService() {
            return studentService;
        }

        @Autowired
        public void setStudentService(StudentService studentService) {
            this.studentService = studentService;
        }
}

25.再在webapp.WEB-INF.jsp文件夹里新建一个success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${requestScope.student}
</body>
</html>

26.将项目加载到Tomcat,浏览器访问

http://localhost:8080/SSM/show

这里写图片描述

SSM框架代码下载地址: 通过gradle搭建SSM框架+slf4j 以Gradle实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值