在MyEclipse中搭建spring-boot+mybatis+freemarker框架

时间:2024-08-28 21:07:02

一、创建项目

  1.右键-->New-->Project...

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  2.选中Maven Project,点击next

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  3.选中第一个

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  4.添写Group Id,Artifact Id,选择Compiler Level为1.6

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  5.创建完成后的目录结构如下

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

二、创建目录环境

  1.修改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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lm.spring-boot</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--视图采用freemarker渲染 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<!-- 指定最终生成jar包的文件名-->
<finalName>spring-boot</finalName>
</build>
</project>

  2.更新maven相关的jar包

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  3.创建相关的包和目录,创建后的目录结构如下:

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  application包存放的是应用程序启动类。

  common包存放应用类。

  controller包存放控制类。

  dao包存放数据类。

  model包存放实体类。

  service包存放服务类。

  mapper文件夹存放映射文件。

  static文件夹存放页面相关的静态文件,如js、css等。

  web文件夹存放ftl页面。

三、创建程序入口Application.java

  在application包创建类文件Application.java

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  类文件内容为:

package sinosoft.bjredcross.application;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; @EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(basePackages={"sinosoft.bjredcross"})//指定spring管理的bean所在的包
@MapperScan("sinosoft.bjredcross.dao")//指定mybatis的mapper接口所在的包
public class Application {     public static void main(String[] args) {         SpringApplication.run(Application.class, args);
    }     //创建数据源
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")//指定数据源的前缀 ,在application.properties文件中指定
    public DataSource dataSource() {
        return new DataSource();
    }
    
    //创建SqlSessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }
    
    //创建事物管理器
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

四、创建配置文件application.properties

  在src/main/resource目录创建文件application.properties,文件内容如下:

#datasource
spring.datasource.url=jdbc\:mysql\://localhost\:3306/firstaid?useUnicode\=true&characterEncoding\=utf8&zeroDateTimeBehavior\=convertToNull
spring.datasource.username=root
spring.datasource.password=SinoSoftadmin123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/web/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.order=1 #server
server.port=8088

五、系统运行测试

  在Application.java文件上右键run as点击java application

在MyEclipse中搭建spring-boot+mybatis+freemarker框架

  执行正常显示如下:

在MyEclipse中搭建spring-boot+mybatis+freemarker框架