创建 SpringBoot 项目
配置application.properties
根据自己数据库进行配置
spring.datasource.name=exam
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://192.168.153.143:3306/exam
运行启动类
数据库配置完成
补齐目录
pojo层
Student.java
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student {
private Integer id;
private String name;
private Integer age;
private String gender;
private String telephone;
private String email;
private Integer classId;
private String className;
}
dao层
ExamDao.java
package nj.zb.kb21.springboot.dao;
import nj.zb.kb21.springboot.pojo.Classes;
import nj.zb.kb21.springboot.pojo.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ExamDao {
List<Student> getAllStudent();
Integer addStudent(Student student);
}
在启动类添加maScan注解
@MapperScan
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加,
mappers层
ExamDao.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="nj.zb.kb21.springboot.dao.ExamDao">
<insert id="addStudent">
insert into exam.student
(id, name, age, gender, telephone, email, classid)
values (null, #{name}, #{age}, #{gender}, #{telephone}, #{email}, #{classId})
</insert>
<select id="getAllStudent" resultType="nj.zb.kb21.springboot.pojo.Student">
select s.id,
s.name,
s.age,
s.gender,
s.telephone,
s.email,
s.classid,
c.name classesName
from exam.student s
left join classes c on s.classid = c.id
</select>
</mapper>
在application.properities后添加配置
spring.datasource.name=exam
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://192.168.153.143:3306/exam
mybatis.mapper-locations=classpath:/mappers/*.xml
//用于将配置路径下的*.xml文件加载到mybatis中
Service层
ExamService.java
public interface ExamService {
/**
* 查询所有学生信息及班级信息
* @return
*/
public List<Student> getAllStudent();
/**
* 添加新的学生信息
* @param student
* @return
*/
public Integer addStudent(Student student);
}
ExamServiceImpl.java
@Service
@Transactional
public class ExamServiceImpl implements ExamService {
@Autowired
private ExamDao examDao;
@Override
public List<Student> getAllStudent() {
return examDao.getAllStudent();
}
@Override
public Integer addStudent(Student student) {
return examDao.addStudent(student);
}
}
Controller层
ExamController.java
@Controller
@RequestMapping(value = "/exam")
public class ExamController {
@Autowired
private ExamService examService;
@GetMapping("/allstu")
@ResponseBody
public List<Student> getAllStudent() {
List<Student> allStudent = examService.getAllStudent();
return allStudent;
}
@RequestMapping("addstu")
@ResponseBody
public Integer saveStudent(Student student) {
Integer integer = examService.addStudent(student);
return integer;
}
}
运行程序
在浏览器输入:http://localhost:8080/exam/allstu