IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> SSM+SpringBoot配置文件和详细实践过程(完整的数据传输显示) -> 正文阅读

[Java知识库]SSM+SpringBoot配置文件和详细实践过程(完整的数据传输显示)

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;

/*
* SpringBoot启动类*/

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

    <!--tomcat容器运行的是war,springboot默认生成的是jar包-->
    <packaging>war</packaging>

    <!-- 继承父级工程   -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.5</version>
    </parent>

    <dependencies>

    <!--web启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.4.2</version>
    </dependency>

    <!--thymeleaf: html模板-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.4.2</version>
    </dependency>

    <!--tomcat-->
    <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>

    <!--aop-->
    <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>


    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!--oracle-->

        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>21.3.0.0</version>
        </dependency>


        <!--C3P0数据源-->
    <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><!--包含目录下的.properties,.xml 文件都会扫描到-->
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory><!--所在的目录-->
                <includes><!--包含目录下的.properties,.xml 文件都会扫描到-->
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>


</project>
application.yml
#端口号
server:
  port: 8081

spring:
  thymeleaf:   #html 模板
    mode: HTML5
    encoding: UTF-8  #默认即使utf-8
    servlet:
      content-type: text/html  #默认
    cache: false #缓存
    prefix: classpath:/templates/  #默认/templates/
    suffix: .html  #默认.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配置文件
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>



    <!--设置:定义mybatis的一些全局属性-->
    <!--jdbcTypeForNull:当添加数据的时候,部分字段为null时,自动填入null,不加配置则会报错-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
        <!--PARTIAL[默认],FULL[全匹配],NONE[不匹配]-->
        <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) {
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**","/user/userList");
    }

}

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){
            //判断如果为AJAX请求则返回登录页面  并让请求为错误
            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   //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"})
        // var da = {name:"PageNum",value:"1",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) {
                    // alert(data.list[0].username)
                    var str = "";
                    for (var i in data.list) {
                        str+=data.list[i].username;
                    }

                    $("#zhanshiyemian").html(str);
                }
            })

        }


        $(function () {
            xianshi();
        })
    </script>
</head>
<body>
<h1>主页面啊</h1>
<!--<img th:src="@{static/img/123.png}" alt="">-->

<h1 th:text="${session.username}"></h1>

<div id="zhanshiyemian"></div>
<div id="qwe"></div>


</body>
</html>
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-10-25 12:24:25  更:2021-10-25 12:26:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 23:39:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码