【初学入门Demo注解版】SpringBoot + MyBatis + Thymeleaf +MySQL

【注解版】SpringBoot + MyBatis + Thymeleaf +MySQL

最近在自学SpringBoot,所以在此写下整合过程笔记,方便记忆!源码下载

  1. 创建数据库及表
	CREATE DATABASE `springbootdb`
	
	USE `springbootdb`;
	
	DROP TABLE IF EXISTS `user`;
	
	CREATE TABLE `user` (
	  `id` bigint(10) NOT NULL AUTO_INCREMENT,
	  `username` varchar(20) NOT NULL,
	  `password` varchar(20) NOT NULL,
	  `age` int(1) NOT NULL,
	  PRIMARY KEY (`id`)
	) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
	
	insert  into `user`(`id`,`username`,`password`,`age`) values 
	(1,'小黑','123456',1),
	(2,'小白','123456',2),
	(3,'小兰','123456',3),
	(4,'小小','123456',1),
	(5,'小率','123456',6),
	(6,'小给','123456',1),
	(7,'小哦','123456',5),
	(8,'小合','123456',1),
	(9,'小百','123456',100);
  1. 创建项目
    创建项目1
    创建项目2
    创建项目3
    创建项目4
    创建项目5
    创建项目6
    创建项目7
    创建项目8
  2. 这是创建项目成功后的pom.xml文件
	<?xml version="1.0" encoding="UTF-8"?>
	<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>
	    <parent>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-parent</artifactId>
	        <version>2.1.3.RELEASE</version>
	        <relativePath/> <!-- lookup parent from repository -->
	    </parent>
	    <groupId>com.example</groupId>
	    <artifactId>demo</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	    <name>demo</name>
	    <description>Demo project for Spring Boot</description>
	
	    <properties>
	        <java.version>1.8</java.version>
	    </properties>
	
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-thymeleaf</artifactId>
	        </dependency>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-web</artifactId>
	        </dependency>
	        <dependency>
	            <groupId>org.mybatis.spring.boot</groupId>
	            <artifactId>mybatis-spring-boot-starter</artifactId>
	            <version>2.0.0</version>
	        </dependency>
	
	        <dependency>
	            <groupId>mysql</groupId>
	            <artifactId>mysql-connector-java</artifactId>
	            <scope>runtime</scope>
	        </dependency>
	        <dependency>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-test</artifactId>
	            <scope>test</scope>
	        </dependency>
	    </dependencies>
	
	    <build>
	        <plugins>
	            <plugin>
	                <groupId>org.springframework.boot</groupId>
	                <artifactId>spring-boot-maven-plugin</artifactId>
	            </plugin>
	        </plugins>
	    </build>

	</project>
  1. 创建包、接口、类、控制器、网页
    在这里插入图片描述
  2. User.java
	package com.example.entity;

	import java.io.Serializable;
	
	/**
	 * @author Nopi
	 */
	public class User implements Serializable {
	
	    private Integer id;
	    private String username;
	    private String password;
	    private int age;
	
	    public Integer getId() {
	        return id;
	    }
	
	    public void setId(Integer 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;
	    }
	
	    public int getAge() {
	        return age;
	    }
	
	    public void setAge(int age) {
	        this.age = age;
	    }
	
	    public User(Integer id, String username, String password, int age) {
	        this.id = id;
	        this.username = username;
	        this.password = password;
	        this.age = age;
	    }
	
	    public User() {
	    }
	}

  1. UserDao.java
	package com.example.dao;
	
	import com.example.entity.User;
	import org.apache.ibatis.annotations.Mapper;
	import org.apache.ibatis.annotations.Select;
	import java.util.List;
	
	/**
	 * @author Nopi
	 */
	@Mapper
	public interface UserDao {
	
	    /**
	     * 获取所有用户信息数据接口
	     * @return List<User>
	     */
	    @Select("select * from User")
	    List<User> getUserList();
	}

  • @Select 是查询类的注解,所有的查询均使用这个
  • @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
  • @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值
  • @Update 负责修改,也可以直接传入对象
  • @delete 负责删除
  1. UserService.java
	package com.example.service;
	
	import com.example.entity.User;
	import java.util.List;
	
	/**
	 * @author Nopi
	 */
	public interface UserService {
	
	    /**
	     * 获取所有用户信息业务接口
	     * @return List<User>
	     */
	    List<User> getUserList();
	}

  1. UserServiceImpl.java
	package com.example.service.impl;
	
	import com.example.dao.UserDao;
	import com.example.entity.User;
	import com.example.service.UserService;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Service;
	import java.util.List;
	
	/**
	 * @author Nopi
	 */
	@Service
	public class UserServiceImpl implements UserService {
	
	    @Autowired
	    private UserDao userDao;
	
	    @Override
	    public List<User> getUserList() {
	        return userDao.getUserList();
	    }
	}
  1. UserController.java
	package com.example.controller;
	
	import com.example.service.UserService;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Controller;
	import org.springframework.ui.Model;
	import org.springframework.web.bind.annotation.RequestMapping;
	
	/**
	 * @author Nopi
	 */
	@Controller
	public class UserController {
	
	    @Autowired
	    private UserService userService;
	
	    @RequestMapping("/getUserList")
	    public String getUserList(Model model){
	        model.addAttribute("uList",userService.getUserList());
	        return "UserListShow";
	    }
	}

  1. DemoApplication.java
	package com.example.demo;
	
	import org.mybatis.spring.annotation.MapperScan;
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.context.annotation.ComponentScan;
	
	@SpringBootApplication
	@ComponentScan("com.example.*")
	@MapperScan("com.example.dao")
	public class DemoApplication {
	
	    public static void main(String[] args) {
	        SpringApplication.run(DemoApplication.class, args);
	    }
	
	}
  1. UserListShow.html
	<!DOCTYPE html>
	<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
	<head>
	    <meta charset="UTF-8">
	    <title>用户展示</title>
	</head>
	<body>
	    <table cellpadding="0" cellspacing="0" width="700px">
	        <tr th:each="user:${uList}">
	            <td th:text="${user.id}"></td>
	            <td th:text="${user.username}"></td>
	            <td th:text="${user.password}"></td>
	            <td th:text="${user.age}"></td>
	        </tr>
	    </table>
	</body>
	</html>
  1. application.properties
	spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
	spring.datasource.username=root
	spring.datasource.password=root
	spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?serverTimezone=GMT
	
	spring.thymeleaf.prefix=classpath:/templates/
	spring.thymeleaf.suffix=.html
	spring.thymeleaf.cache=false
	spring.thymeleaf.encoding=UTF-8
	spring.thymeleaf.mode=HTML
  1. 启动,浏览器地址栏输入 http://localhost:8080/getUserList
    在这里插入图片描述
  2. 结束

猜你喜欢

转载自blog.csdn.net/weixin_43266090/article/details/88081863
今日推荐