-
创建项目
修改pom.xml 为2.7.5
-
引入mybatisplus
2.1 修改pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--C标签库得使用-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--如下是为了配置,可以正常访问jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</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部分,这里不再追叙
具体代码请去资源中下载