springboot+mybatis+springMVC基础框架搭建

项目结构概览

pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>
    <!-- springboot依赖的父类 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <!-- 项目骨架 -->
    <groupId>com.pupeiyuan</groupId>
    <artifactId>ssm-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 支持springWEB web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- 通用Mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>RELEASE</version>
        </dependency>
        <!-- 分页助手 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- mysql 数据库驱动. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

spring.profiles.active=test

application-test.properties

#datasource -- mysql
jdbc.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=pypua
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.InitialSize=10
jdbc.MinIdle=10
jdbc.MaxActive=100

#Tomcat
server.port=8082
server.servlet-path=/

#log4j
#logging.level.org.springframework=DEBUG

#SpringMVC
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

#mybatis && Mapper
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.configuration.mapUnderscoreToCamelCase=true
mapper.mappers=com.karle.tk.TkMapper
mapper.identity=MYSQL

#banner
banner.charset= UTF-8

#jsp
server.jsp-servlet.init-parameters.development=true

# pagehelper properties
pagehelper.offsetAsPageNum=true
pagehelper.rowBoundsWithCount=true
pagehelper.pageSizeZero=true
pagehelper.reasonable=false
pagehelper.params=pageNum=pageHelperStart;pageSize=pageHelperRows;
pagehelper.supportMethodsArguments=false

main.java

package com.pupeiyuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.pupeiyuan.config.MainApplication;

/**
 * Springboot启动类
 * @author Administrator
 *
 */

public class main {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

MainApplication.java

package com.pupeiyuan.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;

@Configuration
//扫描bean
@ComponentScan(basePackages = "com.pupeiyuan.*")
//不用自动配置数据源
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class MainApplication extends SpringBootServletInitializer {

    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;

    @Value("${jdbc.username}")
    private String jdbcUsername;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Value("${jdbc.InitialSize}")
    private int InitialSize;

    @Value("${jdbc.MinIdle}")
    private int MinIdle;

    @Value("${jdbc.MaxActive}")
    private int MaxActive;

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        // BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        DruidDataSource druidDataSource = new DruidDataSource();
        // 数据库驱动
        druidDataSource.setDriverClassName(jdbcDriverClassName);
        // 相应驱动的jdbcUrl
        druidDataSource.setUrl(jdbcUrl);
        // 数据库的用户名
        druidDataSource.setUsername(jdbcUsername);
        // 数据库的密码
        druidDataSource.setPassword(jdbcPassword);
        // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0
        // 初始化时建立物理连接的个数
        druidDataSource.setInitialSize(InitialSize);
        // 最小连接池数量
        druidDataSource.setMinIdle(MinIdle);
        // 最大连接池数量
        druidDataSource.setMaxActive(MaxActive);

        //配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙
        try {
            druidDataSource.setFilters("stat,wall,log4j");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 通过connectProperties属性来打开mergeSql功能;慢SQL记录
        druidDataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        //合并多个DruidDataSource的监控数据
        druidDataSource.setUseGlobalDataSourceStat(true);
        return druidDataSource;
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MainApplication.class);
    }
}

MybatisConfig.java

package com.pupeiyuan.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.github.pagehelper.PageHelper;

import tk.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

@Configuration
@MapperScan("com.pupeiyuan.mapper")
public class MyBatisConfig {

    @Autowired
    private DataSource dataSource;

    @Bean
    @ConditionalOnMissingBean // 当容器里没有指定的Bean的情况下创建该对象
    public SqlSessionFactoryBean sqlSessionFactoryBean() {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 设置别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("com.pupeiyuan.bean");

        return sqlSessionFactoryBean;
    }

}

SpringMVCConfig.java

package com.pupeiyuan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.pupeiyuan.interceptors.IsLoginHandlerInterceptor;

@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter{

    @Autowired
    private IsLoginHandlerInterceptor isLoginHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 判断用户是否登录的拦截器
        registry.addInterceptor(isLoginHandlerInterceptor).addPathPatterns("/burket/**");
    }
}

DruidConfiguration.java

package com.pupeiyuan.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfiguration {

    /**
     * 注册一个StatViewServlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean DruidStatViewServle2() {
        // org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
                "/druid/*");
        // 添加初始化参数:initParams
        // 白名单:
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        // IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not
        // permitted to view this page.
        servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
        // 登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "123456");
        // 是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    /**
     * 注册一个:filterRegistrationBean
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

原文地址:https://www.cnblogs.com/pypua/p/10076952.html

时间: 2024-10-07 22:35:29

springboot+mybatis+springMVC基础框架搭建的相关文章

SpringMVC基础框架搭建

SpringMVC框架搭建步骤: 1.将需要jar包导入lib文件夹下   2.配置web.xml  3.配置springMVC核心配置文件  4.编码Controller类 说明:本项目源码导入eclipse,在tomcat运行后 输入http://localhost:8080/BrainTrain/welcome.jsp进行测试 所需的jar包:http://pan.baidu.com/s/1i3QKYNF(百度云盘) 项目源码:http://pan.baidu.com/s/1kTWM9Rh

Springmvc基础框架搭建流程(1)-基于xml配置文件

该篇文章对SpringMVC的基本使用过程做简单介绍,这里基于xml配置文件进行配置的.使用的工程为简单的系统登录过程. 1.eclipse下创建web工程,名称为SpringLogin,根目录修改为WebRoot(这样的Web工程可以在myeclipse下正常运行),该工程实现登录功能: 2.在lib中添加springmvc所需的jar包,这里使用的是3.2.9版本的jar包: 3.在src下创建2个包com.by.controller.com.by.service.com.by.manage

Spring+SpringMVC+Mybatis+jdbc基础框架搭建(注解版)

创建好maven项目后,开始搭建框架. 项目结构如下: resource文件夹下创建了三个文件夹mybatis,spring,它们分别用来放相关的配置文件,mapper 文件夹则是用来放查询数据库的xml文件.generatorConfjg.xml是用于代码生成的,在这里可以去掉,且不做讲述.taglib.xml文件也可以去掉,与本文无关. 1.引入包 在pom.xml文件中添加相关依赖包.我这里引入了一些代理包,可以自行去掉. <properties> <project.build.s

【转】Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+MySql的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果. 本文工程免费下载 一.准备工作 1. 首先创建一个表: CREATE TABLE `t_user` ( `USER_ID` int(11) NOT NULL AUTO_INCREMENT, `USER_NAME` char(3

Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+MySql的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果. 一.准备工作 1. 首先创建一个表: CREATE TABLE `t_user` ( `USER_ID` int(11) NOT NULL AUTO_INCREMENT, `USER_NAME` char(30) NOT NU

微信公众平台开发教程(三) 基础框架搭建

微信公众平台开发教程(三) 基础框架搭建 上一章,我们已经初步讲解了微信公众账号开发的基本原理,今天我们来探索设计实现. 首先我们设计了模块层次图,当然图中只是给出一种实现方式,不局限于此.具体见下图. 主要功能介绍如下: 1)请求接口层.处理HTTP请求,及响应 2)分发层.由接口层传入请求,然后具体分析请求类型,分发至不同的处理器 3)业务逻辑层.这里是我们的具体业务逻辑了,根据请求,实现具体的业务逻辑. 4)数据层.我们在实现某个应用时可能需要访问数据,可以是数据库或者是文件.如果是简单应

mybatis springmvc velocity环境搭建

前言 轻量级ORM框架MyBatis完美的配合SpringMVC web框架实现了后台action的开发,结合Java模版引擎velocity实现了Java代码与前端代码的隔离. 搭建过程 后台配置mybatis 添加依赖 Spring 3.2.4-RELEASE <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-web

springboot+mybatis+springmvc整合实例

以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是"零配置","零配置"不等于什么也不配置,只是说相对于传统的ssm框架的xml配置或是注解配置,要少的多.作为常规的来说,一个ssm框架整合,拿maven来说,首先在src/main/resource下加入jdbc.properties,spring-mvc.xml,spring-myba

spring+mybatis+mina+logback框架搭建

第一次接触spring,之前从来没有学过spring,所以算是赶鸭子上架,花了差不多一个星期来搭建,中间遇到各种各样的问题,一度觉得这个框架搭建非常麻烦,没有一点技术含量,纯粹就是配置,很低级!但随着搭建的完成,有一点点体会:框架可以让我们的代码更加像一个项目,而不是一个普普通通的作业,这在之前我们学生时代往往不会注意到这一点.我觉得这就是专业和业余的区别.当然,目前,我连spring入门可能都算不上,只是为了完成任务来搭建这套框架,但还是很有收获的,所以记录下这篇博客,给过来人参考. 另外还有