项目用Inteli做的,Eclipse的同学可以做参考
第一步:新建项目
第二步:配置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.example
groupId>
-
<artifactId>demo
artifactId>
-
<version>0.0.1-SNAPSHOT
version>
-
<packaging>jar
packaging>
-
-
<name>demo
name>
-
<description>Demo project for Spring Boot
description>
-
-
<parent>
-
<groupId>org.springframework.boot
groupId>
-
<artifactId>spring-boot-starter-parent
artifactId>
-
<version>1.5.7.RELEASE
version>
-
<relativePath/>
-
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-aop
artifactId>
-
dependency>
-
<dependency>
-
<groupId>org.springframework.boot
groupId>
-
<artifactId>spring-boot-starter-jdbc
artifactId>
-
dependency>
-
<dependency>
-
<groupId>org.mybatis.spring.boot
groupId>
-
<artifactId>mybatis-spring-boot-starter
artifactId>
-
<version>1.3.1
version>
-
dependency>
-
<dependency>
-
<groupId>org.springframework.boot
groupId>
-
<artifactId>spring-boot-starter-web
artifactId>
-
dependency>
-
-
<dependency>
-
<groupId>mysql
groupId>
-
<artifactId>mysql-connector-java
artifactId>
-
<scope>runtime
scope>
-
dependency>
-
<dependency>
-
<groupId>org.springframework.boot
groupId>
-
<artifactId>spring-boot-starter-test
artifactId>
-
<scope>test
scope>
-
dependency>
-
dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot
groupId>
-
<artifactId>spring-boot-maven-plugin
artifactId>
-
plugin>
-
plugins>
-
build>
-
-
-
project>
-
第三步:创建数据库
第四步:创建目录
第五步:创建Student.java实体类
Student.java
-
package com.example.models;
-
-
public
class Student {
-
private
int id;
-
private String name;
-
private String sex;
-
private
int age;
-
-
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 String getSex() {
-
return sex;
-
}
-
-
public void setSex(String sex) {
-
this.sex = sex;
-
}
-
-
public int getAge() {
-
return age;
-
}
-
-
public void setAge(int age) {
-
this.age = age;
-
}
-
-
@Override
-
public String toString() {
-
return
"Student{" +
-
"id=" + id +
-
", name='" + name +
'\'' +
-
", sex='" + sex +
'\'' +
-
", age=" + age +
-
'}';
-
}
-
}
第六步:创建Mapper接口
StudentMapper.java
-
package com.example.Dao;
-
-
import com.example.models.Student;
-
import org.apache.ibatis.annotations.Select;
-
import org.springframework.stereotype.Repository;
-
-
@Repository
-
public
interface StudentMapper {
-
@Select(
"SELECT * FROM student WHERE id=#{id}")
-
Student getStudentByID(int id);
-
}
第七步:创建Controller
StudentController.java
-
package com.example.Controller;
-
-
import com.example.Dao.StudentMapper;
-
import com.example.models.Student;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
-
@RestController
-
public
class StudentController {
-
@Autowired
-
private StudentMapper studentMapper;
-
-
@RequestMapping(
"/demo")
-
public Student get(){
-
Student student=studentMapper.getStudentByID(
2);
-
return student;
-
}
-
-
@RequestMapping(value =
"res")
-
public String df(){
-
return
"Hello";
-
}
-
}
(注意这边新建接口对象的时候会出错误提示,但是不影响正常运行,原因不明,如果有哪位知道怎么解决请告知)
第八步:修改主程序
DemoApplication.java
-
package com.example.demo;
-
-
import org.mybatis.spring.annotation.MapperScan;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.context.annotation.ComponentScan;
-
import org.springframework.transaction.annotation.EnableTransactionManagement;
-
-
@SpringBootApplication
-
@EnableTransactionManagement
-
@ComponentScan(
"com.example.Controller")
-
@MapperScan(
"com.example.Dao")
-
-
public
class DemoApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(DemoApplication.class, args);
-
}
-
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/world spring.datasource.username=root spring.datasource.password=a8996855439 spring.datasource.driverClassName=com.mysql.jdbc.Driver mybatis.type-aliases-package=com.example.models
结果如下,就成功了