springboot-springmvc-mybatis-环境
- 创建项目
- 修改pom文件
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- spring-boot启动器 -->
<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>2.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.1.10</version>
</dependency>
</dependencies>
- 在
src/main/resources
下添加application.properties
全局配置文件
# mysql-message
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=leidada
# datasource-pool
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# aliases
mybatis.type-aliases-package=com.x.pojo
- 数据库表设计
create table users(
id int(10) primary key auto_increment,
username varchar(20) unique,
password varchar(20)
);
insert into users values(default, "张三","zs");
insert into users values(default, "李四","ls");
springboot-springmvc-mybatis-添加用户
- 根据表结构创建实体类
public class Users {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 创建mapper接口以及映射配置文件
public interface UsersMapper {
void insertUser(Users user);
}
<?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.x.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(id,username, password) values(default,#{username},#{password})
</insert>
</mapper>
- 创建业务层
public interface UsersService {
void insertUser(Users user);
}
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void insertUser(Users user) {
this.usersMapper.insertUser(user);
}
}
- 创建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 user) {
this.usersService.insertUser(user);
return "ok";
}
}
- 在
src/main/resources/templates
中创建页面input.html
<body>
<form th:action="@{/user/addUser}" method="post">
姓名:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit" value="确定">
</form>
</body>
ok.html
<body>
操作成功
</body>
- 启动类,注意:
注意 |
---|
@MapperScan("mapper所在包名") |
整合mybatis时,扫描接口,生成代理对象 |
@SpringBootApplication
@MapperScan("com.x.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
springboot-springmvc-mybatis-查询全部
- mapper接口和配置文件
List<Users> selALl();
<select id="selALl" resultType="users">
select id,username,password form users
</select>
- 业务层
List<Users> selAll();
public List<Users> selAll() {
return this.usersMapper.selALl();
}
- controller
/**
* 查询全部用户
*/
@RequestMapping("/selAll")
public String selAll(Model model) {
List<Users> list = this.usersService.selAll();
model.addAttribute("list", list);
return "showUser";
}
- 新建showUser页面
<body>
<table border="1" style="width: 300px;">
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
</tr>
</table>
</body>
springboot-springmvc-mybatis-用户更新-数据回显
- mapper接口和配置文件
/*
* 根据id查找
*/
Users selUsersById(Integer id);
<select id="selUsersById" resultType="users">
select id,username,password from users where id=#{value}
</select>
- 业务层
Users selUsersById(Integer id);
public Users selUsersById(Integer id) {
return this.usersMapper.selUsersById(id);
}
- controller
/**
* 根据用户id查找用户
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model) {
Users user = this.usersService.selUsersById(id);
model.addAttribute("user", user);
return "updateUser";
}
- 新建updateUser页面,进行数据回显
th:field
<body>
<form th:action="@{/users/edituser}" method="post">
<input type="hidden" name="id" th:field="${user.id}">
姓名:<input type="text" name="username" th:field="${user.username}"><br>
密码:<input type="text" name="password" th:field="${user.password}"><br>
<input type="submit" value="确定">
</form>
</body>
- 修改showUser页面
<body>
<table border="1" style="width: 300px;">
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>操作</th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
</td>
</tr>
</table>
</body>
springboot-springmvc-mybatis-用户更新
- mapper接口和配置文件
void updateUser(Users user);
<update id="updateUser" parameterType="users">
update users set username=#{username},password=#{password} where id=#{id}
</update>
- 业务层
void updateUser(Users user);
public void updateUser(Users user) {
this.usersMapper.updateUser(user);
}
- controller
/**
* 修改用户数据
*/
@RequestMapping("/edituser")
public String edituser(Users user) {
this.usersService.updateUser(user);
return "ok";
}
- 返回到ok.html
springboot-springmvc-mybatis-用户删除
- mapper接口和配置文件
void delUser(Integer id);
<delete id="delUser">
delete from users where id=#{value}
</delete>
- 业务层
void delUser(Integer id);
public void delUser(Integer id) {
this.usersMapper.delUser(id);
}
- controller
/**
* 删除用户
*/
@RequestMapping("/delUser")
public String delUser(Integer id) {
this.usersService.delUser(id);
return "redirect:/users/selAll";
}
- 修改showUser.html
<body>
<table border="1" style="width: 300px;">
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>修改</th>
<th>删除</th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
</td>
<td>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>