詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
1.在MyEclipse里創(chuàng)建一個(gè)maven項(xiàng)目。File>New>Maven Project:
勾選圖中紅色部分,然后點(diǎn)擊Next。

2.填寫下圖中紅色部分然后點(diǎn)擊Finish。

3.此時(shí)一個(gè)maven項(xiàng)目已經(jīng)生成,目錄結(jié)構(gòu)如下:

4.打開pom.xml在里面編輯如下內(nèi)容:
<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)建數(shù)據(jù)源
@Bean
@ConfigurationProperties(prefix = "spring.datasource")//指定數(shù)據(jù)源的前綴 ,在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下建立應(yīng)用的配置文件application.properties。
#datasource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的數(shù)據(jù)庫(kù)名稱?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull spring.datasource.username=數(shù)據(jù)庫(kù)用戶名 spring.datasource.password=數(shù)據(jù)庫(kù)密碼 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
相應(yīng)的配置需要根據(jù)自己的實(shí)際情況去做修改。
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方法返回的是一個(gè)字符串,因?yàn)槲覀兘o應(yīng)用加載了freemarker模塊做視圖展現(xiàn),所以需要?jiǎng)?chuàng)建一個(gè)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,所以最終建立一個(gè)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>
模板文件所在位置的目錄結(jié)構(gòu)如下圖:

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類實(shí)現(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)建實(shí)體類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此時(shí)在控制臺(tái)會(huì)看到如下日志輸出:

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

15.在pom.xml文件上右鍵Run As>Maven install可將項(xiàng)目打包為jar文件,生成的jar在target目錄下,可以將此jar拷貝到服務(wù)器上通過(guò)"java -jar 最終生成jar包的名字"運(yùn)行項(xiàng)目。
16.本項(xiàng)目的源碼已經(jīng)上傳到spring-boot_jb51.rar,有需要的朋友可以自行下載
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解idea搭建springboot+mybatis框架的教程
- 使用IDEA搭建SSM框架的詳細(xì)教程(spring + springMVC +MyBatis)
- Spring+Mybatis+Mysql搭建分布式數(shù)據(jù)庫(kù)訪問(wèn)框架的方法
- 詳解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級(jí)詳細(xì)版)
- Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文)
- Spring MVC 4.1.3 + MyBatis零基礎(chǔ)搭建Web開發(fā)框架(注解模式)
- Java的MyBatis框架項(xiàng)目搭建與hellow world示例
- Windows下Java+MyBatis框架+MySQL的開發(fā)環(huán)境搭建教程
- MyBatis框架搭建與代碼解讀分析
相關(guān)文章
Java實(shí)現(xiàn)解析Excel復(fù)雜表頭
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)解析Excel復(fù)雜表頭功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-01-01
Springboot2.x結(jié)合Mabatis3.x下Hikari連接數(shù)據(jù)庫(kù)報(bào)超時(shí)錯(cuò)誤
本文針對(duì)Springboot2.x與Mybatis3.x結(jié)合使用時(shí),Hikari連接數(shù)據(jù)庫(kù)出現(xiàn)超時(shí)錯(cuò)誤的問(wèn)題進(jìn)行了深入分析,并提供了一系列有效的解決方法,感興趣的可以了解一下2023-11-11
Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié)
這篇文章主要介紹了Spring Boot Actuator監(jiān)控端點(diǎn)小結(jié),需要的朋友可以參考下2017-06-06
SpringBoot3快速整合MyBatisPlus的示例代碼
本文介紹了快速整合MyBatis-Plus到Spring Boot 3項(xiàng)目中,包括依賴引入、代碼生成器使用等,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
淺談SpringBoot實(shí)現(xiàn)自動(dòng)裝配的方法原理
SpringBoot的自動(dòng)裝配是它的一大特點(diǎn),可以大大提高開發(fā)效率,減少重復(fù)性代碼的編寫。本文將詳細(xì)講解SpringBoot如何實(shí)現(xiàn)自動(dòng)裝配,需要的朋友可以參考下2023-05-05
SpringBoot整合mybatis常見問(wèn)題(小結(jié))
這篇文章主要介紹了SpringBoot整合mybatis常見問(wèn)題(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
如何設(shè)置IDEA遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境并結(jié)合cpolar實(shí)現(xiàn)ssh遠(yuǎn)程開發(fā)(最新推薦)
本文主要介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境,并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)無(wú)公網(wǎng)遠(yuǎn)程連接,然后實(shí)現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),感興趣的朋友跟隨小編一起看看吧2024-03-03

