博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot框架快速搭建
阅读量:6164 次
发布时间:2019-06-21

本文共 13065 字,大约阅读时间需要 43 分钟。

1.    新建Maven项目  spring-boot

 

2.    pom.xml

4.0.0
com.java
spring-boot
1.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
org.springframework.boot
spring-boot-starter-thymeleaf
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
com.alibaba
druid
1.1.11
mysql
mysql-connector-java
${project.artifactId}
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
repackage

 

 

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 * */@SpringBootApplicationpublic 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
findUserList(User user); void addUser(User user); void updateUser(User user); void deleteUser(User user);}

 

 

6.   UserMapper.xml

select replace(uuid(), '-', '') as id from dual
insert into user(id,username,password,age) values(#{id},#{username},#{password},#{age});
update user set username=#{username}, password=#{password}, age=#{age} where id=#{id}
delete from user where id=#{id}

 

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
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
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
findUserList(User user) { LOG.info("Into findUserList"); System.out.println(user); List
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.Driverspring.datasource.url=jdbc:mysql://192.168.32.10:3306/springboot?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.type=com.alibaba.druid.pool.DruidDataSource#Thymeleaf 参见org.springframework.boot.autoconfigure.thymeleaf.ThymeleafPropertiesspring.thymeleaf.prefix=classpath:/views/spring.thymeleaf.suffix=.html

 

13、log4j.properties

log4j.rootLogger= INFO, stdout,logfilelog4j.logger.INFO=INFO,logfilelog4j.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%nlog4j.appender.logfile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.logfile.File=G:/Java/logs/springboot/LogFile.loglog4j.appender.logfile.DatePattern='_'yyyy-MM-dd'.log'log4j.appender.logfile.Threshold=INFOlog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %m%nlog4j.appender.logfile.Append=true

 

14、/static/index.html

            登录        
用户名:
密码:

 

15、/views/user/login.html

            登录        
用户名:
密码:

 

16、/views/user/main.html

            主页        
ID 用户名 密码 年龄 操作

 

17、/views/user/addOrUpdateUser.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文件中

    

   

打开网址,Ctrl + S 保存文件

 

 配置完成!

 

   运行   Starter.java   ,在浏览器输入下面地址访问

   

 

 

.

转载于:https://www.cnblogs.com/jonban/p/springboot.html

你可能感兴趣的文章
在Eclipse中搭建Python Django
查看>>
struts国际化
查看>>
Laravel 5.0 - Middleware (中间件)
查看>>
文件特殊权限及facl
查看>>
我的友情链接
查看>>
Android按两次返回键退出应用
查看>>
第一章:认识Redhat Linux
查看>>
文本查看指令
查看>>
我的友情链接
查看>>
android开源项目框架大全:《IT蓝豹》
查看>>
学习六十一
查看>>
delphi ini文件的基础读写。
查看>>
CentOS7中搭建cobbler自动装机服务
查看>>
多线程设计模式:第二篇 - 四种基础模式
查看>>
域名跳转
查看>>
企业网站建设注意事项
查看>>
WPS Office 2019 发布Linux 个人版
查看>>
未来PCB行业互联网+发展趋势
查看>>
PDF如何旋转页面
查看>>
UI设计入门:解析设计标注规范和图标规范
查看>>