项目的架构:
一、domain包:
User类
import java.sql.Date;
public class User {
private int id;
private String name;
private Date birthday;
private String address;
// 构造方法
public User() {
super();
}
public User(int id, String name, Date birthday, String address) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.address = address;
}
// getter & setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
UserMapper 接口,主要实现数据库的操作
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> selectAll();
@Select("select * from user where id=#{id}")
User selectUserById(int id);
@Select("select * from user where name=#{name}")
User selectUserByuserName(String userName);
@Insert("insert into user(name,birthday,address) values(#{name},#{birthday},#{address})")
void addUser(User user);
@Update("update user set name=#{name},birthday=#{birthday},address=#{address} where id=#{id}")
void updateUser(User user);
@Delete("delete from user where id=#{id}")
void deleteUser(int id);
}
二、service包
使用业务层逻辑
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gsq.springbootdemo.domain.User;
import com.gsq.springbootdemo.domain.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapperp;
public List<User> listAll() {
return userMapperp.selectAll();
}
public User selectUserById(int id) {
return userMapperp.selectUserById(id);
}
public User selectUserByuserName(String userName) {
return userMapperp.selectUserByuserName(userName);
}
public void addUser(User user) {
userMapperp.addUser(user);
System.out.println("good add user.");
}
public void updateUser(User user) {
userMapperp.updateUser(user);
System.out.println("update user success "+user.getId());
}
public void deleteUser(int id) {
userMapperp.deleteUser(id);
System.out.println("delete user success "+ id);
}
}
三、web包
相应页面请求
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.gsq.springbootdemo.domain.User;
import com.gsq.springbootdemo.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/list/all")
public List<User> listAll() {
List<User> list = userService.listAll();
for(User u:list) {
System.out.println(u.getId());
System.out.println(u.getName());
System.out.println(u.getBirthday());
System.out.println(u.getAddress());
}
return userService.listAll();
}
@RequestMapping(value={"/selectUserById"},method=RequestMethod.GET)
public User selectUserById(int id) {
return userService.selectUserById(id);
}
@RequestMapping(value={"/selectUserByuserName"},method=RequestMethod.GET)
public User selectUserByUserName(String userName) {
return userService.selectUserByuserName(userName);
}
@RequestMapping(value={"/addUser"},method=RequestMethod.POST)
public void addUser(User user) {
userService.addUser(user);
System.out.println("good add user.");
}
@RequestMapping(value={"/updateUser"},method=RequestMethod.POST)
public void updateUser(User user) {
userService.updateUser(user);
}
@RequestMapping(value={"/deleteUser"},method=RequestMethod.POST)
public void deleteUser(int id) {
userService.deleteUser(id);
}
}
四、spring boot Main函数入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
五、application.properties 配置
spring.datasource.url=jdbc:mysql://192.168.1.211:3306/springSample?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456a?
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=com.gsq.springbootdemo.domain
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
六、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>
<groupId>com.gsq</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
七、数据库相关
/*
Navicat MySQL Data Transfer
Source Server : 192.168.1.211
Source Server Version : 50629
Source Host : 192.168.1.211:3306
Source Database : springSample
Target Server Type : MYSQL
Target Server Version : 50629
File Encoding : 65001
Date: 2018-11-26 18:30:26
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_login_log
-- ----------------------------
DROP TABLE IF EXISTS `t_login_log`;
CREATE TABLE `t_login_log` (
`login_log_id` int(32) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`ip` varchar(23) DEFAULT NULL,
`login_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`login_log_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_login_log
-- ----------------------------
INSERT INTO `t_login_log` VALUES ('1', '123456', '0:0:0:0:0:0:0:1', '2018-10-16 10:59:26');
INSERT INTO `t_login_log` VALUES ('2', '123456', '0:0:0:0:0:0:0:1', '2018-10-16 11:20:23');
INSERT INTO `t_login_log` VALUES ('3', '123456', '0:0:0:0:0:0:0:1', '2018-10-16 11:20:40');
INSERT INTO `t_login_log` VALUES ('4', '123456', '0:0:0:0:0:0:0:1', '2018-10-16 13:59:09');
INSERT INTO `t_login_log` VALUES ('5', '123456', '0:0:0:0:0:0:0:1', '2018-10-16 13:59:39');
INSERT INTO `t_login_log` VALUES ('6', '123456', '0:0:0:0:0:0:0:1', '2018-10-19 15:18:46');
INSERT INTO `t_login_log` VALUES ('7', '123456', '0:0:0:0:0:0:0:1', '2018-10-19 15:44:29');
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL DEFAULT '0',
`user_name` varchar(30) NOT NULL,
`credits` int(11) DEFAULT NULL,
`password` varchar(32) NOT NULL,
`last_visit` datetime DEFAULT NULL,
`last_ip` varchar(23) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('123456', 'albert', '46', '123456', '2018-10-19 15:44:29', '0:0:0:0:0:0:0:1');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(64) NOT NULL COMMENT '姓名',
`birthday` date DEFAULT NULL COMMENT '生日',
`address` varchar(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '1993-10-10', '北京');
INSERT INTO `user` VALUES ('2', '李四', '1995-10-01', '上海');
INSERT INTO `user` VALUES ('3', '王老五', '2000-10-01', '广州');
INSERT INTO `user` VALUES ('4', '陈庆', '2000-10-01', '故宫门前');