idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查

  1. 创建项目
    idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查_第1张图片
    idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查_第2张图片
    修改pom.xml 为2.7.5
    idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查_第3张图片

  2. 引入mybatisplus
    2.1 修改pom.xml

<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.2version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.29version>
        dependency>

        
        <dependency>
            <groupId>jstlgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        
        <dependency>
            <groupId>org.apache.tomcat.embedgroupId>
            <artifactId>tomcat-embed-jasperartifactId>
        dependency>

2.2 修改application.properties 如下内容:

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/jdbc08
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml

2.3src下的实体类
配置表名
配置主键

@Data
@TableName("tb_dept")
public class DeptBean implements Serializable {
    @TableId(value = "did", type = IdType.ASSIGN_ID)
    private int did;
    private String dname;

}

2.4 src下的mapper

@Mapper
public interface DeptMapper extends BaseMapper<DeptBean> {

}

2.5 src下的service

@Service
public class DeptService {

    @Autowired
    private DeptMapper deptMapper;

    public void addDeptService(DeptBean name){
        deptMapper.insert(name);
    }

    public void delDeptService(int did){
        deptMapper.deleteById(did);
    }

    public void updateDeptService(DeptBean deptBean){
        deptMapper.updateById(deptBean);
    }

    public DeptBean byidDeptService(int did){
        return deptMapper.selectById(did);
    }

    public List<DeptBean> allDeptService(){
        return deptMapper.selectList(null);
    }

}

2.6 src下的controller

@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;



    @RequestMapping("addDept")
    public String addDept(DeptBean dname){
        deptService.addDeptService(dname);
        return "allDept";
    }

    @RequestMapping("allDept")
    public String allDept(Model model){
        model.addAttribute("alldept",deptService.allDeptService());
        return "deptAll.jsp";
    }

    @RequestMapping("byidDept")
    public String byidDept(int id ,Model model){
        System.out.println(id);
        model.addAttribute("byidDept",deptService.byidDeptService(id));
        return "deptUpdate.jsp";
    }

    @RequestMapping("delDept")
    public String delDept(int id){
        deptService.delDeptService(id);
        return "allDept";
    }

    @RequestMapping("updateDept")
    public String updateDept(DeptBean deptBean){
        deptService.updateDeptService(deptBean);
        return "allDept";
    }
}

2.7 关于jsp部分,请参考 https://blog.csdn.net/zhangting123123/article/details/132368168?spm=1001.2014.3001.5501 中的jsp部分,这里不再追叙

具体代码请去资源中下载

你可能感兴趣的:(java,开发语言)