一、spring boot项目搭建
1、项目采用spring boot + mybatis,项目管理采用maven。至于maven安装以及环境配置就不细说了。
2、创建maven项目,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.0modelVersion>
<groupId>com.manninggroupId>
<artifactId>readingListartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>readingListname>
<description>Reading List Demodescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.6.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.7java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>net.sourceforge.nekohtmlgroupId>
<artifactId>nekohtmlartifactId>
<version>1.9.22version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
3、创建maven项目结构如下:
4、创建入口java文件(ReadingListApplication.java):
package com.manning;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ReadingListApplication {
public static void main(String[] args) {
SpringApplication.run(ReadingListApplication.class, args);
}
}
a、@SpringBootApplication注解开启组件扫描自动注释;
b、main()方法启动引导应用程序;
5、定义一个用户类(User):
package com.manning.entity;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6、定义dao层接口:
package com.manning.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.manning.entity.User;
@Mapper
public interface UserMapper {
public List queryUserByName(String username);
}
创建对应的mapper.xml文件:
<mapper namespace="com.manning.dao.UserMapper">
<select id="queryUserByName" resultType="User">
select * from user
select>
mapper>
7、定义server层接口和实现类:
package com.manning.service;
import java.util.List;
import com.manning.entity.User;
public interface UserService {
public List queryUserByName(String username);
}
实现类:
package com.manning.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manning.dao.UserMapper;
import com.manning.entity.User;
import com.manning.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List queryUserByName(String username) {
return userMapper.queryUserByName(username);
}
}
8、定义controller类:
package com.manning.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.manning.entity.User;
import com.manning.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/queryUserByName")
public String queryUserByName(String username,Model model){
List list = userService.queryUserByName(username);
model.addAttribute("users", list);
return "userList";
}
}
9、在src/main/resources/templates下创建页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Reading Listtitle>
<link rel="stylesheet" th:href="@{/style.css}">link>
head>
<body>
<h2>用户列表:h2>
<div>
<div th:unless="${#lists.isEmpty(users)}">
<dl th:each="user : ${users}">
<dt class="bookHeadline">
<span th:text="用户名:${user.username}">span>
<span th:text="密码:${user.password}">span>
dt>
dl>
div>
body>
html>
10、application.properties配置文件:
mybatis.type-aliases-package=com.manning.entity
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.cache=false 禁用模板缓存,避免开发过程中每次修改页面也要重启服务的烦恼。
11、启动项目:
本人用的是eclipse工具,如果eclipse没有安装STS插件,那么需要在入口类(ReadingListApplication.java)通过以下方式启动:
启动成功控制台打印图片:
本人第一次写文章,有问题希望各位朋友们见谅和指出,谢谢!
文章参考:http://www.cnblogs.com/suncj/p/4065589.html