- 导入依赖以及配置组件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- springboot父类工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>com.spring.boot.jdbc</groupId>
<artifactId>03-Spring-boot-jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.7</java.version>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web启动器 -->
<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>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
- 配置application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/059topic
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.bjsxt.pojo
- 创建实体对象
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;
}
}
- 编写数据库持久层
接口:
public interface UsersMapper {
void insertUser(Users users);
List<Users> selectUsersAll();
Users selectUsersById(Integer id);
void updateUser(Users users);
void deleteUserById(Integer id);
}
mapper.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.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
<select id="selectUsersAll" resultType="users">
select id,name,age from users
</select>
<select id="selectUsersById" resultType="users">
select id,name,age from users where id = #{value}
</select>
<update id="updateUser" parameterType="users">
update users set name=#{name} ,age=#{age} where id=#{id}
</update>
<delete id="deleteUserById">
delete from users where id = #{value}
</delete>
</mapper>
- 编写业务层
接口:
public interface UsersService {
public void insertUser(Users users);
List<Users> selectUsersAll();
Users selectUsersById(Integer id);
void updateUser(Users users);
void deleteUserById(Integer id);
}
实现类:
/**
* @Author:CarlosXu
* @Date:2019/8/9 0009
* @Description:com.bjsxt.service.impl
*/
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void insertUser(Users users) {
this.usersMapper.insertUser(users);
}
@Override
public List<Users> selectUsersAll() {
return this.usersMapper.selectUsersAll();
}
@Override
public Users selectUsersById(Integer id) {
return this.usersMapper.selectUsersById(id);
}
@Override
public void updateUser(Users users) {
this.usersMapper.updateUser(users);
}
@Override
public void deleteUserById(Integer id) {
this.usersMapper.deleteUserById(id);
}
}
- 编写控制层
/**
* @Author:CarlosXu
* @Date:2019/8/9 0009
* @Description:com.bjsxt.controller
*/
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/{page}")
public String pageIndex(@PathVariable String page) {
return page;
}
@RequestMapping("/insertUsers")
public String insertUsers(Users users) {
this.usersService.insertUser(users);
return "ok";
}
@RequestMapping("/showAllUsers")
public String selectAllUsers(Model model) {
List<Users> list = this.usersService.selectUsersAll();
model.addAttribute("list",list);
return "showUsers";
}
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model) {
Users users = this.usersService.selectUsersById(id);
model.addAttribute("user",users);
return "edit";
}
@RequestMapping("/edit")
public String editUserById(Users users) {
this.usersService.updateUser(users);
return "redirect:/users/showAllUsers";
}
@RequestMapping("/delUser")
public String deleteUserById(Integer id) {
this.usersService.deleteUserById(id);
return "redirect:/users/showAllUsers";
}
}
- 编写视图层
showUsers.xml
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:500px;">
<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>
add.heml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/users/insertUsers}" method="post">
用户名:<input type="text" name="name"/> <br/>
年 龄:<input type="text" name="age"/> <br/>
<input type="submit" value="okok"/>
</form>
</body>
</html>
edit.xml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/users/edit(id=${user.id})}" method="post">
用户名:<input type="text" name="name" th:field="${user.name}"/> <br/>
年 龄:<input type="text" name="age" th:field="${user.age}"/> <br/>
<input type="submit" value="okok"/>
</form>
</body>
</html>
ok.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
success!!!
</body>
</html>
- 编写启动类
@SpringBootApplication
@MapperScan("com.bjsxt.mapper") //@MapperScan 用户扫描MyBatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}