开发环境说明:
idea2017.2.6
jdk8
maven3.5
绿色版mysql+绿色版mysql客户端
1、idea创建springboot
2、点击直接生成pow.xml文件
4.0.0
htt.wave
notification
0.0.1-SNAPSHOT
jar
notification
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
3、一次编写model、service、cotroller包下的java文件
Admin.java
package htt.wave.notification.model;
import java.util.Date;
public class Admin {
private long id;
private String userName;
private String password;
private int age;
private String phoneNumber;
private String headPicture;
private Date addDate;
private Date updateDate;
private int state;
public long getId() {
return id;
}
public void setId(long 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 String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getHeadPicture() {
return headPicture;
}
public void setHeadPicture(String headPicture) {
this.headPicture = headPicture;
}
public Date getAddDate() {
return addDate;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
@Override
public String toString() {
return "Admin [id=" + id + ", userName=" + userName + ", password=" + password + ", age=" + age
+ ", phoneNumber=" + phoneNumber + ", headPicture=" + headPicture + ", addDate=" + addDate
+ ", updateDate=" + updateDate + ", state=" + state + "]";
}
}
4、service的代码
package htt.wave.notification.service;
import java.util.List;
import htt.wave.notification.model.Admin;
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 AdminService {
@Select("SELECT * FROM `boottest`.`admin` where userName = #{userName} and password = #{password} and state = 0;")
Admin findByNameAndPassword(Admin admin);
@Select("SELECT * FROM `boottest`.`admin` where userName = #{userName} and password = #{password} and realName = #{realName}")
List findByAdminWithPage(Admin admin, int start, int end);
@Insert("INSERT INTO `boottest`.`admin` (`id`, `userName`, `password`, `realName`, `age`, `phoneNumber`, `headPicture`, `addDate`, `updateDate`, `state`) VALUES (null, #{userName}, #{password}, #{realName}, #{age}, #{phoneNumber}, #{headPicture}, now(), now(), 0);")
int insert(Admin admin);
@Update("UPDATE `boottest`.`admin` SET `userName` = #{userName}, `password` = #{password}, `realName` = #{realName}, `age` = #{age}, `phoneNumber` = #{phoneNumber}, `headPicture` = #{headPicture}, `updateDate` = now(), `state` = #{state} WHERE `id` = #{id};")
int updateStateById(int id);
@Delete("DELETE FROM `boottest`.`admin` WHERE id = #{id}")
int deleteById(int id);
}
5、cotroller的代码
package htt.wave.notification.cotroller;
import htt.wave.notification.model.Admin;
import htt.wave.notification.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminService service;
@RequestMapping("login")
public Admin page1(Admin admin) {
return service.findByNameAndPassword(admin);
}
}
6、application.properties的配置
spring.datasource.url=jdbc:mysql://localhost:3306/boottest?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
7、mysql数据库的配置
8、接口访问:http://localhost:8080/login?userName=huitao&password=123456
到此springboot的配置基本完成