SSM框架+slf4j 以Gradle实现
环境:win10+jdk8+tomcat9+Intellij IDEA
首先,作为一个喜欢偷懒的人,管理jar之类的的事情太累,所以用了Gradle项目管理器
第一步:
新建一个gradle-web项目,目录结构为

在build.gradle中添加
group 'com.pigge'
version '1.0-SNAPSHOT' apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea' sourceCompatibility = 1.8 repositories {
mavenLocal()
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",)
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'
}
在resource下新建database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/StudentMS?characterEncoding=utf8&useSSL=true
jdbc.username=root
jdbc.password=123456
再新建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="/"/>
<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>
在修改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>
</web-app>
再在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="info" 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="info" includeLocation="true">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
</root>
</Loggers>
</Configuration>
然后,在entity包下建立实体类
public class Student {
private Integer id;
private String name;
private String sex;
private LocalDate 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 LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
在mapper包下建立StudentMapper.java(dao层,有的喜欢取名为**Dao类)
public interface StudentMapper {
@Select("select * from student where id = #{id}")
Student selectOne(Integer id);
}
在resource文件夹下建立与mapper包相同的路径,再新建StudentMapper.xml(否则StudentMapper.java与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>
空的什么方法也没有,测试时只调用StudentMapper.java的抽象方法
在service下新建StudentService
public interface StudentService {
Student selectOne(Integer id);
}
再在service下的impl包新建StudentServiceImpl
@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);
}
}
在controller下建立StudentController.java
@Controller
public class StudentController {
private StudentService studentService; @RequestMapping("show")
public String show(ModelMap map,HttpServletRequest request){
map.put("student",studentService.selectOne(1));
return "success";
} public StudentService getStudentService() {
return studentService;
} @Autowired
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
}
在新建一个success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.student}
</body>
</html>
将项目加载到Tomcat,执行

好了,测试已经成功了
最后在附上码云地址:https://gitee.com/hjm0928/SSM
SSM框架+slf4j 以Gradle实现的更多相关文章
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...
- [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World
来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral ...
- Eclipse使用Maven创建Web项目+整合SSM框架
一.准备环境: maven:apache-maven-3.2.3 jdk:jdk1.8.0_25 tomcat:tomcat-9.0 二.配置Maven.jdk 1.Window——>Prefe ...
- 使用idea2017搭建SSM框架
搭建个SSM框架居然花费了我好长时间!特此记录! 需要准备的环境: idea 2017.1 jdk1.8 Maven 3.3.9 请提前将idea与Maven.jdk配置好,本次项目用的都是比较新的 ...
- 整合最优雅SSM框架:SpringMVC + Spring + MyBatis
我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...
- ssm框架整合+maven项目创建
在引入外部maven插件后就可以创建一个maven项目了,这篇文章主要介绍ssm框架的整合和如何创建一个maven项目 1.在开发工具的项目空白区单击右键,依次选择New.Other,会出现如下界面, ...
- 最优雅SSM框架:SpringMVC + Spring + MyBatis
在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...
- 【原】无脑操作:eclipse + maven搭建SSM框架
网上看到一些Spring + Spring MVC + MyBatis框架的搭建教程,不是很详细或是时间久远了,自己动手整一个简单无脑的! 0.系统环境 1)Windows 10 企业版 2)JDK ...
- ssm框架的搭建实现CRUD的操作
最近在开发公司的一个系统,系统的框架是用ssm的框架搭建的,当然和这次写博客的不一样,它拥有很多的配置文件,企业级的开发所需要的配置文件是非常繁琐的,今天记录一下一个简单的SSM框架的搭建和实现一个C ...
随机推荐
- java从控制台接收一个数字
//时间:2017/7/22//作者:江骆//功能:从控制台接收一个数import java.io.*; //引入一个IO流的包public class helloworld1{ public ...
- Linux下的定时任务 - Cron服务
最近搞咕自己的笔记系统,虽然现在是个人的使用,对于数据库的数据还是比较少,但是安全还是一个我必须注意的东西. (特别是前段时间中了比特币的病毒之后,更是让我关注了我的主机的安全的问题.) 今天的随记是 ...
- SAP高可用性(HA)
1.SAP系统高可用的要求 高可用性是从终端用户的角度来需求,及要求最大化系统的可用性.其目的是降低意外系统关闭时间(服务器生效.存储失效.操作系统失败--),减少预期系统关闭时间(系统及架构的维护. ...
- centOS 7一个解决“network.service: control process exited, code=exited status=1”方法
今天早上2017-08-04,我打开虚拟机,使用远程工具xshell对虚拟机进行连接,我发现连接不上去,然后我ifconfig,发现找不到ens33了,就剩一个本地回环,看来是我的网络出现了问题,然后 ...
- Linux下识别分区文件系统类型
Linux下挂载文件系统有时候需要填写文件系统.但有的设备拿到手还不知道文件系统,这种情况,可以用 parted命令 # parted /dev/vda GNU Parted 3.2 Using /d ...
- 业余草通告CSDN博客用户zhang__ao非法转载文章的公告
今天早上有粉丝给我反馈,CSDN的一位用户大量非法的转载了我的个人网站:业余草(www.xttblog.com)上的大量文章.现一对该用户转载业余草上网站上的所有文章进行了举报! 从上图中可以看出,该 ...
- Netty4 学习笔记之一:客户端与服务端通信 demo
前言 因为以前在项目中使用过Mina框架,感受到了该框架的强大之处.于是在业余时间也学习了一下Netty.因为Netty的主要版本是Netty3和Netty4(Netty5已经被取消了),所以我就直接 ...
- mysql更新某个字符串字段的部分内容
如果现在需要Mysql更新字段重部分数据,而不是全部数据,应该采用何种方法呢?下面介绍了两种情况下Mysql更新字段中部分数据的方法,供您参考. Mysql更新字段中部分数据第一种情况: update ...
- Hive基础(4)---Hive的内置服务
版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] 一:Hive的几种内置服务 ...
- 笨鸟先飞之ASP.NET MVC系列之过滤器(01过滤器简介)
过滤器 什么是过滤器? 过滤器(Filter) 主要的作用大致可以理解为把我们的附加逻辑注入到MVC框架的请求处理. 在ASP.NET MVC的请求处理中一种有19个管道事件分别是 BeginRequ ...