第十篇:Spring Boot整合mybatis+Mysql 入门试炼02
前言:
1、(SprigBoot整合SpringMVC+Mybatis)
2、以thymeleaf作為視圖層技術(shù)整合
3、springboot版本2.0.5.RELEASE
創(chuàng)建項目
1、添加依賴及啟動器
2、添加application.yml全局配置文件
#服務(wù)器端口 server:port: 8082 #mybatis mapper映射文件和掃描實體類配置文件 位置 mybatis:config-location: classpath:/mybatis/config/mybatis-config.xmlmapper-locations: classpath:/mybatis/mapper/*.xml #mysql數(shù)據(jù)庫驅(qū)動 url username password spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=trueusername: rootpassword: rootmybatis-config.xml:mybatis配置文件
存放此文件位置:src/main/resource/mybatis/config
如果,沒有創(chuàng)建即可!!!
3、數(shù)據(jù)庫表設(shè)計 創(chuàng)建數(shù)據(jù)庫test,新建user表
CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',`age` int(11) DEFAULT NULL COMMENT '年齡',`password` varchar(32) COLLATE utf8_bin DEFAULT NULL COMMENT '密碼',`sex` int(11) DEFAULT NULL COMMENT '性別',`username` varchar(32) COLLATE utf8_bin DEFAULT NULL COMMENT '用戶名',PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
一、添加用戶
需求實現(xiàn)流程:
- 1、用戶在頁面上輸入用戶信息
- 2、點擊確認按鈕,保存用戶數(shù)據(jù)
前端用戶信息輸入頁面->>>確認按鈕(提交action跳轉(zhuǎn)相應(yīng)操作成功/失敗頁面)
- 2.1 新建包
創(chuàng)建以下幾個目錄:
mybatis/mapper:存放mapper接口的映射文件 mybatis/config:存放mybatis的配置文件(掃描實體類)- 2.2 創(chuàng)建實體類User:
為了清楚觀看數(shù)據(jù),已重寫toString方法
@Data public class User implements Serializable {private Integer id;private Integer age;private String password;private Integer sex;private String username;private static final long serialVersionUID = 1L; }2.2 創(chuàng)建mapper接口及mapper映射文件
public interface UserMapper {int insert(User record); }2.2.1 公用部分:
<?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.gblfy.repository.UserMapper" ><resultMap id="BaseResultMap" type="com.gblfy.entity.User" ><id column="id" property="id" jdbcType="INTEGER" /><result column="age" property="age" jdbcType="INTEGER" /><result column="password" property="password" jdbcType="VARCHAR" /><result column="sex" property="sex" jdbcType="INTEGER" /><result column="username" property="username" jdbcType="VARCHAR" /></resultMap><sql id="Base_Column_List" >id, age, password, sex, username</sql> </mapper>2.2.2 添加用戶部分
<insert id="insert" parameterType="com.gblfy.springboot.chapter2.entity.User" >insert into user (id, age, password, sex, username)values (#{id,jdbcType=INTEGER}, #{age,jdbcType=INTEGER}, #{password,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR})</insert>2.3 創(chuàng)建service接口及實現(xiàn)類
public interface UserService {/*** 新增用戶** @param record* @return*/int insert(User record); }實現(xiàn)類
@Service @Transactional//事物 public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper usersMapper;@Overridepublic int insert(User user) {return usersMapper.insert(user);} }2.4 新建controller
方法showPage:用于做頁面跳轉(zhuǎn) 這樣就可以訪問templates目錄下的靜態(tài)Html文件 templates:默認是安全的、外界無訪問權(quán)限 /*** @author gblfy* @ClassNme UsersController* @Description 用戶在頁面上,添加數(shù)據(jù),后臺保存,視圖技術(shù)采用thymeleaf* @Date 2019/2/22 18:35* @version1.0*/ @Controller//頁面跳轉(zhuǎn)不用@ResponseBody 只處理返回數(shù)據(jù)json @RequestMapping("/user") public class UserController {@Autowiredprivate UserService userService;/*** 跳轉(zhuǎn)頁面** @param page* @return*/@RequestMapping("/{page}")public String showPage(@PathVariable String page) {return page;}/*** 添加用戶** @param user* @return*/@RequestMapping("/addUser")public String save(User user) {userService.insert(user);return "ok";} }- 2.5 頁面
鏈接:http://localhost:8082/user/input
效果圖:
點擊頁面中的確認按鈕即可跳轉(zhuǎn)到url 和/user/addUser綁定的對應(yīng)方法上,
觸發(fā)這個方法之后,通過調(diào)用userService接口中的insert方法把user信息對象當做參數(shù)傳到業(yè)務(wù)處理層
在業(yè)務(wù)處理層,通過調(diào)用mapper接口,mapper接口和mapper映射文件,與數(shù)據(jù)庫進行交互
操作成功頁面:
如果點擊確認插入數(shù)據(jù)庫用戶數(shù)據(jù)成功,就會跳轉(zhuǎn)操作成功頁面
在resources/templates目錄下面創(chuàng)建ok.html:
二、查詢所用用戶信息
2.1 mapper映射文件:
<select id="selectAll" resultMap="BaseResultMap" >select<include refid="Base_Column_List"/>from user </select>2.2 mapper接口
public interface UserMapper {/*** 查詢所有用戶信息** @return*/List<User> selectAll(); }2.3 服務(wù)接口
public interface UserService {/*** 查詢所有用戶信息* @return*/List<User> selectAll(); }2.4 服務(wù)實現(xiàn)類
@Service @Transactional//事物 public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper usersMapper; @Override public List<User> selectAll() {return usersMapper.selectAll();} }2.5 controller層
/*** @author gblfy* @ClassNme UsersController* @Description 用戶在頁面上,添加數(shù)據(jù),后臺保存,視圖技術(shù)采用thymeleaf* @Date 2019/2/22 18:35* @version1.0*/ @Controller//頁面跳轉(zhuǎn)不用@ResponseBody 只處理返回數(shù)據(jù)json @RequestMapping("/user") public class UserController {@Autowiredprivate UserService userService;/*** 跳轉(zhuǎn)頁面** @param page* @return*/@RequestMapping("/{page}")public String showPage(@PathVariable String page) {return page;} /*** 查詢?nèi)坑脩?* @param model* @return*/ @RequestMapping("/findUserAll") public String findUserAll(Model model) {List<User> list = userService.selectAll();model.addAttribute("list", list);return "showUser";} }當頁面訪問:localhost:8080/user/findUserAll時,會觸發(fā)findUserAll這個方法,先去調(diào)用userService接口中的selectAll方法與持久層交互完成用戶信息的查詢,將查詢的結(jié)果返回到控制層,通過Model將數(shù)據(jù)傳到頁面,進行顯示。會跳轉(zhuǎn)到showUser.html頁面上,利用thymeleaf視圖技術(shù),將用戶互信息顯示。
三、通過id查詢用戶
3.1 mapper映射文件:
select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from userwhere id = #{id,jdbcType=INTEGER} </select>3.2 mapper接口
User selectByPrimaryKey(Integer id);3.3 服務(wù)接口
/*** 根據(jù)id查詢** @param id* @return*/ User selectByPrimaryKey(Integer id);3.4 服務(wù)實現(xiàn)類
@Override public User selectByPrimaryKey(Integer id) {return usersMapper.selectByPrimaryKey(id); }3.5 控制層
/*** 通過id查詢用戶** @param id* @return*/ @RequestMapping("/findUserById") public String findUserById(Integer id, Model model) {User user = userService.selectByPrimaryKey(id);model.addAttribute("user", user);return "updateUser"; }3.6 前端顯示頁面:showUser.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>操作提示頁面</title> </head> <body><table border="1" style="width: 600px"><tr><th>用戶id</th><th>用戶姓名</th><th>口令</th><th>年齡</th><th>性別</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 th:text="${user.age}"></td><td th:text="${user.sex}"></td><td><a th:href="@{/user/findUserById(id=${user.id})}">更新用戶</a></td><td><a th:href="@{/user/delUser(id=${user.id})}">刪除用戶</a></td></tr></table> </body> </html>
在前端顯示頁面點擊更新用戶(任選一個),就可以吧用戶id傳遞過去
這是一種頁面跳轉(zhuǎn)的方式
也可以直接在url后面修改id,進行查詢,是一樣的效果
四、編輯用戶信息
4.1 mapper映射文件
<update id="updateByPrimaryKey" parameterType="com.gblfy.entity.User" >update userset age = #{age,jdbcType=INTEGER},password = #{password,jdbcType=VARCHAR},sex = #{sex,jdbcType=INTEGER},username = #{username,jdbcType=VARCHAR}where id = #{id,jdbcType=INTEGER} </update>4.2 mapper接口
User selectByPrimaryKey(Integer id);4.3 服務(wù)接口
/*** 更新用戶信息** @param record* @return*/ void updateByPrimaryKey(User record);4.4 服務(wù)實現(xiàn)類
@Override public void updateByPrimaryKey(User record) {usersMapper.updateByPrimaryKey(record); }4.5控制層
/*** 編輯用戶信息** @param user* @return*/ @RequestMapping("/editUser") public String findUserById(User user) {userService.updateByPrimaryKey(user);return "ok"; }在前端頁面點擊更新用戶操作,就會把需要更新的用戶id傳遞過去
點擊確認按鈕,提交
數(shù)據(jù)庫查看:
五、通過id刪除用戶信息
5.1 mapper映射文件:
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from userwhere id = #{id,jdbcType=INTEGER} </delete>5.2 mapper接口
void deleteByPrimaryKey(Integer id);5.3 服務(wù)接口
/*** 根據(jù)id刪除** @param id* @return*/ void deleteByPrimaryKey(Integer id);5.4 服務(wù)實現(xiàn)類
@Override public void deleteByPrimaryKey(Integer id) {usersMapper.deleteByPrimaryKey(id); }5.5 控制層
/*** 刪除用戶** @param id* @return*/ @RequestMapping("/delUser") public String findUserById(Integer id) {userService.deleteByPrimaryKey(id);return "redirect:/user/findUserAll"; }在前端頁面上點擊刪除用戶操作,就會把需要刪除的用戶id傳遞過去,然后,根據(jù)用戶id將和用戶id相對應(yīng)的用戶信息刪除,最后,重定向到刪除頁面,觀察結(jié)果。
本文源碼下載:
github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-10
總結(jié)
以上是生活随笔為你收集整理的第十篇:Spring Boot整合mybatis+Mysql 入门试炼02的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战07_SSM整合ActiveMQ支持
- 下一篇: Jenkins修改管理员密码