SSM+SpringBoot
SpringBoot_application
@EnableTransactionManagement
启动必要:
package com.zaz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@EnableTransactionManagement
@SpringBootApplication
public class SpringBoot_application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(com.zaz.SpringBoot_application.class);
}
public static void main(String[] args) {
SpringApplication.run(com.zaz.SpringBoot_application.class,args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<artifactId>Spring_Boot_01</artifactId>
<groupId>com.zaz</groupId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.5</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.3.0.0</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.20</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
application.yml
server:
port: 8081
spring:
thymeleaf:
mode: HTML5
encoding: UTF-8
servlet:
content-type: text/html
cache: false
prefix: classpath:/templates/
suffix: .html
mvc:
static-path-pattern: /static/**
datasource:
driverClass: oracle.jdbc.driver.OracleDriver
jdbcUrl: jdbc:oracle:thin:@127.0.0.1:1521:orcl
user: zhangaizhi
password: 123456
acquireIncrement: 5
initialPoolSize: 10
minPoolSize: 10
maxPoolSize: 50
idleConnectionTestPeriod: 60
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:com/zaz/*/dao/mapper/common/*.xml
banner.txt
_ _ _ _
/ / |
/ \ / /
/ / \ / /
/ // / \ | / _ _ _
// /// \ \ | / / /
// \ \ | / / /
\ / \____/ / /
///....// \ / \/
/.....// \ /
./// \/ \
/.//... ------- \ \
/./...//.// | | ------------
/ // |-------| ------ \
/ /// // | | -------- \
// | | ----- \
// / |-------| | | \
/ GGGGGGG GGGGGGG | | | | | \
GG V GG | | | | | \
/// GGGGGGG GGGGGG GG . GG | | | | | \
/// GG V GGGGG .. GG | \| | / \ | \ /
/// GG .. GG GG G / \
/// GG G GGG GGG
/// GG GG GGGGGG
/// GGG GGG /
GGGGGG ; /
/ / |
/ // |
// // -------------|
/// .Love | ----------- |
../ / / . | | |
/ / / / / / / / / . .. Love -----|-----
/ / / / / . |
/ / / // / ..Love --------|----
/ / /// / / |
/ / / / ---------
// // /// / | |
/ / / | |
/ / ---------
/ /
/ / _ _
/ / / \ ___
/ / / \ / \
/ / \ / | |
/ / \/ \/
--- -- - -- -
/ / / / -- -- ------ - ------- ---
/ / / / You're so good at it. [Chinese: nì hài]
log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.com.zte.*.dao=debug
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
######################################################################################
# Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
######################################################################################
# DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
log4j.logger.com.opensymphony.xwork2=error
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="autoMappingBehavior" value="FULL"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.zaz.user.model"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="Oracle"/>
</plugin>
</plugins>
</configuration>
C3P0Config
配置c3p0配置信息:
package com.zaz.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class C3P0Config {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource createDataSource(){
return new ComboPooledDataSource();
}
}
MvcConfig
mvc的配置文件,拦截器配置:
package com.zaz.config;
import com.zaz.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
}
LoginInterceptor
package com.zaz.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
if(username == null){
if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
response.setHeader("Content-Type","text/plain;charset=utf8");
response.getWriter().write("FAILURE");
}else {
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
return false;
}else {
return true;
}
}
}
文件树
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iJYS8A6c-1635084902635)(C:/Users/lenovo/AppData/Roaming/Typora/typora-user-images/image-20211018173643290.png)]
UserAction
package com.zaz.user.control;
import com.zaz.user.model.SmbmsUser;
import com.zaz.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserAction {
@Autowired
private IUserService iUserService;
@RequestMapping("/mainView")
public String mainView(){
return "maina";
}
@RequestMapping("/maininfo1")
public String mainView1(SmbmsUser smbmsUser, Model model, HttpSession session){
System.out.println(smbmsUser);
SmbmsUser s = new SmbmsUser();
s.setUsername("张三");
model.addAttribute("user",s);
session.setAttribute("username",s);
return "maina";
}
@RequestMapping("/maininfo2")
@ResponseBody
public List<SmbmsUser> mainView2(SmbmsUser smbmsUser, Model model, HttpSession session){
System.out.println(smbmsUser);
SmbmsUser s = new SmbmsUser();
s.setUsername("张三");
model.addAttribute("user",s);
session.setAttribute("username",s);
List<SmbmsUser> list = new ArrayList<>();
return list;
}
@RequestMapping("/zhanshi")
@ResponseBody
public List<SmbmsUser> zhanshi(SmbmsUser smbmsUser, Model model, HttpSession session){
SmbmsUser s = new SmbmsUser();
List<SmbmsUser> userListService = iUserService.getUserListService(s);
System.out.println(userListService);
return userListService;
}
}
IUserDao
package com.zaz.user.dao;
import com.zaz.user.model.SmbmsUser;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface IUserDao {
public List<SmbmsUser> getUserList(SmbmsUser smbmsUser);
}
UserService
package com.zaz.user.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zaz.user.dao.IUserDao;
import com.zaz.user.model.SmbmsUser;
import com.zaz.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao iUserDao;
@Override
public List<SmbmsUser> getUserListService(SmbmsUser smbmsUser) {
return iUserDao.getUserList(smbmsUser);
}
@Transactional
public PageInfo<SmbmsUser> getUserListServicePage(SmbmsUser smbmsUser, PageInfo<SmbmsUser> pageInfo) {
PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());
List<SmbmsUser> proList = iUserDao.getUserList(smbmsUser);
PageInfo<SmbmsUser> pageInfo1 = new PageInfo<SmbmsUser>(proList);
return pageInfo1;
}
}
login.html
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script>
function mainView() {
location.href = "../user/mainView";
}
</script>
<body>
<h1>登录页面</h1>
<h1 onclick="mainView()">主页面</h1>
</body>
</html>
maina.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script th:src="@{static/js/jquery-3.4.1.js}"></script>
</head>
<body>
<h1>主页面啊</h1>
<img th:src="@{static/img/123.png}" alt="">
<h1 th:text="${user.username}"></h1>
<h1 th:text="${session.username.username}"></h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script th:src="@{/static/js/jquery-3.4.1.js}" ></script>
<script th:inline="javascript">
var da = $("#qwe").serializeArray();
da.push({name:"PageNum",value:"1"})
da.push({name:"PageSize",value:"5"})
function xianshi() {
var basePath = [[${#httpServletRequest.getScheme() + "://" + #httpServletRequest.getServerName() + ":" + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath()}]];
$.ajax({
url:basePath + '/user/ajaxtest',
data:da,
type:"post",
dataType:"json",
success:function (data) {
var str = "";
for (var i in data.list) {
str+=data.list[i].username;
}
$("#zhanshiyemian").html(str);
}
})
}
$(function () {
xianshi();
})
</script>
</head>
<body>
<h1>主页面啊</h1>
<h1 th:text="${session.username}"></h1>
<div id="zhanshiyemian"></div>
<div id="qwe"></div>
</body>
</html>
|