Spring Boot 入门篇:SpringBoot+SpringMVC+MyBatis 实战

概述

通过使用整合 SpringBoot + SpringMVC + MyBatis ,实现对users表进行CRUD操作

工具

eclipse + mvn + jdk1.7 + spring boot 1.5.10.RELEASE + mysql

步骤

一、创建项目

  1. 修改 pom.xml 文件

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.10.RELEASE
  
  cn.alinwork
  10_springboot_mybatis
  0.0.1-SNAPSHOT
  
  
    
    1.7
    
    3.0.2.RELEASE
    
    2.0.4
  
  
  
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.1.1
    
    
    
        mysql
        mysql-connector-java
    
    
    
        com.alibaba
        druid
        1.0.9
    
  
  

  • demo 使用 jdk1.7 + spring boot 1.5.10.RELEASE 版本;
  • thymeleaf 是 spring boot 推荐的视图层技术;
  • druid 是阿里巴巴开发的号称为监控而生的数据库连接池;
  1. 创建数据库表
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;
  1. 在 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 使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来。所以,我们要想把 Spring Boot 玩的溜,就要懂得如何开启各个功能模块的默认配置,这就需要了解 Spring Boot 的配置文件 application.properties。
      Spring Boot 使用了一个全局的配置文件 application.properties,放在 src/main/resources 目录下或者类路径的/config下。Sping Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。


二、代码编辑

  1. 创建实体类
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;
    }
}

  1. 创建 Mapper 映射器
package cn.alinwork.mapper;

import java.util.List;
import cn.alinwork.pojo.User;

public interface UserMapper {
    //增加用户
    int insertUser(User user);
    //查询所有用户信息
    List selectUserAll();
    //根据id查询用户信息
    User findUserById(Integer id);
    //更新用户信息
    int updateUser(User user);
    //删除用户
    int deleteUser(Integer id);
}

  1. 编写 sql 文件



    
    
        insert into user(name,age) values(#{name},#{age})
    
    
    
    
    
    
    
        update user set name = #{name} , age = #{age} where id = #{id}
    
    
    
        delete from user where id = #{id}
    

  • namespace 指定对应Mapper 映射器类
  • parameterType 为 cn.alinwork.pojo.User 类时,可以用 user 替代;
    因为在 application.properties 文件里作了配置
    mybatis.type-aliases-package=cn.alinwork.pojo
  1. 创建 service 类
  • 定义接口类
package cn.alinwork.service;

import java.util.List;
import cn.alinwork.pojo.User;

public interface UserService {
    int addUser(User user);
    List findAllUser();
    User findUserById(Integer id);
    int updateUser(User user);
    int deleteUser(Integer id);
}

  • 定义实现类
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 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);
    }
}

  1. 创建 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 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";
    }
}
  1. 视图文件
  • queryAll.html




展示用户数据


    
用户ID用户姓名用户年龄操作
修改 删除

添加用户

  • addUser.html




用户新增


    
用户姓名:
用户年龄:
  • update.html




修改用户


    
用户姓名:
用户年龄:
  • ok.html




添加成功页


    

ok !!!

去首页

  1. 编写启动类
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/ ,出现页面:

ok,至此已成功完毕......

你可能感兴趣的:(Spring Boot 入门篇:SpringBoot+SpringMVC+MyBatis 实战)