SpringBoot和Mybatis整合例一
- 目录结构图:
- 每个包对应的代码:
主启动类:
package com.hn.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.hn.springbootmybatis.mapper")
@SpringBootApplication
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
controller包:
package com.hn.springbootmybatis.controller;
import com.hn.springbootmybatis.pojo.Student;
import com.hn.springbootmybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Iterator;
import java.util.List;
@RestController
@RequestMapping("/")
public class PageController {
@Autowired
private StudentService studentService;
@RequestMapping("hello")
public String hello(){
return "Hello world";
}
@RequestMapping("/query")
public String queryStudent(){
List<Student> list = studentService.queryStudents();
if (list.size() ==0 || list==null){
return "没有查到任何的信息";
}
Iterator it = list.iterator();
while (it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
return "查到学生信息";
}
}
mapper包:
package com.hn.springbootmybatis.mapper;
import com.hn.springbootmybatis.pojo.Student;
import java.util.List;
/*
这里的mapper注解一定不能去掉
*/
//@Mapper
public interface StudentMapper {
//查询所有的学生
//@Select("select * from student")
List<Student> queryStudents();
}
pojo包:
package com.hn.springbootmybatis.pojo;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
private String studentno;
private String studentname;
private String loginpwd;
private Character sex;
private Integer majorid;
private String phone;
private String email;
private Date borndate;
public Student() {
}
public Student(String studentno, String studentname, String loginpwd, Character sex, Integer majorid, String phone, String email, Date borndate) {
this.studentno = studentno;
this.studentname = studentname;
this.loginpwd = loginpwd;
this.sex = sex;
this.majorid = majorid;
this.phone = phone;
this.email = email;
this.borndate = borndate;
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getLoginpwd() {
return loginpwd;
}
public void setLoginpwd(String loginpwd) {
this.loginpwd = loginpwd;
}
public Character getSex() {
return sex;
}
public void setSex(Character sex) {
this.sex = sex;
}
public Integer getMajorid() {
return majorid;
}
public void setMajorid(Integer majorid) {
this.majorid = majorid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBorndate() {
return borndate;
}
public void setBorndate(Date borndate) {
this.borndate = borndate;
}
@Override
public String toString() {
return "Student{" +
"studentno='" + studentno + '\'' +
", studentname='" + studentname + '\'' +
", loginpwd='" + loginpwd + '\'' +
", sex=" + sex +
", majorid=" + majorid +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", borndate=" + borndate +
'}';
}
}
service包:
package com.hn.springbootmybatis.service;
import com.hn.springbootmybatis.mapper.StudentMapper;
import com.hn.springbootmybatis.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> queryStudents() {
try {
List<Student> list = studentMapper.queryStudents();
System.out.println("list的大小是:"+list.size());
return list;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
mybatis-mapper包:
这是StudentMapper.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.hn.springbootmybatis.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.hn.springbootmybatis.pojo.Student">
<result column="studentno" property="studentno"></result>
<result column="studentname" property="studentno"></result>
<result column="loginpwd" property="loginpwd"></result>
<result column="sex" property="sex"></result>
<result column="majorid" property="majorid"></result>
<result column="phone" property="phone"></result>
<result column="email" property="email"></result>
<result column="borndate" property="borndate"></result>
</resultMap>
<select id="queryStudents" resultType="com.hn.springbootmybatis.pojo.Student">
select * from student;
</select>
</mapper>
最后一个是application.properties配置文件:
#springboot配置
spring.application.name=spring-boot-mybatis #服务的名字
spring.rsocket.server.port=8080 #服务启动端口号
#mybatis配置
mybatis.mapper-locations=classpath:mybatis-mapper/*.xml
mybatis.type-aliases-package=com.hn.springbootmybatis.pojo
#数据库连接配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/student?characterEncoding=UTF-8&serverTimezone=UTC&ssl=false
spring.datasource.username=root #你的数据库名字
spring.datasource.password=root #你的数组库密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
数据库的创建表语句:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`studentno` varchar(10) NOT NULL,
`studentname` varchar(20) NOT NULL,
`loginpwd` varchar(8) NOT NULL,
`sex` char(1) DEFAULT NULL,
`majorid` int(11) NOT NULL,
`phone` varchar(11) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
`borndate` datetime DEFAULT NULL,
PRIMARY KEY (`studentno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `student` VALUES ('S001', '张三封', '8888', '男', '1', '13288886666', 'zhangsanfeng@126.com', '1966-09-01 00:00:00');
INSERT INTO `student` VALUES ('S002', '殷天正', '8888', '男', '1', '13888881234', 'yintianzheng@qq.com', '1976-09-02 00:00:00');
INSERT INTO `student` VALUES ('S003', '周伯通', '8888', '男', '2', '13288886666', 'zhoubotong@126.com', '1986-09-03 00:00:00');
INSERT INTO `student` VALUES ('S004', '张翠山', '8888', '男', '1', '13288886666', null, '1995-09-04 00:00:00');
INSERT INTO `student` VALUES ('S005', '小小张', '8888', '女', '3', '13288885678', 'xiaozhang@126.com', '1990-09-05 00:00:00');
INSERT INTO `student` VALUES ('S006', '张无忌', '8888', '男', '2', '13288886666', 'zhangwuji@126.com', '1998-08-09 00:00:00');
效果展示图:
到此项目已经搭建完毕!
完整项目下载地址:https://gitlab.com/oxLoveRain/springboot-mybatis.git