一 数据库
数据库大家自己去创建就可以了,按照大家的实际情况来写sql语句和数据库的账号,密码
二 目录结构
三 配置pom.xml文件
新建maven文件并配饰pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
四 创建对应的类
User.java
package com.yuyi.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private Date birthday;
private Integer sex;
private String address;
private String uuuid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUuuid() {
return uuuid;
}
public void setUuuid(String uuuid) {
this.uuuid = uuuid;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
+ address + ", uuuid=" + uuuid + "]";
}
}
UserMapper.java
package com.yuyi.dao;
import org.apache.ibatis.annotations.Mapper;
import com.yuyi.pojo.User;
@Mapper
public interface UserMapper {
User getUserById(int id);
}
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.yuyi.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="com.yuyi.pojo.User">
select * from user where id=#{id}
</select>
</mapper>
UserMapper.java
package com.yuyi.dao;
import org.apache.ibatis.annotations.Mapper;
import com.yuyi.pojo.User;
@Mapper
public interface UserMapper {
User getUserById(int id);
}
UserService.java
package com.yuyi.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuyi.dao.UserMapper;
import com.yuyi.pojo.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id){
User user=new User();
user=userMapper.getUserById(id);
return user;
}
}
UserController.java
package com.yuyi.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.yuyi.pojo.User;
import com.yuyi.service.UserService;
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUserById(@PathVariable("id") int id) {
User user=userService.getUserById(id);
System.out.println(user);
return userService.getUserById(id);
}
}
Application.java
package com.yuyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
spring.datasource.username=root
spring.datasource.password=628081
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///mybatis
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.yuyi.pojo