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 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); ListfindUserList(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); ListfindUserList(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 ListfindUserList(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 ListfindUserList(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 ,在浏览器输入下面地址访问
.