<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.java.springbootgroupId>
<artifactId>springboot-demoartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>springboot-demo Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
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.3.0version>
dependency>
dependencies>
project>
application.yml配置文件
#设置访问端口
server:
port: 8080
#设置数据库连接
spring:
datasource:
url: jdbc:mysql://localhost:3306/student?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: 用户名
password: 密码
initial-size: 10
max-active: 20
max-idle: 8
min-idle: 8
#设置静态资源访问
resources:
static-locations: classpath:static/
#thymeleaf模板引擎
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false
#mybatis配置
mybatis:
mapper-locations: classpath:mapping/*.xml
type-aliases-package: com.java.springboot.bean
#log日志
logging:
level:
com:
example:
mapper : debug
自己存数据即可
CREATE TABLE `stus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
package com.java.springboot.entity;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.java.springboot.mapper;
import com.java.springboot.bean.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/** * @author Woo_home * @create by 2019/8/12 */
@Repository
public interface UserMapper {
/* * 根据用户id查询用户 */
User select(int id);
/* *查询所有用户 */
List<User> userList();
}
和mapper一样即可
package com.java.springboot.service;
import com.java.springboot.entity.User;
public interface UserService {
User findUserById(int id);
}
package com.java.springboot.server;
import com.java.springboot.bean.User;
import com.java.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** * @author Woo_home * @create by 2019/8/12 */
@Service
public class UserService {
@Autowired
private UserMapper userMapper; //注入UserMapper
public User select(int id){
return userMapper.select(id);
}
public List<User> userList(){
return userMapper.userList();
}
}
UserMapper.xml
<mapper namespace="com.java.springboot.mapper.UserMapper">
<select id="select" resultType="com.java.springboot.bean.User">
select * from stus where id = #{id}
select>
<select id="userList" resultType="com.java.springboot.bean.User">
select * from stus
select>
mapper>
package com.java.springboot.controller;
import com.java.springboot.bean.User;
import com.java.springboot.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/** * @author Woo_home * @create by 2019/8/12 */
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/index")
@ResponseBody
public String index(){
return "Hello Springboot";
}
@RequestMapping("select/{id}")
@ResponseBody
public String save(@PathVariable int id){
return userService.select(id).toString();
}
@RequestMapping("/userList")
public String userList(Model model){
List<User> users = userService.userList();
model.addAttribute("users",users);
return "user";
}
}
html代码
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<link href="/css/bootstrap.css" rel="stylesheet">
head>
<body>
<table class="table">
<thead>
<tr>
<th>Idth>
<th>Usernameth>
<th>Passwordth>
tr>
thead>
<tbody>
<tr th:each="users:${users}">
<td th:text="${users.id}">td>
<td th:text="${users.username}">td>
<td th:text="${users.password}">td>
tr>
tbody>
table>
body>
html>
在浏览器输入
localhost:8080/index
localhost:8080/select/?
localhost:8080/userList
都可以访问到内容