maven仓库配置:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
application.properties配置:
server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=1234
logging.level.com.yjj.mapper=debug
mybatis.mapper-locations=classpath:com/yjj/mapper/*.xml
#设置数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
实体类Dept:
Dept.java:
/**Getter,Setter,equals,canEqual,hasCode,toString*/
@Data
/**生成一个无参数的构造方法*/
@NoArgsConstructor
/**会生成一个包含所有变量*/
@AllArgsConstructor
public class Dept implements Serializable {
private int deptno;
private String dname;
private String loc;
public Dept(String dname, String loc) {
this.dname = dname;
this.loc = loc;
}
}
mapper:
DeptMapper.java:
@Mapper
public interface DeptMapper {
/**
* 查询全部
* @return
*/
List<Dept> queryAll();
/**
* 根据id删除
* @param deptno
*/
void delete(int deptno);
/**
* 添加
* @param dept
*/
void insert(Dept dept);
/**
* 根据id查询
* @param deptno
* @return
*/
Dept queryByDeptno(int deptno);
/**
* 修改
* @param dept
*/
void update(Dept dept);
}
DeptMapper.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//http://mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yjj.mapper.DeptMapper">
<!--查询全部-->
<select id="queryAll" resultType="com.yjj.entity.Dept" >
SELECT
deptno,
dname,
loc
FROM
dept
</select>
<!-- 根据id删除 -->
<delete id="delete">
DELETE
FROM
dept
WHERE
deptno = #{deptno}
</delete>
<!--添加-->
<insert id="insert" >
INSERT INTO dept (dname, loc)
VALUES
(#{dname}, #{loc})
</insert>
<!-- 根据id查询 -->
<select id="queryByDeptno" resultType="com.yjj.entity.Dept">
SELECT
deptno,
dname,
loc
FROM
dept
WHERE
deptno = #{deptno}
</select>
<!--修改-->
<update id="update">
UPDATE dept
<set>
<if test=" dname != null and dname != '' ">
dname=#{dname},
</if>
<if test=" loc != null and loc != '' ">
loc=#{loc},
</if>
</set>
WHERE
deptno = #{deptno}
</update>
</mapper>
service:
DeptService.java:
public interface DeptService {
/**
* 查询全部
* @return
*/
List<Dept> queryAll();
/**
* 根据id删除
* @param deptno
*/
void delete(int deptno);
/**
* 添加
* @param dept
*/
void insert(Dept dept);
/**
* 根据id查询
* @param deptno
* @return
*/
Dept queryByDeptno(int deptno);
/**
* 修改
* @param dept
*/
void update(Dept dept);
}
service.impl:
DeptSereviceImpl.java:
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public List<Dept> queryAll() {
return deptMapper.queryAll();
}
@Override
public void delete(int deptno) {
deptMapper.delete(deptno);
}
@Override
public void insert(Dept dept) {
deptMapper.insert(dept);
}
@Override
public Dept queryByDeptno(int deptno) {
return deptMapper.queryByDeptno(deptno);
}
@Override
public void update(Dept dept) {
deptMapper.update(dept);
}
}
controller:
DeptController :
@Controller
@RequestMapping("dept")
public class DeptController {
@Autowired
private DeptService deptService;
/**
* 查询全部
* @param map
* @return
*/
@RequestMapping("queryAll")
public String queryAll(ModelMap map){
//System.out.println(deptService.queryAll().toString());
map.addAttribute("deptList",deptService.queryAll());
return "query";
}
/**
* 删除
* @param deptno
* @return
*/
@RequestMapping("delete")
public String delete(int deptno){
deptService.delete(deptno);
return "redirect:/dept/queryAll";
}
/**
* 根据id查询
* @param deptno
* @param map
* @return
*/
@RequestMapping("queryByDeptno")
public String queryByDeptno(int deptno,ModelMap map){
map.addAttribute("dept",deptService.queryByDeptno(deptno));
return "update";
}
/**
* 修改
* @param dept
* @return
*/
@RequestMapping("update")
public String update(Dept dept){
deptService.update(dept);
return "redirect:/dept/queryAll";
}
/**
* 添加
* @param dept
* @return
*/
@RequestMapping("insert")
public String insert(Dept dept){
deptService.insert(dept);
return "redirect:/dept/queryAll";
}
}
前端页面:
query.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>查询全部</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" width="600px">
<tr align="right">
<td colspan="4"><a th:href="@{/add.html}">添加</a></td>
</tr>
<tr align="center">
<td>deptno</td>
<td>dname</td>
<td>loc</td>
<td>操作</td>
</tr>
<tr align="center" th:each="d:${deptList}">
<td th:text="${d.deptno}">deptno</td>
<td th:text="${d.dname}">dname</td>
<td th:text="${d.loc}">loc</td>
<td>
<a th:href="@{'/dept/queryByDeptno?deptno='+${d.deptno}}">修改</a>
<a th:href="@{'/dept/delete?deptno='+${d.deptno}}">删除</a>
</td>
</tr>
</table>
</body>
</html>
updata.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改</title>
</head>
<body>
<form th:action="@{'/dept/update'}" method="post">
deptno: <input type="text" name="deptno" th:value="${dept.deptno}" readonly><br>
dname: <input type="text" name="dname" th:value="${dept.dname}"><br>
loc: <input type="text" name="loc" th:value="${dept.loc}"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
add.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加</title>
</head>
<body>
<form action="/dept/insert" method="post">
dname: <input type="text" name="dname" ><br>
loc: <input type="text" name="loc" ><br>
<input type="submit" value="添加">
</form>
</body>
</html>
数据库:
dept.sql:
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`DEPTNO` int(11) NOT NULL AUTO_INCREMENT,
`DNAME` varchar(255) DEFAULT NULL,
`loc` varchar(255) DEFAULT NULL,
PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of dept
-- ----------------------------
INSERT INTO `dept` VALUES ('10', 'ACCOUNTING', 'NEW YORK');
INSERT INTO `dept` VALUES ('20', 'RESEARCH', 'DALLAS');
INSERT INTO `dept` VALUES ('30', 'SALES', 'CHICAGO');
INSERT INTO `dept` VALUES ('40', 'OPERATIONS', 'BOSTON');
INSERT INTO `dept` VALUES ('48', '123123', '123123');
INSERT INTO `dept` VALUES ('52', '123', '123');
INSERT INTO `dept` VALUES ('53', '1', '1');
项目结构: