天天看點

java 快速建構ssm項目_SSM快速搭建

基本環境搭建

1、 建立Maven工程

2、 導入項目相關依賴的jar包

spring

springmvc

mybatis

mysql

junit

spring-test

lombok

devtools

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

ssm-crud

1.0-SNAPSHOT

org.springframework

spring-webmvc

5.2.8.RELEASE

org.springframework

spring-aspects

5.2.8.RELEASE

org.springframework

spring-jdbc

5.2.8.RELEASE

org.springframework

spring-aop

5.2.8.RELEASE

org.springframework

spring-tx

5.2.8.RELEASE

com.fasterxml.jackson.core

jackson-databind

2.11.2

org.mybatis

mybatis

3.5.0

org.mybatis

mybatis-spring

2.0.5

mysql

mysql-connector-java

8.0.21

com.alibaba

druid

1.1.24

javax.servlet.jsp

javax.servlet.jsp-api

2.3.3

provided

jstl

jstl

1.2

javax.servlet

javax.servlet-api

4.0.1

provided

junit

junit

4.12

test

org.springframework

spring-test

5.2.8.RELEASE

test

org.mybatis.generator

mybatis-generator-core

1.4.0

com.github.pagehelper

pagehelper

5.1.10

com.github.jsqlparser

jsqlparser

3.1

org.projectlombok

lombok

1.18.12

provided

org.springframework.boot

spring-boot-devtools

1.5.6.RELEASE

true

3、 引用bootstrap前端架構

4、 編寫ssm相關配置檔案

web.xml

spring-mvc.xml

appicationContext.xml

spring-mybatis.xml

jdbc.properties

4.1、web.xml

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_4_0.xsd"

version="4.0">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

com.example.demo.Listener.MyContextListener

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

dispatcherServlet

/

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceRequestEncoding

true

forceResponseEncoding

true

characterEncodingFilter

/*

hiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

hiddenHttpMethodFilter

/*

4.2、spring-mvc.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

4.3、appicationContext.xml

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"

xmlns:tx="http://www.springframework.org/schema/tx"

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

4.4、spring-mybatis.xml

/p>

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4.5、jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

jdbc.username=root

jdbc.password=12345678

4.6、log4j.properties

#############

# 輸出到控制台

#############

# log4j.rootLogger日志輸出類别和級别:隻輸出不低于該級别的日志資訊DEBUG < INFO < WARN < ERROR < FATAL

# WARN:日志級别 CONSOLE:輸出位置自己定義的一個名字 logfile:輸出位置自己定義的一個名字

log4j.rootLogger=WARN,CONSOLE,logfile

# 配置CONSOLE輸出到控制台

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# 配置CONSOLE設定為自定義布局模式

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

# 配置CONSOLE日志的輸出格式 [frame] 2019-08-22 22:52:12,000 %r耗費毫秒數 %p日志的優先級 %t線程名 %C所屬類名通常為全類名 %L代碼中的行号 %x線程相關聯的NDC %m日志 %n換行

log4j.appender.CONSOLE.layout.ConversionPattern=[frame] %d{yyyy-MM-dd HH:mm:ss,SSS} - %-4r %-5p [%t] %C:%L %x - %m%n

################

# 輸出到日志檔案中

################

# 配置logfile輸出到檔案中 檔案大小到達指定尺寸的時候産生新的日志檔案

log4j.appender.logfile=org.apache.log4j.RollingFileAppender

# 儲存編碼格式

log4j.appender.logfile.Encoding=UTF-8

# 輸出檔案位置此為項目根目錄下的logs檔案夾中

log4j.appender.logfile.File=logs/root.log

# 字尾可以是KB,MB,GB達到該大小後建立新的日志檔案

log4j.appender.logfile.MaxFileSize=10MB

# 設定滾定檔案的最大值3 指可以産生root.log.1、root.log.2、root.log.3和root.log四個日志檔案

log4j.appender.logfile.MaxBackupIndex=3

# 配置logfile為自定義布局模式

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n

##########################

# 對不同的類輸出不同的日志檔案

##########################

# club.bagedate包下的日志單獨輸出

log4j.logger.club.bagedate=DEBUG,bagedate

# 設定為false該日志資訊就不會加入到rootLogger中了

log4j.additivity.club.bagedate=false

# 下面就和上面配置一樣了

log4j.appender.bagedate=org.apache.log4j.RollingFileAppender

log4j.appender.bagedate.Encoding=UTF-8

log4j.appender.bagedate.File=logs/bagedate.log

log4j.appender.bagedate.MaxFileSize=10MB

log4j.appender.bagedate.MaxBackupIndex=3

log4j.appender.bagedate.layout=org.apache.log4j.PatternLayout

log4j.appender.bagedate.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n

5、工具類

5.1.1 Msg(封裝傳回資訊的對象)

@Data

public class Msg {

//100代表成功,200代表失敗

private int code;

private String message;

private Map data;

public Msg() {

data = new HashMap<>();

}

public static Msg success() {

Msg msg = new Msg();

msg.setCode(100);

msg.setMessage("處理成功!");

return msg;

}

public static Msg error() {

Msg msg = new Msg();

msg.setCode(200);

msg.setMessage("處理失敗!");

return msg;

}

public Msg addData(String s, Object o) {

data.put(s, o);

return this;

}

}

6、mybatis generator(mybatis逆向工程)

6.1、配置檔案

/p>

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"

userId="root"

password="12345678">

6.2、生成檔案的類

public class Generator {

@Test

public void generator() throws Exception {

List warnings = new ArrayList();

// 指定配置檔案

File configFile = new File("F:/MyProjects/complete/ssm-crud/generatorConfig.xml");

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config = cp.parseConfiguration(configFile);

DefaultShellCallback callback = new DefaultShellCallback(true);

MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

myBatisGenerator.generate(null);

}

}

遇到的坑

坑一

c3p0-0.9.5.2 jar包缺少其它依賴

解決辦法:添加mchange-commons-java-0.2.20 jar包(注意jar包版本)

com.mchange

mchange-commons-java

0.2.20

坑二

pagehelper-5.1.10 jar包缺少其它依賴

解決方法:添加jsqlparser-3.1 jar包(注意jar包版本)

com.github.jsqlparser

jsqlparser

3.1

坑三

關閉tomcat時報錯

錯誤提示:Web應用程式[ROOT]似乎啟動了一個名為[C3P0PooledConnectionPoolManager[identityToken->31usy7ac1ocyx2d1tj3xmv|630da823]-AdminTaskTimer]的線程,但未能停止它。這很可能會造成記憶體洩漏。

解決方法: 換資料源druid

com.alibaba

druid

1.1.24

坑四

關閉tomcat時報錯

錯誤提示:為防止記憶體洩漏,JDBC驅動程式已被強制取消注冊。

解決方法:配置監聽器jie

com.example.demo.config.listener.MyContextListener

public class MyContextListener implements ServletContextListener {

@Override

public void contextInitialized(ServletContextEvent arg0) {

System.out.println("webService start");

}

@Override

public void contextDestroyed(ServletContextEvent arg0) {

System.out.println("webService stop");

try {

while (DriverManager.getDrivers().hasMoreElements()) {

DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());

}

System.out.println("jdbc Driver close");

AbandonedConnectionCleanupThread.checkedShutdown();

System.out.println("clean thread success");

} catch (SQLException e) {

e.printStackTrace();

}

}

}

坑五

使用lombok插件,編寫實體類時,某成員變量的第二個字母為大寫,導緻springmvc無法接收前台傳來的參數

解決方法:自己編寫setter/getter

private Integer dId;

//解決dId無法接收參數的問題,下面的命名方式是JavaBean規範命名

public Integer getdId() {

return dId;

}

public void setdId(Integer dId) {

this.dId = dId;

}