博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSM框架+slf4j 以Gradle实现
阅读量:5142 次
发布时间:2019-06-13

本文共 5875 字,大约阅读时间需要 19 分钟。

环境: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.8repositories {    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.Driverjdbc.url=jdbc:mysql://localhost:3306/StudentMS?characterEncoding=utf8&useSSL=truejdbc.username=rootjdbc.password=123456

再新建spring-config.xml

在修改web.xml

log4jContextName
star
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-config.xml
springmvc
/

再在resource下新建log4j2.xml文件

然后,在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不在同一文件夹下,容易出错)

空的什么方法也没有,测试时只调用StudentMapper.java的抽象方法

在service下新建StudentService

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

再在service下的impl包新建StudentServiceImpl

@Servicepublic 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

@Controllerpublic 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" %>    Title    ${requestScope.student}
 

将项目加载到Tomcat,执行

好了,测试已经成功了

最后在附上码云地址:https://gitee.com/hjm0928/SSM

转载于:https://www.cnblogs.com/hjm0928/p/7745421.html

你可能感兴趣的文章
? blog. b树 b+树 (hasn)与 海量 学习笔记
查看>>
Linux常用命令笔记总结(待补充)
查看>>
爬虫程序cookie和proxy的解决方法
查看>>
使用set()求出列表交集 分类: python基础学习 ...
查看>>
[SCOI 2016]美味
查看>>
[SDOI 2016]征途
查看>>
uboot移植
查看>>
JavaScript基本编码模式
查看>>
ACCESS和MSSQL-如何随机读取数据库记录
查看>>
用canvas给自己的博客园加背景(一)
查看>>
intellij maven项目加载慢
查看>>
bzoj 1485 : [HNOI2009]有趣的数列
查看>>
爬虫框架之scrapy
查看>>
turtle库基础练习
查看>>
Linux设备文件简介(转载)
查看>>
JavaScript.内置对象
查看>>
文件操作
查看>>
hadoop集群中动态添加节点
查看>>
C# 中在对象后面跟“?” 以及在类型后面跟问号
查看>>
RuntimeError: implement_array_function method already has a docstring
查看>>