概述
通過使用整合 SpringBoot + SpringMVC + MyBatis ,實現(xiàn)對users表進行CRUD操作
工具
eclipse + mvn + jdk1.7 + spring boot 1.5.10.RELEASE + mysql
步驟
一臣嚣、創(chuàng)建項目
- 修改 pom.xml 文件
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<groupId>cn.alinwork</groupId>
<artifactId>10_springboot_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- 1.5.10.RELEASE 版本 spring boot 使用 jdk1.7 -->
<java.version>1.7</java.version>
<!-- thymeleaf 版本 -->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!--
web 啟動器 温艇,thymeleaf 是 spring boot 推薦的視圖層技術七问;
spring-boot-starter-thymeleaf 下面已經包括了 spring-boot-starter-web,
所以可以把 spring-boot-starter-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 數(shù)據(jù)庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
</project>
- demo 使用 jdk1.7 + spring boot 1.5.10.RELEASE 版本;
- thymeleaf 是 spring boot 推薦的視圖層技術;
- druid 是阿里巴巴開發(fā)的號稱為監(jiān)控而生的數(shù)據(jù)庫連接池沟娱;
- 創(chuàng)建數(shù)據(jù)庫表
CREATE TABLE `users` (
`id` smallint NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` smallint DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 在 application.properties 文件配置連接池
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=free
spring.datasource.password=1234
#連接池類型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#mybatis 掃描路徑
mybatis.type-aliases-package=cn.alinwork.pojo
application.properties 文件 :
??????Spring Boot 使用“習慣優(yōu)于配置”(項目中存在大量的配置,此外還內置了一個習慣性的配置腕柜,讓你無需手動進行配置)的理念讓你的項目快速運行起來济似。所以矫废,我們要想把 Spring Boot 玩的溜,就要懂得如何開啟各個功能模塊的默認配置砰蠢,這就需要了解 Spring Boot 的配置文件 application.properties蓖扑。
??????Spring Boot 使用了一個全局的配置文件 application.properties,放在 src/main/resources 目錄下或者類路徑的/config下台舱。Sping Boot 的全局配置文件的作用是對一些默認配置的配置值進行修改赵誓。
二、代碼編輯
- 創(chuàng)建實體類
package cn.alinwork.pojo;
public class User {
private int id;
private String name;
private int age;
public User() {}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name: " + name + " ,age: " + age;
}
}
- 創(chuàng)建 Mapper 映射器
package cn.alinwork.mapper;
import java.util.List;
import cn.alinwork.pojo.User;
public interface UserMapper {
//增加用戶
int insertUser(User user);
//查詢所有用戶信息
List<User> selectUserAll();
//根據(jù)id查詢用戶信息
User findUserById(Integer id);
//更新用戶信息
int updateUser(User user);
//刪除用戶
int deleteUser(Integer id);
}
- 編寫 sql 文件
<?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="cn.alinwork.mapper.UserMapper">
<!-- 增加用戶 -->
<insert id="insertUser" parameterType="user">
insert into user(name,age) values(#{name},#{age})
</insert>
<!-- 查詢所有用戶信息 -->
<select id="selectUserAll" resultType="user">
select id, name , age from user
</select>
<!-- 根據(jù)id查詢用戶信息 -->
<select id="findUserById" parameterType="java.lang.Integer" resultType="user">
select id,name,age from user where id = #{id}
</select>
<!-- 更新用戶信息 -->
<update id="updateUser" parameterType="user">
update user set name = #{name} , age = #{age} where id = #{id}
</update>
<!-- 刪除用戶 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
</mapper>
- namespace 指定對應Mapper 映射器類
- parameterType 為 cn.alinwork.pojo.User 類時柿赊,可以用 user 替代俩功;
因為在 application.properties 文件里作了配置
mybatis.type-aliases-package=cn.alinwork.pojo
- 創(chuàng)建 service 類
- 定義接口類
package cn.alinwork.service;
import java.util.List;
import cn.alinwork.pojo.User;
public interface UserService {
int addUser(User user);
List<User> findAllUser();
User findUserById(Integer id);
int updateUser(User user);
int deleteUser(Integer id);
}
- 定義實現(xiàn)類
package cn.alinwork.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.alinwork.mapper.UserMapper;
import cn.alinwork.pojo.User;
import cn.alinwork.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return this.userMapper.insertUser(user);
}
@Override
public List<User> findAllUser() {
return this.userMapper.selectUserAll();
}
@Override
public User findUserById(Integer id) {
return this.userMapper.findUserById(id);
}
@Override
public int updateUser(User user) {
return this.userMapper.updateUser(user);
}
@Override
public int deleteUser(Integer id) {
return this.userMapper.deleteUser(id);
}
}
- 創(chuàng)建 controller 類
package cn.alinwork.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.alinwork.pojo.User;
import cn.alinwork.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/")
private String findUserAll(Model model){
List<User> users = this.userService.findAllUser();
model.addAttribute("users", users);
return "queryAll";
}
@RequestMapping("/gotoAddUser")
private String gotoAddUser(){
return "addUser";
}
@RequestMapping("/addUser")
private String addUser(User user){
this.userService.addUser(user);
return "ok";
}
@RequestMapping("/findUserById")
private String findUserById(int id , Model model){
User user = this.userService.findUserById(id);
model.addAttribute("user", user);
return "update";
}
@RequestMapping("/updateUser")
private String updateUser(User user){
this.userService.updateUser(user);
return "ok";
}
@RequestMapping("/deleteUser")
private String deleteUser(int id){
this.userService.deleteUser(id);
return "ok";
}
}
- 視圖文件
- queryAll.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>展示用戶數(shù)據(jù)</title>
</head>
<body>
<div>
<table border="1px">
<tr><th>用戶ID</th><th>用戶姓名</th><th>用戶年齡</th><th>操作</th></tr>
<tr th:each="user:${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/user/findUserById(id=${user.id})}">修改</a>
<a th:href="@{/user/deleteUser(id=${user.id})}">刪除</a>
</td>
</tr>
</table>
<h1><a th:href="@{/user/gotoAddUser}">添加用戶</a></h1>
</div>
</body>
</html>
- addUser.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>用戶新增</title>
</head>
<body>
<form th:action="@{/user/addUser}">
用戶姓名:<input type="text" name="name" /><br>
用戶年齡:<input type="text" name="age" /><br>
<input type="submit" value="確定" />
</form>
</body>
</html>
- update.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>修改用戶</title>
</head>
<body>
<form th:action="@{/user/updateUser}">
<input type="hidden" 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="確定" />
</form>
</body>
</html>
- ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>添加成功頁</title>
</head>
<body>
<h1>ok !!!</h1>
<h1><a th:href="@{/user/}">去首頁</a></h1>
</body>
</html>
- 編寫啟動類
package cn.alinwork;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBoot 啟動類
*/
@SpringBootApplication
@MapperScan("cn.alinwork.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
三、測試
在瀏覽器輸入 http://localhost:8080/user/ 碰声,出現(xiàn)頁面:
ok诡蜓,至此已成功完畢......