springboot框架快速搭建
1. 新建Maven项目 spring-boot
2. 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.0</modelVersion>
- <groupId>com.java</groupId>
- <artifactId>spring-boot</artifactId>
- <version>1.0.0</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- </parent>
- <dependencies>
- <!-- Spring Boot -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-logging</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-log4j</artifactId>
- <version>1.3.8.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.0.1</version>
- </dependency>
- <!-- 数据库连接池 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.11</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- </dependencies>
- <build>
- <finalName>${project.artifactId}</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
3. 启动类 Starter.java
- package com.java.springboot;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.builder.SpringApplicationBuilder;
- import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- /**
- * 启动类
- *
- * @author Logan
- * @version 1.0.0
- *
- */
- @SpringBootApplication
- public class Starter extends SpringBootServletInitializer {
- public static void main(String[] args) {
- SpringApplication.run(Starter.class, args);
- }
- @Override
- protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
- return builder.sources(Starter.class);
- }
- }
4. User.java
- package com.java.springboot.pojo;
- public class User {
- private String id;
- private String username;
- private String password;
- private Integer age;
- public String getId() {
- return id;
- }
- public void setId(String 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 Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
- }
- }
5. UserMapper.java
- package com.java.springboot.mapper;
- import java.util.List;
- import com.java.springboot.pojo.User;
- public interface UserMapper {
- Integer checkLogin(User user);
- List<User> findUserList(User user);
- void addUser(User user);
- void updateUser(User user);
- void deleteUser(User user);
- }
6. 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.java.springboot.mapper.UserMapper">
- <!-- 配置数据库字段和实体类映射 -->
- <resultMap id="userMap" type="com.java.springboot.pojo.User">
- <id property="id" column="id" />
- <result property="username" column="username" />
- <result property="password" column="password" />
- <result property="age" column="age" />
- </resultMap>
- <!-- 检查登录 -->
- <select id="checkLogin" parameterType="com.java.springboot.pojo.User" resultType="java.lang.Integer">
- select 1 from user where username=#{username} and password=#{password}
- </select>
- <!-- 查询用户列表 -->
- <select id="findUserList" parameterType="com.java.springboot.pojo.User" resultType="com.java.springboot.pojo.User">
- select id,username,password,age from user
- </select>
- <!-- 新增用户 -->
- <insert id="addUser" parameterType="com.java.springboot.pojo.User" >
- <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
- select replace(uuid(), '-', '') as id from dual
- </selectKey>
- insert into user(id,username,password,age) values(#{id},#{username},#{password},#{age});
- </insert>
- <!-- 修改用户信息 -->
- <update id="updateUser" parameterType="com.java.springboot.pojo.User" >
- update user set username=#{username}, password=#{password}, age=#{age} where id=#{id}
- </update>
- <!-- 根据id删除用户 -->
- <delete id="deleteUser" parameterType="java.lang.String">
- delete from user where id=#{id}
- </delete>
- </mapper>
7、UserService.java
- package com.java.springboot.service;
- import java.util.List;
- import com.java.springboot.pojo.User;
- public interface UserService {
- Integer checkLogin(User user);
- List<User> findUserList(User user);
- void addOrUpdateUser(User user);
- void deleteUser(User user);
- }
8、UserServiceImpl.java
- package com.java.springboot.service.impl;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import com.java.springboot.mapper.UserMapper;
- import com.java.springboot.pojo.User;
- import com.java.springboot.service.UserService;
- @Service("userService")
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
- @Override
- public Integer checkLogin(User user) {
- return userMapper.checkLogin(user);
- }
- @Override
- public List<User> findUserList(User user) {
- return userMapper.findUserList(user);
- }
- @Override
- @Transactional
- public void addOrUpdateUser(User user) {
- if (null == user.getId() || user.getId().isEmpty()) {
- userMapper.addUser(user);
- } else {
- userMapper.updateUser(user);
- }
- }
- @Override
- @Transactional
- public void deleteUser(User user) {
- userMapper.deleteUser(user);
- }
- }
9、UserController.java
- package com.java.springboot.controller;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- 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 org.springframework.web.bind.annotation.ResponseBody;
- import com.java.springboot.pojo.User;
- import com.java.springboot.service.UserService;
- @Controller
- @RequestMapping("/user")
- public class UserController {
- private static final Log LOG = LogFactory.getLog(UserController.class);
- @Autowired
- private UserService userService;
- @RequestMapping("/login")
- public String login() {
- return "user/login";
- }
- @RequestMapping("/checkLogin")
- public String checkLogin(User user, Model model) {
- LOG.info("Into checkLogin");
- System.out.println(user);
- String responseUrl = null;
- // 检查用户名密码是否存在
- Integer result = userService.checkLogin(user);
- System.out.println("result: " + result);
- LOG.info("result: " + result);
- // 绑定用户名密码输出返回
- if (null == result) {
- model.addAttribute("username", user.getUsername());
- model.addAttribute("password", user.getPassword());
- responseUrl = "user/login";
- }
- // 登录成功,访问主页
- else {
- responseUrl = "user/main";
- }
- LOG.info("Exit checkLogin");
- return responseUrl;
- }
- @ResponseBody
- @RequestMapping("/findUserList")
- public List<User> findUserList(User user) {
- LOG.info("Into findUserList");
- System.out.println(user);
- List<User> list = userService.findUserList(user);
- LOG.info("Exit findUserList");
- return list;
- }
- @RequestMapping("/toAddOrUpdateUser")
- public String toAddOrUpdateUser(User user, Model model) {
- LOG.info("Into toAddOrUpdateUser");
- System.out.println("user: " + user);
- // 输出id,根据id是否为空,判断是新增还是修改
- model.addAttribute("user", user);
- LOG.info("Exit toAddOrUpdateUser");
- return "user/addOrUpdateUser";
- }
- @RequestMapping("/addOrUpdate")
- public String addOrUpdate(User user) {
- LOG.info("Into addOrUpdate");
- System.out.println("user: " + user);
- userService.addOrUpdateUser(user);
- System.out.println(user);
- LOG.info("Exit addOrUpdate");
- return "user/main";
- }
- @ResponseBody
- @RequestMapping("/deleteUser")
- public String deleteUser(User user) {
- System.out.println(user);
- LOG.info("Into deleteUser");
- userService.deleteUser(user);
- LOG.info("Exit deleteUser");
- return "删除成功!";
- }
- }
10、ApplicationConfig.java
- package com.java.springboot.config;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.context.annotation.Configuration;
- /**
- * 配置类
- *
- * @author Logan
- * @version 1.0.0
- *
- */
- @Configuration
- @MapperScan({ "com.java.springboot.mapper" })
- public class ApplicationConfig {
- }
11、src/main/resources 下配置文件
12、application.properties
- server.port=8080
- #数据库自动配置
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://192.168.32.10:3306/springboot?useUnicode=true&characterEncoding=UTF-8
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
- #Thymeleaf 参见org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
- spring.thymeleaf.prefix=classpath:/views/
- spring.thymeleaf.suffix=.html
13、log4j.properties
- log4j.rootLogger= INFO, stdout,logfile
- log4j.logger.INFO=INFO,logfile
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} [%5p] [%c] - %m%n
- log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.logfile.File=G:/Java/logs/springboot/LogFile.log
- log4j.appender.logfile.DatePattern='_'yyyy-MM-dd'.log'
- log4j.appender.logfile.Threshold=INFO
- log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
- log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %m%n
- log4j.appender.logfile.Append=true
14、/static/index.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>登录</title>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <script type="text/javascript">
- function addUser() {
- location.href = "/user/toAddOrUpdateUser";
- }
- </script>
- </head>
- <body>
- <form action="/user/checkLogin" method="post">
- <table>
- <tr>
- <td>用户名:</td>
- <td><input name="username" /><br /></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td><input type="password" name="password" /><br /></td>
- </tr>
- <tr>
- <td><input type="submit" value="登录" /></td>
- <td><input type="button" value="注册" onclick="addUser();" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
15、/views/user/login.html
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <title>登录</title>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <script type="text/javascript">
- function addUser() {
- location.href = "/user/toAddOrUpdateUser";
- }
- </script>
- </head>
- <body>
- <form action="/user/checkLogin" method="post">
- <table>
- <tr>
- <td>用户名:</td>
- <td><input name="username" th:value="${username}" /><br /></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td><input type="password" name="password" th:value="${password}" /><br /></td>
- </tr>
- <tr>
- <td><input type="submit" value="登录" /></td>
- <td><input type="button" value="注册" onclick="addUser();" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
16、/views/user/main.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>主页</title>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <script src="/js/jquery-3.3.1.min.js"></script>
- <style type="text/css">
- td,
- th {
- border: 1px solid #AAA;
- }
- </style>
- <script type="text/jscript">
- $(function() {
- queryUserList();
- });
- function queryUserList() {
- $.post("/user/findUserList", {
- id: 1
- }, function(data) {
- var userList = data;
- var line = null;
- $("#list").empty();
- for(var i in userList) {
- line = tr(td(userList[i].id) + td(userList[i].username) + td(userList[i].password) + td(userList[i].age) + td(addBtn() + delBtn()));
- $("#list").append(line);
- }
- });
- }
- function tr(text) {
- return "<tr>" + text + "</tr>";
- }
- function td(text) {
- if(!text) {
- text = "";
- }
- return "<td>" + text + "</td>";
- }
- function addBtn() {
- return "<button onClick='update(this);'>修改</button>";
- }
- function delBtn() {
- return "<button onClick='deleteUser(this);'>删除</button>";
- }
- function update(obj) {
- var row = getRow(obj);
- var user = {
- id: row.eq(0).text(),
- username: row.eq(1).text(),
- password: row.eq(2).text(),
- age: row.eq(3).text()
- }
- var form = document.createElement("form");
- form.action = "/user/toAddOrUpdateUser";
- form.method = "post";
- var input = null;
- for(var key in user) {
- input = document.createElement("input");
- input.type = "hidden";
- input.name = key;
- input.value = user[key];
- form.appendChild(input)
- }
- document.body.appendChild(form);
- form.submit();
- }
- function deleteUser(obj) {
- var row = getRow(obj);
- var id = row.eq(0).text();
- $.post("/user/deleteUser", {
- id: id
- }, function(data) {
- alert(data);
- queryUserList();
- });
- }
- function getRow(obj) {
- return $(obj).parent().parent().children();
- }
- </script>
- </head>
- <body>
- <button onClick="location.href='/user/toAddOrUpdateUser'">新增</button>
- <div>
- <table style="border-collapse: collapse; width: 800px;">
- <thead>
- <tr>
- <th>ID</th>
- <th>用户名</th>
- <th>密码</th>
- <th>年龄</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody id="list">
- </tbody>
- </table>
- </div>
- </body>
- </html>
17、/views/user/addOrUpdateUser.html
- <!DOCTYPE html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <title>新增用户</title>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- </head>
- <body>
- <form action="/user/addOrUpdate" method="post">
- <input name="id" th:value="${user.id}" type="hidden" />
- <table>
- <tr>
- <td>用户名:</td>
- <td><input type="text" name="username" th:value="${user.username}" /><br /></td>
- </tr>
- <tr>
- <td>密码:</td>
- <td><input type="password" name="password" th:value="${user.password}" /><br /></td>
- </tr>
- <tr>
- <td>年龄:</td>
- <td><input type="text" name="age" th:value="${user.age}" /><br /></td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="提交" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
18. 创建数据库
- DROP DATABASE IF EXISTS springboot;
- CREATE DATABASE springboot;
- USE springboot;
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE `user` (
- `id` varchar(32) NOT NULL,
- `username` varchar(32) NOT NULL,
- `password` varchar(32) NOT NULL,
- `age` int(3) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- ;
注意:
jquery-3.3.1.min.js 请自行下载,配置到Main.jsp文件中
https://code.jquery.com/jquery-3.3.1.min.js
http://code.jquery.com/jquery-3.3.1.min.js
打开网址,Ctrl + S 保存文件
配置完成!
运行 Starter.java ,在浏览器输入下面地址访问
.
springboot框架快速搭建的更多相关文章
- springBoot框架的搭建
1新建一个项目: 2.注意选择JDK1.8,和选择spring initializr加载springBoot相关jar包: 3.下一步next: 4.下一步next,选择Web和MyBatis然后ne ...
- 玩转 SpringBoot 2 快速搭建 | RESTful Api 篇
概述 RESTful 是一种架构风格,任何符合 RESTful 风格的架构,我们都可以称之为 RESTful 架构.我们常说的 RESTful Api 是符合 RESTful 原则和约束的 HTTP ...
- ASP.NET MVC项目框架快速搭建实战
MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,采用”Domain Model as View Model“的MVC开发 ...
- springboot项目快速搭建
1. 问题描述 springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础上,对已有的maven gav进行了封装而已,今天用最简单 ...
- 玩转SpringBoot 2 快速搭建 | Spring Initializr 篇
SpringBoot 为我们提供了外网 Spring Initializr 网页版来帮助我们快速搭建 SpringBoot 项目,如果你不想用 IDEA 中的插件,这种方式也是不错的选择.闲话少说,直 ...
- 玩转 SpringBoot 2 快速搭建 | Spring Tool Suite篇
Spring Tool Suite (STS) 工具介绍 我个人比较推荐使用 Spring Tool Suite(STS),之所以推荐使用 Spring Tool Suite(STS) ,是因为它是 ...
- Django+Vue.js框架快速搭建web项目
一.vue环境搭建1.下载安装node.js.2.安装淘宝镜像cnpm,在命令窗口输入: npm install -g cnpm --registry=https://registry.npm.tao ...
- SpringBoot 1.快速搭建一个 SpringBoot Maven工程
一.新建一个Maven工程 (1)选择创建简单MAVNE工程 (2)输入你自己的MAVEN工程的Group Id(必填).Artifact Id(必填).Version(必填).Packaging(必 ...
- 玩转 SpringBoot 2 快速搭建 | IntellJ IDEA篇
IntellJ IDEA 介绍 IntelliJ IDEA 简称 IDEA,目前被认为是最好用的开发Java 语言开发工具之一.不过是收费的.和其同类型的工具有 Eclipse 和 MyEclip ...
随机推荐
- Linux调优(内存,CPU)
一.相关概念简介 system call:系统调用 time slice:cpu时间片 O(1):Linux系统进程调度器 page frame:分页 RSS:常驻内存集,无法被页面化的数据 MMU: ...
- 关于dijkstra求最短路(模板)
嗯.... dijkstra是求最短路的一种算法(废话,思维含量较低, 并且时间复杂度较为稳定,为O(n^2), 但是注意:!!!! 不能处理边权为负的情况(但SPFA可以 ...
- Codeforces Round #364 (Div. 2) A
Description There are n cards (n is even) in the deck. Each card has a positive integer written on i ...
- 自动化测试资源(一):谷歌浏览器驱动 ChromeDriver
ChromeDriver(官网):https://sites.google.com/a/chromium.org/chromedriver/ (需要XX上网,官网里有驱动和浏览器的版本映射关系) (如 ...
- Hadoop源生实用工具之distcp
1 概览 DistCp(Distributed Copy)是用于大规模集群内部或者集群之间的高性能拷贝工具. 它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成. 它把文件和目录的列 ...
- C#中动态创建数据库和数据表,很经典【转】
用ADOX创建access数据库方法很简单,只需要new一个Catalog对象,然后调用它的Create方法就可以了,如下: ADOX.Catalog catalog = new Catalog(); ...
- javascript获取滚动条位置(兼容所有浏览器)
有两种方式来获取浏览器滚动条的位置 第一种:document.documentElement.scrollTop 第二种:$("body").scrollTop() 第一种方式能够 ...
- C#获得字符串首字符字母(大写)
/// <summary> /// 获得字符串首字符字母(大写): /// </summary> /// <param name="cnChar"&g ...
- CF1152C Neko does Maths
思路: 假设a <= b,lcm(a + k, b + k) = (a + k) * (b + k) / gcd(a + k, b + k) = (a + k) * (b + k) / gcd( ...
- 深入剖析javaScript中的深拷贝和浅拷贝
如何区分深拷贝与浅拷贝,简单来说,假设B复制了A,当修改A时,看B是否会发生变化,如果B也跟着变了,说明这是浅拷贝,如果B没变,那就是深拷贝:我们先看两个简单的案例: //案例1(深拷贝) var a ...