1.在myeclipse里创建一个maven项目。file>new>maven project:
勾选图中红色部分,然后点击next。
2.填写下图中红色部分然后点击finish。
3.此时一个maven项目已经生成,目录结构如下:
4.打开pom.xml在里面编辑如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<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.创建程序入口application.java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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);
}
//创建数据源
@bean
@configurationproperties (prefix = "spring.datasource" ) //指定数据源的前缀 ,在application.properties文件中指定
public datasource datasource() {
return new datasource();
}
//创建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();
}
//创建事物管理器
@bean
public platformtransactionmanager transactionmanager() {
return new datasourcetransactionmanager(datasource());
}
}
|
6.在src/main/resources下建立应用的配置文件application.properties。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#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下创建mybatis目录并在目录下创建usermapper.xml文件:
1
2
3
4
5
6
7
|
<?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.创建usercontroller类和视图文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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模块做视图展现,所以需要创建一个list模板,模板所在的目录在application.properties中指定为spring.freemarker.template-loader-path=classpath:/templates/,所以我们需要在src/main/resources下创建templates目录,然后在templates下创建user目录,模板文件后缀在application.properties中指定为spring.freemarker.suffix=.ftl,所以最终建立一个list.ftl文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!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.创建userservice接口:
1
2
3
4
5
6
7
8
9
10
11
|
package com.lm.service;
import java.util.list;
import com.lm.model.user;
public interface userservice {
list<user> findall();
}
|
10.创建userserviceimpl类实现userservice接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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.创建usermapper接口:
1
2
3
4
5
6
7
8
9
10
|
package com.lm.dao;
import java.util.list;
import com.lm.model.user;
public interface usermapper {
list<user> findall();
}
|
12.创建实体类user:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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.本项目的源码已经上传到,有需要的朋友可以自行下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/mmliuman/article/details/52797441