SpringBoot學習筆記(8)——SpringBoot整合SpringMVC+MyBatis

SpringBoot學習筆記(8)——SpringBoot整合SpringMVC+MyBatis

一、創建項目

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>
		<!-- mysql數據庫驅動 -->
		<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>
	</dependencies>

2. 添加application.properties全局配置文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.type-aliases-package=com.bjsxt.pojo

其中,url中的ssm爲庫名

3. 數據庫表設計

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

二、添加用戶

1. 創建實體類

public class Users {
	private Integer id;
	private String name;
	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 Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

2. 創建mapper接口以及映射配置文件

創建UsersMapper接口:

public interface UsersMapper {
	void insertUser(Users users);
}

創建UsersMapper.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.bjsxt.mapper.UsersMapper">
	<insert id="insertUser" parameterType="users">
		insert into users(name,age) values(#{name},#{age})
	</insert>
</mapper>

3. 創建業務層

@Service
@Transactional
public class UsersServiceImpl implements UsersService {
	
	@Autowired
	private UsersMapper usersMapper;
	
	@Override
	public void addUser(Users users) {
		this.usersMapper.insertUser(users);
	}
}

4. 創建Controller

@Controller
@RequestMapping("/users")
public class UsersController {

	@Autowired
	private UsersService usersService;
	
	/**
	 * 頁面跳轉
	 */
	@RequestMapping("/{page}")
	public String showPage(@PathVariable String page){
		return page;
	}
	
	@RequestMapping("/addUser")
	public String addUser(Users users){
		this.usersService.addUser(users);
		return "ok";
	}
}

5. 編寫頁面

input.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用戶</title>
</head>
<body>
	<form th:action="@{/users/addUser}" method="post">
		用戶姓名:<input type="text" name="name"/><br/>
		用戶年齡:<input type="text" name="age"/><br/>
		<input type="submit" value="確定"/><br/>
	</form>
</body>
</html>

ok.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示頁面</title>
</head>
<body>
	操作成功!!!
</body>
</html>

6. 啓動類

@SpringBootApplication
@MapperScan("com.bjsxt.mapper") //@MapperScan 用戶掃描MyBatis的Mapper接口
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

三、查詢用戶

1. 在mapper接口以及映射配置文件中添加相關代碼

	List<Users> selectUsersAll();
	<select id="selectUsersAll" resultType="users">
		select id,name,age from users
	</select>

2. 在業務層中添加查詢方法

	@Override
	public List<Users> findUserAll() {
		return this.usersMapper.selectUsersAll();
	}

3. 在Controller中添加方法

	@RequestMapping("/findUserAll")
	public String findUserAll(Model model){
		List<Users> list = this.usersService.findUserAll();
		model.addAttribute("list", list);
		return "showUsers";
	}

4. 添加頁面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用戶數據</title>
</head>
<body>
	<table border="1" style="width:300px;">
		<tr>
			<th>用戶ID</th>
			<th>用戶姓名</th>
			<th>用戶年齡</th>
		</tr>
		<tr th:each="user : ${list}">
			<td th:text="${user.id}"></td>
			<td th:text="${user.name}"></td>
			<td th:text="${user.age}"></td>
		</tr>
	</table>
</body>
</html>

四、更新用戶

1. 更新用戶之前的查詢,並將數據在頁面中回顯

1.1 在mapper接口以及映射配置文件中添加相關代碼

	Users selectUsersById(Integer id);
	<select id="selectUsersById" resultType="users">
		select id,name,age from users where id = #{value}
	</select>

1.2 修改業務層代碼

	@Override
	public Users findUserById(Integer id) {
		return this.usersMapper.selectUsersById(id);
	}

1.3 修改Controller

	@RequestMapping("/findUserById")
	public String findUserById(Integer id,Model model){
		Users user = this.usersService.findUserById(id);
		model.addAttribute("user", user);
		return "updateUser";
	}

1.4 添加頁面updateUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form th:action="@{/users/editUser}" method="post">
			<input type="hidden" name="id" th:field="${user.id}"/>
			用戶姓名:<input type="text" name="name" th:field="${user.name}"/><br/>
			用戶年齡:<input type="text" name="age" th:field="${user.age}"/><br/>
			<input type="submit" value="確定"/><br/>
	</form>
</body>
</html>

1.5 修改showUsers.html頁面,添加操作功能

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用戶數據</title>
</head>
<body>
	<table border="1" style="width:300px;">
		<tr>
			<th>用戶ID</th>
			<th>用戶姓名</th>
			<th>用戶年齡</th>
			<th>操作</th>
		</tr>
		<tr th:each="user : ${list}">
			<td th:text="${user.id}"></td>
			<td th:text="${user.name}"></td>
			<td th:text="${user.age}"></td>
			<td>
				<a th:href="@{/users/findUserById(id=${user.id})}">更新用戶</a>
			</td>
		</tr>
	</table>
</body>
</html>

2. 用戶更新

2.1 在mapper接口以及映射配置文件中添加相關代碼

	void updateUser(Users users);
	<update id="updateUser" parameterType="users">
		update users set name=#{name} ,age=#{age} where id=#{id}
	</update>

2.2 修改業務層代碼

	@Override
	public void updateUser(Users users) {
		this.usersMapper.updateUser(users);
	}

2.3 修改Controller

	@RequestMapping("/editUser")
	public String editUser(Users users){
		this.usersService.updateUser(users);
		return "ok";
	}

五、刪除用戶

1. 在mapper接口以及映射配置文件中添加相關代碼

	void deleteUserById(Integer id);
	<delete id="deleteUserById">
		delete from users where id = #{value}
	</delete>

2. 修改業務層代碼

	@Override
	public void deleteUserById(Integer id) {
		this.usersMapper.deleteUserById(id);
	}

3. 修改Controller

	@RequestMapping("/delUser")
	public String delUser(Integer id){
		this.usersService.deleteUserById(id);
		return "redirect:/users/findUserAll";
	}

4. 修改showUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用戶數據</title>
</head>
<body>
	<table border="1" style="width:300px;">
		<tr>
			<th>用戶ID</th>
			<th>用戶姓名</th>
			<th>用戶年齡</th>
			<th>操作</th>
		</tr>
		<tr th:each="user : ${list}">
			<td th:text="${user.id}"></td>
			<td th:text="${user.name}"></td>
			<td th:text="${user.age}"></td>
			<td>
				<a th:href="@{/users/findUserById(id=${user.id})}">更新用戶</a>
				<a th:href="@{/users/delUser(id=${user.id})}">刪除用戶</a>
			</td>
		</tr>
	</table>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章