1.创建springboot项目
在创建项目的时候选择SpringBootWeb,JDBC API,数据库驱动(我用mysql就选择MySQL Driver)
在pom.xml文件里面添加需要的依赖,也就是添加有注释那几条
<dependencies>
<!--mybatis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.创建数据库
此处我简单的创建了一个数据库
3.创建对应的实体类
package com.example.druid.substance;
public class Student {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
4.创建Mapper目录
在resources里创建一个Mapper目录用于写Mapper文件
5.配置文件的设置
此处使用的是yaml格式配置文件
需要配置数据库信息,数据源,扫描mapper和实体类
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_boot?serverTimezone=UTC
filters: stat,wall
mybatis:
type-aliases-package: com.example.druid.substance
mapper-locations: classpath:mybatis/mapper/*.xml
6.三层的代码
前端页面:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/SayHello">test</a>
</body>
</html>
如果是和Vue一起使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="demo">
<input type="button" value="查询学生" @click="queryStudents">
<p v-for="student in students">
{{ student.name }}
{{ student.age }}
</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var app = new Vue({
el:"#demo",
data:{
students:"查询到的学生"
},
methods:{
queryStudents:function(){
var now = this;
axios.get("/SayHello")
.then(function(response){
console.log(response.data);
now.students = response.data;
},function(err){
})
}
}
})
</script>
</body>
</html>
Controller
package com.example.druid.controller;
import com.example.druid.mapper.StudentMapper;
import com.example.druid.service.ISayHello;
import com.example.druid.service.serviceImp.SayHelloImp;
import com.example.druid.substance.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class SayHelloController {
@Autowired
private ISayHello sayHello;
@RequestMapping("SayHello")
@ResponseBody
public List<Student> SayHello(){
List<Student> list = sayHello.querryAllStudents();
return list;
}
}
Service接口
package com.example.druid.service;
import com.example.druid.substance.Student;
import java.util.List;
public interface ISayHello {
List<Student> querryAllStudents();
}
Service实现类
package com.example.druid.service.serviceImp;
import com.example.druid.mapper.StudentMapper;
import com.example.druid.service.ISayHello;
import com.example.druid.substance.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SayHelloImp implements ISayHello {
@Autowired
StudentMapper studentMapper;
@Override
public List<Student> querryAllStudents() {
List<Student> list = studentMapper.queryAllStudents();
return list;
}
}
mapper
package com.example.druid.mapper;
import com.example.druid.substance.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface StudentMapper {
List<Student> queryAllStudents();//查询所有学生
}
Mapper的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.example.druid.mapper.StudentMapper">
<select id="queryAllStudents" resultType="com.example.druid.substance.Student">
select * from student
</select>
</mapper>
运行的主类
记得加上扫描注解
package com.example.druid;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.druid.mapper")
@SpringBootApplication
public class DruidApplication {
public static void main(String[] args) {
SpringApplication.run(DruidApplication.class, args);
}
}