最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月04日 09:58:16   作者:mmliuman  
這篇文章主要介紹了詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1.在MyEclipse里創(chuàng)建一個maven項目。File>New>Maven Project:

勾選圖中紅色部分,然后點擊Next。

2.填寫下圖中紅色部分然后點擊Finish。

3.此時一個maven項目已經生成,目錄結構如下:

4.打開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>
  <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>
  </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>

5.創(chuàng)建程序入口Application.java.

package com.lm.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={"com.lm"})//指定spring管理的bean所在的包
@MapperScan("com.lm.dao")//指定mybatis的mapper接口所在的包
public class Application{

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

  //創(chuàng)建數據源
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource")//指定數據源的前綴 ,在application.properties文件中指定
  public DataSource dataSource() {
    return new DataSource();
  }

  //創(chuàng)建SqlSessionFactory
  @Bean
  public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

    return sqlSessionFactoryBean.getObject();
  }

  //創(chuàng)建事物管理器
  @Bean
  public PlatformTransactionManager transactionManager() {
    return new DataSourceTransactionManager(dataSource());
  }
}

6.在src/main/resources下建立應用的配置文件application.properties。

#datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的數據庫名稱?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=數據庫用戶名
spring.datasource.password=數據庫密碼
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=true
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=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
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=80

相應的配置需要根據自己的實際情況去做修改。

7.在在src/main/resources下創(chuàng)建mybatis目錄并在目錄下創(chuàng)建UserMapper.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lm.dao.UserMapper">
 <select id="findAll" resultType="com.lm.model.User" parameterType="java.lang.String">
  select id, username,password,email from t_user
 </select>
</mapper>

8.創(chuàng)建UserController類和視圖文件:

package com.lm.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lm.model.User;
import com.lm.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/list")
  public String list(ModelMap map){
    List<User> userList=userService.findAll();
    map.addAttribute("userList", userList);
    return "/user/list";
  }
}

可以看出list方法返回的是一個字符串,因為我們給應用加載了freemarker模塊做視圖展現(xiàn),所以需要創(chuàng)建一個list模板,模板所在的目錄在application.properties中指定為spring.freemarker.template-loader-path=classpath:/templates/,所以我們需要在src/main/resources下創(chuàng)建templates目錄,然后在templates下創(chuàng)建user目錄,模板文件后綴在application.properties中指定為spring.freemarker.suffix=.ftl,所以最終建立一個list.ftl文件:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>用戶列表</title>
</head>
<body>
  <table>
    <tr>
      <th>id</th><th>用戶名</th><th>密碼</th><th>郵箱</th>
    </tr>
    <#list userList as user>
    <tr>
      <td>${user.id}</td> <td>${user.username}</td><td>${user.password}</td><td>${user.email}</td>
    </tr>
    </#list>
 </table>
</body>
</html>

模板文件所在位置的目錄結構如下圖:

9.創(chuàng)建UserService接口:

package com.lm.service;

import java.util.List;

import com.lm.model.User;

public interface UserService {

  List<User> findAll();

}

10.創(chuàng)建UserServiceImpl類實現(xiàn)UserService接口:

package com.lm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lm.dao.UserMapper;
import com.lm.model.User;
import com.lm.service.UserService;

@Service
public class UserServiceImpl implements UserService{

  @Autowired
  private UserMapper userMapper;

  @Override
  public List<User> findAll() {
    return userMapper.findAll();
  }

}

11.創(chuàng)建UserMapper接口:

package com.lm.dao;

import java.util.List;
import com.lm.model.User;

public interface UserMapper {

  List<User> findAll();

}

12.創(chuàng)建實體類User:

package com.lm.model;

public class User {
  private Integer id;
  private String username;
  private String password;
  private String email;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

}

13.至此spring-boot框架已搭建完成,然后在Application.java中run as >java application此時在控制臺會看到如下日志輸出:

14.打開瀏覽器在地址欄輸入http://localhost/user/list便可以看到以下效果:

15.在pom.xml文件上右鍵Run As>Maven install可將項目打包為jar文件,生成的jar在target目錄下,可以將此jar拷貝到服務器上通過"java -jar 最終生成jar包的名字"運行項目。

16.本項目的源碼已經上傳到spring-boot_jb51.rar,有需要的朋友可以自行下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java實現(xiàn)解析Excel復雜表頭

    Java實現(xiàn)解析Excel復雜表頭

    這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)解析Excel復雜表頭功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • 淺談Java變量的初始化順序詳解

    淺談Java變量的初始化順序詳解

    本篇文章是對Java變量的初始化順序進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Springboot2.x結合Mabatis3.x下Hikari連接數據庫報超時錯誤

    Springboot2.x結合Mabatis3.x下Hikari連接數據庫報超時錯誤

    本文針對Springboot2.x與Mybatis3.x結合使用時,Hikari連接數據庫出現(xiàn)超時錯誤的問題進行了深入分析,并提供了一系列有效的解決方法,感興趣的可以了解一下
    2023-11-11
  • Spring Boot Actuator監(jiān)控端點小結

    Spring Boot Actuator監(jiān)控端點小結

    這篇文章主要介紹了Spring Boot Actuator監(jiān)控端點小結,需要的朋友可以參考下
    2017-06-06
  • SpringBoot3快速整合MyBatisPlus的示例代碼

    SpringBoot3快速整合MyBatisPlus的示例代碼

    本文介紹了快速整合MyBatis-Plus到Spring Boot 3項目中,包括依賴引入、代碼生成器使用等,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12
  • Java的字節(jié)緩沖流與字符緩沖流解析

    Java的字節(jié)緩沖流與字符緩沖流解析

    這篇文章主要介紹了Java的字節(jié)緩沖流與字符緩沖流解析,Java 緩沖流是Java I/O庫中的一種流,用于提高讀寫數據的效率,它通過在內存中創(chuàng)建緩沖區(qū)來減少與底層設備的直接交互次數,從而減少了I/O操作的開銷,需要的朋友可以參考下
    2023-11-11
  • 淺談SpringBoot實現(xiàn)自動裝配的方法原理

    淺談SpringBoot實現(xiàn)自動裝配的方法原理

    SpringBoot的自動裝配是它的一大特點,可以大大提高開發(fā)效率,減少重復性代碼的編寫。本文將詳細講解SpringBoot如何實現(xiàn)自動裝配,需要的朋友可以參考下
    2023-05-05
  • SpringBoot整合mybatis常見問題(小結)

    SpringBoot整合mybatis常見問題(小結)

    這篇文章主要介紹了SpringBoot整合mybatis常見問題(小結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 如何設置IDEA遠程連接服務器開發(fā)環(huán)境并結合cpolar實現(xiàn)ssh遠程開發(fā)(最新推薦)

    如何設置IDEA遠程連接服務器開發(fā)環(huán)境并結合cpolar實現(xiàn)ssh遠程開發(fā)(最新推薦)

    本文主要介紹如何在IDEA中設置遠程連接服務器開發(fā)環(huán)境,并結合Cpolar內網穿透工具實現(xiàn)無公網遠程連接,然后實現(xiàn)遠程Linux環(huán)境進行開發(fā),感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • MyBatis Generator介紹及使用方法

    MyBatis Generator介紹及使用方法

    MyBatis Generator 是一款針對 MyBatis 或 iBATIS 設計的代碼生成器,由 MyBatis 官方提供,這篇文章主要介紹了MyBatis Generator介紹及使用方法,需要的朋友可以參考下
    2023-06-06

最新評論

吴堡县| 大厂| 洱源县| 嵩明县| 惠安县| 冀州市| 鸡泽县| 通榆县| 阿克陶县| 土默特左旗| 奉节县| 崇明县| 丹阳市| 金秀| 肇源县| 太谷县| 瓮安县| 舞钢市| 和林格尔县| 曲麻莱县| 溧阳市| 呼和浩特市| 城固县| 民县| 龙江县| 榆林市| 集安市| 尚义县| 青浦区| 连南| 吉林市| 克什克腾旗| 富民县| 潞西市| 富锦市| 白水县| 漳浦县| 康定县| 仪陇县| 扶绥县| 湟中县|