**
1.pom文件需要导入的包:
<dependencies>
<!-- springboot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
2.新建表:
我只添加了简单的id,names和much
3.application.properties文件配置如下:
准备工作做完了,下面是代码:
4.实体类—>guanli.java:
public class Guanli {
private Integer id;
private String names;
private String much;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getMuch() {
return much;
}
public void setMuch(String much) {
this.much = much;
}
}
5.guanlimapper.java
public interface GuanliMapper {
//查找所有
List<Guanli> selectAll();
//添加
void insertUser(Guanli guanli);
//删除
void deleteUserById(Integer id);
//根据id查询
Guanli selectUsersById(Integer id);
//修改
void updateUser(Guanli guanli);
}
6.GaunliMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.it.boot.mapper.GuanliMapper">
<!-- 查询所有 -->
<select id="selectAll" resultType="com.it.boot.pojo.Guanli">
SELECT * FROM guanli
</select>
<!--添加-->
<insert id="insertUser" parameterType="com.it.boot.pojo.Guanli">
INSERT INTO mybatis1.guanli(names,much) VALUES(#{names},#{much})
</insert>
<!--删除-->
<delete id="deleteUserById" >
delete from mybatis1.guanli where id = #{value}
</delete>
<!--根据id查找-->
<select id="selectUsersById" resultType="com.it.boot.pojo.Guanli">
SELECT id,names,much FROM mybatis1.guanli where id=#{value}
</select>
<!--修改-->
<update id="updateUser" parameterType="com.it.boot.pojo.Guanli">
update mybatis1.guanli set names=#{names},much=#{much} where id=#{id}
</update>
</mapper>
7.GuanliService.java
ps:记得一定要添加@service注解,我就出过这样的错
@Service
@Transactional
public class GuanliService {
@Autowired
private GuanliMapper guanliMapper;
//管理界面查看全部信息
public List<Guanli> findUserAll() {
return this.guanliMapper.selectAll();
}
//添加
public void insert(Guanli guanli){
this.guanliMapper.insertUser(guanli);
}
//删除
public void deleteUserById(Integer id) {
this.guanliMapper.deleteUserById(id);
}
//根据id查询
public Guanli findUserById(Integer id) {
return this.guanliMapper.selectUsersById(id);
}
//修改
public void updateUser(Guanli guanli) {
this.guanliMapper.updateUser(guanli);
}
}
8.GuanliController.java
@Controller
@RequestMapping("/users")
public class GuanliController {
@Autowired
private GuanliService guanliService;
/*
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/*
* 查询全部用户
*/
@RequestMapping("/inset")
public String findUserAll(Model model){
List<Guanli> list = this.guanliService.findUserAll();
model.addAttribute("list", list);
return "guanli";
}
/*
添加
*/
@RequestMapping("/addinsert")
public String addinsert(Guanli guanli){
this.guanliService.insert(guanli);
return "ok";
}
//删除
@RequestMapping("/delUser")
public String delUser(Integer id){
this.guanliService.deleteUserById(id);
return "redirect:/users/inset";
}
//根据id查询
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model){
Guanli guanli = this.guanliService.findUserById(id);
model.addAttribute("guanli", guanli);
return "updateUser";
}
/*
* 更新用户
*/
@RequestMapping("/update")
public String editUser(Guanli guanli){
this.guanliService.updateUser(guanli);
return "ok";
}
}
9.启动类:App.java
@SpringBootApplication
@MapperScan("com.it.boot.mapper")
// 用于扫描mybatis的mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
下面是所有的页面:
1.查询表中所有信息:guanli.html
输入:http://localhost:8080/users/inset
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>管理界面</title>
</head>
<body>
<a href="/users/tianjia.html" >添加</a><!--点击跳转到添加页面-->
<table border="1">
<tr>
<th>ID</th>
<th>课程名称</th>
<th>价格</th>
<!--<th>操作</th>-->
</tr>
<tr th:each="guanli : ${list}">
<td th:text="${guanli.id}"></td>
<td th:text="${guanli.names}"></td>
<td th:text="${guanli.much}"></td>
<td>
<a th:href="@{/users/findUserById(id=${guanli.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${guanli.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8">
<title>添加课程页面</title>
</head>
<body>
<form th:action="@{/users/addinsert}" method="post">
课程名称:<input type="text" name="names"/>
课程价格:<input type="text" name="much"/>
<input type="submit" value="确定"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改课程页面</title>
</head>
<body>
<form th:action="@{/users/update}" method="post">
<input type="hidden" name="id" th:field="${guanli.id}"/>
用户姓名:<input type="text" name="names" th:field="${guanli.names}"/>
用户年龄:<input type="text" name="much" th:field="${guanli.much}"/>
<input type="submit" value="确定"/>
</form>
</body>
</html>
4.还有一个操作成功提示页面:
ps:我想修改成操作成功5秒后跳转到查询所有信息的页面,正在更新大家稍等
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<a href="/users/inset" >管理页面</a>
<body>
操作成功!!!
</body>
</html>