步骤:
1.创建一个springboot项目:
2.创建项目的文件结构以及jdk的版本
3.选择项目所需要的依赖
然后点击finish
5.看一下文件的结构:
6.查看一下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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>anany</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>anany</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.项目不使用application.properties文件 而使用更加简洁的application.yml文件:
将原有的resource文件夹下的application.properties文件删除,
文件的内容如下:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=x5
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
mybatis.mapper-locations=classpath:mapper/*.xml
8.创建数据库:
9.创建controller:
package com.example.anany.controller;
import com.example.anany.entity.Student;
import com.example.anany.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//@Controller @RestController = @Controller + @ResponseBody
@RestController //springboot方法才可用,
public class StudentController {
@Autowired
StudentService ss;
@RequestMapping("query")
//public List<Student> query();
//spring boot controller方法返回值可以自动转json,ssm框架不行
public List<Student> query(Student student,Integer pageNo){
List<Student> students = ss.query(student,pageNo);
//System.out.println(students);
return students;
}
@RequestMapping("add")
//public void add(Student student);
public String add(Student student){
ss.add(student);
return "1";
}
@RequestMapping("update")
//public void update(Student student);
public void update(Student student){
ss.update(student);
}
@RequestMapping("queryById")
//public Student queryById(Integer id);
public Student queryById(Integer id){
Student student = ss.queryById(id);
System.out.println(student);
return student;
}
@RequestMapping("delete")
//public void delete(Integer id);
public void delete(Integer id){
ss.delete(id);
}
}
9.创建service:
package com.example.anany.service;
import com.example.anany.entity.Student;
import java.util.List;
public interface StudentService {
public List<Student> query(Student student,Integer pageNo);
public void add(Student student);
public void update(Student student);
public Student queryById(Integer id);
public void delete(Integer id);
}
9.创建service.impl:
package com.example.anany.service.impl;
import com.example.anany.dao.StudentDao;
import com.example.anany.entity.Student;
import com.example.anany.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentDao dao;
@Override
public List<Student> query(Student student,Integer pageNo) {
return dao.query(student,pageNo);
}
@Override
public void add(Student student) {
dao.add(student);
}
@Override
public void update(Student student) {
dao.update(student);
}
@Override
public Student queryById(Integer id) {
Student student = dao.queryById(id);
return student;
}
@Override
public void delete(Integer id) {
dao.delete(id);
}
}
10.创建dao/mapper:
package com.example.anany.dao;
import com.example.anany.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentDao {
public List<Student> query(@Param("student") Student student, @Param("pageNo") Integer pageNo);
public void add(Student student);
public void update(Student student);
public Student queryById(Integer id);
public void delete(Integer id);
}
11.创建dao/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.anany.dao.StudentDao">
<!--表单显示-->
<select id="query" resultType="com.example.anany.entity.Student">
select id,name,email,age from student1
<where>
<if test="student.name !=null and student.name != ''">
and name=#{student.name}
</if>
<if test="student.email != null and student.email != ''">
and email=#{student.email}
</if>
</where>
<if test="pageNo != null and pageNo != ''">
limit 0,#{pageNo}
</if>
</select>
<insert id="add">
insert into student1(name,email,age) values (#{name},#{email},#{age})
</insert>
<update id="update">
update student1 set name=#{name},email=#{email},age=#{age} where id=#{id}
</update>
<select id="queryById" resultType="com.example.anany.entity.Student">
select id,name,email,age from student1 where id=#{id}
</select>
<delete id="delete">
delete from student1 where id=#{id}
</delete>
</mapper>
12.程序入口,创建项目的时候系统自动创建的:
package com.example.anany;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//不用写配置文件了,直接用这个注解,就可以找到dao层的位置了
@MapperScan({"com.example.anany.dao"})
public class AnanyApplication {
public static void main(String[] args) {
SpringApplication.run(AnanyApplication.class, args);
}
}
13.在运行的时候我们只需要在浏览器中输入地址即可