文章目录
- 前言
- 一、Springboot+模板引擎+mybatis
- 二、使用步骤
- 1.导入依赖
- 总结
- 1.导入依赖
前言
提示:喜欢就点个赞哦
提示:以下是本篇文章正文内容,下面案例可供参考
一、springboot+模板引擎+mysql+整合mybatis
示例:为了方便
二、使用步骤
1.导入依赖
1.导入依赖
代码如下(示例):pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.6 com.oyjl spring-boot-name0.0.1-SNAPSHOT spring-boot-name Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-webcom.alibaba druid1.2.6 mysql mysql-connector-javaorg.projectlombok lomboktrue org.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 org.springframework.boot spring-boot-starter-testtest org.junit.platform junit-platform-commonsorg.apache.commons commons-lang33.6 org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
编写实体类
package com.oyjl.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true) //链式编程
public class Users {
private Integer id;
private String name;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Mapper接口
package com.oyjl.Mapper;
import com.oyjl.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@SuppressWarnings("all")
@Mapper
public interface UsersMapper {
List finAll();
Users findUserById(int id);
int addUser(Users user);
int update(Users user);
int delete(int id);
}
Svrivce接口
package com.oyjl.Service;
import com.oyjl.pojo.Users;
import java.util.List;
public interface UsersService {
List finAll();
Users findUserById(int id);
int addUser(Users user);
int update(Users user);
int delete(int id);
}
Svrivceimpl类
package com.oyjl.Service.impl;
import com.oyjl.Mapper.UsersMapper;
import com.oyjl.Service.UsersService;
import com.oyjl.pojo.Users;
import jdk.internal.instrumentation.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.swing.*;
import java.util.List;
@SuppressWarnings("all")
@Slf4j
@Service
public class UsersServiceimpl implements UsersService {
@Autowired
public UsersMapper usersMapper;
@Override
public List finAll() {
List users = usersMapper.finAll();
return users;
}
@Override
public Users findUserById(int id) {
return usersMapper.findUserById(id);
}
@Override
public int addUser(Users user) {
return usersMapper.addUser(user);
}
@Override
public int update(Users user) {
return usersMapper.update(user);
}
@Override
public int delete(int id) {
return usersMapper.delete(id);
}
}
mybatis.xml
insert into table_user(name,password,age) values (#{name}, #{password}, #{age}) update table_user set name = #{name},password = #{password},age = #{age} where id = #{id} delete from table_user where id=#{id}
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1/itveteran?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.oyjl.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
controller类
package com.oyjl.conrotller;
import com.oyjl.Service.UsersService;
import com.oyjl.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;
@SuppressWarnings("all")
@Controller
public class Userscontroller {
@Autowired
public UsersService usersService;
@RequestMapping("/allout")
public String finAll(Model model){
List list = usersService.finAll();
model.addAttribute("list",list);
return "/indexs.html";
}
//添加
@RequestMapping("/toAdd")
public String toAddPaper() {
return "adduser";
}
@RequestMapping("/add")
public String addPaper(Users user) {
System.out.println(user);
usersService.addUser(user);
return "redirect:/allout";
}
//修改
@RequestMapping("/toEdit")
public String toEdit(Model model,int id) {
Users users=usersService.findUserById(id);
model.addAttribute("user", users);
return "updateBook";
}
@RequestMapping("/edit")
public String edit(Users user) {
usersService.update(user);
return "redirect:/allout";
}
//删除
@RequestMapping("/delete")
public String delete(int id) {
usersService.delete(id);
return "redirect:/allout";
}
}
查询全部页面
书籍列表
用户列表
新增
| 用户ID | 用户名 | 密码 | 年龄 | 操作 |
|---|---|---|---|---|
| 更改 | 删除 |
修改页面
>
修改信息
修改信息
新增页面
修改信息
添加用户
页面就不一一发出来了
2.项目结构
总结
这个要多练,加油吧



