SSM项目:springMVC为控制器、spring 为事务层、 MyBatis 负责持久
首先看下集成后项目结构:
1、打开IntelliJ IDEA,点击 "File" -> "New" -> "Project"。
点击Finish,此时我们就已经创建了一个maven-web项目;
2、在src/main 下,创建 java和resources文件夹;
在java文件夹下:创建controller,service,dao,entity等文件夹。
在resources文件夹下创建mappers(存放实体类映射.xml文件)文件夹和配置文件。
applicationContext.xml --spring 容器相关配置;
dispatcherServlet.xml --springMvc 相关配置;
mybatis-config.xml --mybatis 相关配置;
generatorConfig.xml --自动生成Dao、Model层相关代码;
jdbc.properties --jdbc常量配置;
log4j.properties --集成log4j
3、在webapp/WEB-INF 下,创建views文件夹,存放静态资源;
web.xml --web-servlet相关配置;
4、pom文件相关依赖
4.0.0
org.example
web-ssm
1.0-SNAPSHOT
war
web-ssm Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
4.2.5.RELEASE
3.2.4
1.6.6
1.2.12
1.6.6
1.2.12
junit
junit
4.11
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
8.0.19
com.mchange
c3p0
0.9.5.5
junit
junit
4.11
test
javax.servlet
jstl
1.2
javax.servlet.jsp
jsp-api
2.2.1-b03
provided
javax.servlet
servlet-api
2.5
provided
com.fasterxml.jackson.core
jackson-core
2.7.1
com.fasterxml.jackson.core
jackson-annotations
2.7.1
com.fasterxml.jackson.core
jackson-databind
2.7.1
net.sf.json-lib
json-lib
2.4
jdk15
commons-lang
commons-lang
2.4
commons-logging
commons-logging
1.1
commons-pool
commons-pool
1.5.6
commons-dbcp
commons-dbcp
1.4
commons-beanutils
commons-beanutils
1.8.3
commons-httpclient
commons-httpclient
3.1
commons-collections
commons-collections
3.1
commons-codec
commons-codec
1.9
javax.annotation
jsr250-api
1.0
net.sf.ezmorph
ezmorph
1.0.6
javax.activation
activation
1.1
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
org.mybatis.generator
mybatis-generator-core
1.3.5
cglib
cglib
3.2.5
web-ssm
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
注*:
1*、本例MySQL Server 8.0
2*、使用 mybatis-generator-maven-plugin,在pom.xml中
web-ssm
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
5、applicationContext.xml 相关配置:
6、dispatcherServlet.xml 相关配置:
7、mybatis-config.xml 相关配置:
8、generatorConfig.xml 相关配置:
9、jdbc.properties 相关配置:
#jdbc.driver=com.mysql.jdbc.Driver
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testdata?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=yhuan
#最大连接数
maxPoolSize=20
#最小连接数
minPoolSize=10
#连接超时时间
checkoutTimeout=60000
#失败重连次数
acquireRetryAttempts=3
#初始化连接数量
initialPoolSize=10
10、log4j.properties 相关配置:
### set log levels ###
log4j.rootLogger = INFO,D
###输出到控制台###
log4j.logger.toConsole=debug,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n
### 输出到日志文件 ###
## 其中File 可以采用绝对路径 亦可以采用相对路径 catalina.home 是tomcat目录 如果没有logs文件夹 将会报错 ##
## 更加倾向于将日志文件放在 D:/database/log4j/log4jTest/logtest.log ##
## 绝对路径如:e:/logtest.log ##
log4j.logger.daily=INFO,D
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E:/log4j/log4jTest/logtest.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n
##log4j.logger.org.apache.ibatis=debug,stdout
##log4j.logger.java.sql=debug,stdout
11、web.xml 相关配置:
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:dispatcherServlet.xml
1
SpringMVC
/
12、使用mybatis-generator-maven-plugin:
右键→Run Maven Build,即可在generatorConfig.xml配置的路径下生成Dao,Model层代码。
13、项目添加tomcat启动:
14、新建TestController.java,启动tomcat,测试---
package org.example.controller;
import org.example.entity.Student;
import org.example.entity.User;
import org.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
@Controller
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/testQuery",produces = "text/html;charset=UTF-8")
@ResponseBody
public String testQuery(){
System.out.println("TestController--------------------------------------------");
List userList = testService.query();
return userList.get(0).getName();
}
@RequestMapping(value = "/student/{id}",produces = "text/html;charset=UTF-8")
@ResponseBody
public String student(@PathVariable("id") Integer id){
System.out.println("student-id:"+ id + "------------------------");
Student student = testService.student(id);
return student.getName();
}
@RequestMapping(value = "/hello")
public String hello(){
return "hello";
}
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdata?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8","root","yhuan");
Statement myStmt = conn.createStatement();
ResultSet rs = myStmt.executeQuery("SELECT * FROM user");
while(rs.next()) {
System.out.println(rs.getString("name"));
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
注*:内附main方法,自测数据库连通性。
新建/webapp/WEB-INF/views/hello.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2024/1/16
Time: 16:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
this is helloPage
这是SSM框架的 hello页面
http://localhost:8080/web-ssm/test/testQuery
http://localhost:8080/web-ssm/test/hello
参考文章:IDEA 详细SSM框架的搭建_ssm 文件夹结构-CSDN博客
end!!!