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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值