ssm mysql多表查询_3.SSM整合_多表_一对多的增删改查

1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件,一对多反过来就是多对一

接口

public interface CategoryMapper {

public void addCategory(Category category);

public void delCategory(String id);

public void updateCategory(Category cateory);

//根据name模糊查询

public List selectCategory(String name);

//查询所有

public List selectAll();

//根据id查询

public Category selectById(String id);

//查询分类下的所有信息

public List getNewsWithCate(String id);

}

映射文件

insert into t_category(name, createtime)

value(#{name}, #{createtime});

delete from t_category

where id = #{id}

update t_category

set name = #{name}, createtime = #{createtime}

where id = #{id}

select t1.id Id, t1.name Name, t1.createtime Createtime,

t2.id NewId, t2.title Title, t2.author Author

from t_category t1

left join t_news t2

on t1.id = t2.category_id

where t1.name like "%"#{name}"%"

select t1.id Id, t1.name Name, t1.createtime Createtime,

t2.id NewId, t2.title Title, t2.author Author

from t_category t1

left join t_news t2

on t1.id = t2.category_id

select id, title, author

from t_news

where category_id = #{id}

select id, name ,createtime

from t_category

where id = #{id}

select *

from t_news

where category_id = #{id}

实体类

public class Category {

private Integer id;

private String name;

private String createtime;

//一对多关联

private List news;

//省略getter和setter

接口

public interface NewsMapper {

public void addNew(News news);

public void delNew(String id);

public void updateNew(News news);

//模糊查询

public List selectNew(String name);

//查询所有

public List selectAll();

//根据id查询

public News selectById(String id);

//获取分类的名称

public List getCategoryName();

}

映射文件

insert into t_news(content, title, author, createtime, category_id)

value(#{content},#{title},#{author},#{createtime},#{category.id})

delete from t_news where id = #{id}

update t_news

set

content = #{content},

title = #{title},

author = #{author},

createtime = #{createtime},

category_id = #{category.id}

where

id = #{id}

select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime,

t2.id Categoryid, t2.name Name

from t_news t1

left join t_category t2

on t1.category_id = t2.id

where t1.title like "%"#{name}"%"

select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime,

t2.id Categoryid, t2.name Name

from t_news t1

left join t_category t2

on t1.category_id = t2.id

select id, name

from t_category

where id = #{id}

select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime, t1.category_id category_id

from t_news t1

where t1.id = #{id}

select id, name

from t_category

实体类

public class News {

private Integer id;

private String content;

private String title;

private String createtime;

private Category category;//外键

private String author;

//省略getter和setter

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了实现SSM框架下的多表增删改查操作,我们首先需要进行以下几个步骤: 1. 创建数据库表 首先,我们需要在数据库中创建学生表(student)、教室表(classroom)和教师表(teacher)。这些表应该包含相应的字段,例如学生表可以包含学生ID、姓名、年龄等字段,教室表可以包含教室ID、名称、容量等字段,教师表可以包含教师ID、姓名、职称等字段。 2. 创建实体类 在Java代码中,我们需要创建与数据库表对应的实体类。分别创建名为Student、Classroom和Teacher的实体类,并在类中定义相应的属性和方法。 3. 创建Mapper接口 在SSM框架中,Mapper接口用于定义与数据库交互的方法。我们需要创建名为StudentMapper、ClassroomMapper和TeacherMapper的接口,并在接口中定义增删改查等方法。 4. 创建Mapper.xml文件 在resources目录下创建与Mapper接口对应的Mapper.xml文件,用于编写SQL语句和实现与数据库的交互。 5. 创建Service接口和实现类 创建名为StudentService、ClassroomService和TeacherService的接口,并在接口中定义对应的增删改查方法。然后创建相应的实现类StudentServiceImpl、ClassroomServiceImpl和TeacherServiceImpl,并在实现类中注入Mapper接口,实现具体的业务逻辑。 6. 创建Controller类 创建名为StudentController、ClassroomController和TeacherController的Controller类,用于处理前端请求。在Controller类中注入Service接口,并定义相应的请求路径和方法。 具体的代码实现过程可以参考下面的示例: StudentMapper.xml 文件示例: ```xml <!-- 增加学生 --> <insert id="addStudent" parameterType="com.example.model.Student"> INSERT INTO student (id, name, age) VALUES (#{id}, #{name}, #{age}) </insert> <!-- 删除学生 --> <delete id="deleteStudent" parameterType="int"> DELETE FROM student WHERE id = #{id} </delete> <!-- 更新学生 --> <update id="updateStudent" parameterType="com.example.model.Student"> UPDATE student SET name = #{name}, age = #{age} WHERE id = #{id} </update> <!-- 查询学生 --> <select id="getStudentById" parameterType="int" resultType="com.example.model.Student"> SELECT id, name, age FROM student WHERE id = #{id} </select> ``` StudentMapper.java 文件示例: ```java public interface StudentMapper { // 增加学生 void addStudent(Student student); // 删除学生 void deleteStudent(int id); // 更新学生 void updateStudent(Student student); // 查询学生 Student getStudentById(int id); } ``` StudentServiceImpl.java 文件示例: ```java @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; @Override public void addStudent(Student student) { studentMapper.addStudent(student); } @Override public void deleteStudent(int id) { studentMapper.deleteStudent(id); } @Override public void updateStudent(Student student) { studentMapper.updateStudent(student); } @Override public Student getStudentById(int id) { return studentMapper.getStudentById(id); } } ``` StudentController.java 文件示例: ```java @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @PostMapping("/add") public String addStudent(@RequestBody Student student) { studentService.addStudent(student); return "添加成功"; } @DeleteMapping("/delete/{id}") public String deleteStudent(@PathVariable("id") int id) { studentService.deleteStudent(id); return "删除成功"; } @PutMapping("/update") public String updateStudent(@RequestBody Student student) { studentService.updateStudent(student); return "更新成功"; } @GetMapping("/get/{id}") public Student getStudentById(@PathVariable("id") int id) { return studentService.getStudentById(id); } } ``` 通过以上步骤,我们就可以在SSM框架下实现多表增删改查操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值