目录
一、基础工程搭建
二、代码模块
1.思路
2.代码模块
1.pojo层
2mapper层
3.mybatis配置
4.controller层
总结:
接下来直接运行项目,打开浏览器输入localhost:8080+对应的网址即可。
一、基础工程搭建
1.搭建一个springboot项目
2.勾选所需的依赖配置
3.在yml文件中配置,连接数据库;以及整合mybatis的配置(数据库是事先准备好的,最后展示出来)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/1?useSSl=true&useUnicode=true&characterEncoding=Utf-8&serverTimezone=Asia/Shanghai
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.jiang.pojo
4.在测试类中测试是否连接成功(test包下的)
//注入DataSource
@Autowired
private DataSource dataSource;
@Test
void contextLoads() {
//输出数据源所在的类
System.out.println(dataSource.getClass());
}
5.连接成功
6.配置mybatis-springboot-start依赖,以及以后加载web的依赖(这里也可以在一开始创建的时候添加)
<!--非官方-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、代码模块
1.思路
pojo实体类->mapper接口->mybatis配置->controller层(此处省略了service层),目录结构如下:
2.代码模块
1.pojo层
students类
package com.jiang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
//此处事先添加了lombok插件
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Students {
private int sno;
private String name;
private String sex;
private int age;
private double number;
private String classname;
}
classes类
package com.jiang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
//此处事先添加了lombok插件
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Classes {
private String classname;
private String teacher;
private String course;
private Date date;
private List<Students> students;
}
2mapper层
StudentsMapper接口
package com.jiang.mapper;
import com.jiang.pojo.Students;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper//表示被mybatis整合
@Repository//装配bean
public interface StudentsMapper {
//查询所有学生
List<Students> selectStudents();
//添加学生
int addStudents(Students students);
//删除学生
int deleteStudents(int sno);
//更新学生
int updateStudents(Students students);
}
ClassesMapper接口
package com.jiang.mapper;
import com.jiang.pojo.Classes;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface ClassesMapper {
//查询所有课程
List<Classes> selectClasses();
//添加课程
int addClasses (Classes classes);
//删除课程
int deleteClasses (String classname);
//更新课程
int updateClasses (Classes classes);
//连接查询
}
3.mybatis配置
StudentsMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jiang.mapper.StudentsMapper">
<select id="selectStudents" resultType="Students">
select * from students
</select>
<insert id="addStudents" parameterType="Students">
insert into students(sno,name,sex,age,number,classname) values (#{sno},#{name},#{sex},#{age},#{number},#{classname})
</insert>
<delete id="deleteStudents" parameterType="int">
delete from students where sno=#{sno}
</delete>
<update id="updateStudents" parameterType="Students">
update students set name=#{name},sex=#{sex},age=#{age},number=#{number},classname=#{classname} where sno=#{sno}
</update>
</mapper>
ClassesMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jiang.mapper.ClassesMapper">
<select id="selectClasses" resultType="Classes">
select * from classes
</select>
<insert id="addClasses" parameterType="Classes">
insert into classes (classname,teacher,course,date) values (#{classname},#{teacher},#{course},#{date})
</insert>
<delete id="deleteClasses" parameterType="String">
delete from classes where classname=#{classname}
</delete>
<update id="updateClasses" parameterType="Classes">
update classes set teacher=#{teacher},course=#{course},date=#{date} classname=#{classname}
</update>
</mapper>
4.controller层
StudentsController
package com.jiang.controller;
import com.jiang.mapper.StudentsMapper;
import com.jiang.pojo.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//装配bean
@RestController
public class StudentsController {
//注入students接口
@Autowired
private StudentsMapper studentsMapper;
//实现接口中的查询方法
@GetMapping("/selectStudents")
public List<Students> selectStudents(){
List<Students> students = studentsMapper.selectStudents();
for (Students student : students) {
System.out.println(student);
}
return students;
}
//实现添加方法
@GetMapping("/addStudents")
public String addStudents(){
studentsMapper.addStudents(new Students(111,"柳西娥","女",23,1310001001,"软件一班"));
return "addOK!";
}
//实现删除方法
@GetMapping("/deleteStudents")
public String deleteStudents(){
studentsMapper.deleteStudents(111);
return "deleteOK!";
}
//实现更改方法
@GetMapping("updateStudents")
public String updateStudents(){
studentsMapper.updateStudents(new Students(111,"柳西娥","女",23,1310001011,"软件一班"));
return "updateOK!";
}
}
ClassesController
package com.jiang.controller;
import com.jiang.mapper.ClassesMapper;
import com.jiang.mapper.StudentsMapper;
import com.jiang.pojo.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ClassesController {
@Autowired
private ClassesMapper classesMapper;
@Autowired
private StudentsMapper studentsMapper;
@GetMapping("/selectClasses")
public List<Classes> selectClasses(){
List<Classes> classes = classesMapper.selectClasses();
for (Classes aClass : classes) {
System.out.println(aClass);
}
return classes;
}
@GetMapping("/addClasses")
public String addClasses(){
classesMapper.addClasses(new Classes());
return "addOK!";
}
@GetMapping("/deleteClasses")
public String deleteClasses(){
classesMapper.deleteClasses("软件二班");
return "deleteOK!";
}
@GetMapping("/updateClasses")
public String updateClasses(){
classesMapper.updateClasses(new Classes());
return "updateOK!";
}
}
总结:
接下来直接运行项目,打开浏览器输入localhost:8080+对应的网址即可。
相关文章
暂无评论...