SpringBoot系列之三_一個完整的MVC案例


  這一節讓我們來做一個完整的案例。

  我們將使用MyBatis作為ORM框架,並以非常簡單的方式來使用MyBatis,完成一個完整的MVC案例。

  此案例承接上一節,請先搭建好上一節案例。

 

  一、數據庫准備

  我們使用MySQL作為數據存儲,使用開發工具創建一個數據庫demo,並在demo數據庫中創建一張表user,創建語句如下:

 

  1.  
    CREATE TABLE `user` (
  2.  
    `id` int(11) NOT NULL COMMENT '主鍵',
  3.  
    `name` varchar(64) NOT NULL COMMENT '姓名',
  4.  
    `birthday` date DEFAULT NULL COMMENT '生日',
  5.  
    `address` varchar(256) DEFAULT NULL COMMENT '地址',
  6.  
    PRIMARY KEY (`id`)
  7.  
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  在這個表中寫入一些測試數據,例如:

 

 

  二、編寫項目代碼

  1. 修改pom文件,添加兩個依賴項:

  (1) mysql驅動包

  (2) mybatis支持(MyBatis公司為spring boot編寫了一個mybatis-spring-boot-starter項目)

 

  pom文件內容如下:

 

  1.  
    <dependency>
  2.  
    <groupId>org.mybatis.spring.boot</groupId>
  3.  
    <artifactId>mybatis-spring-boot-starter</artifactId>
  4.  
    <version>1.3.0</version>
  5.  
    </dependency>
  6.  
     
  7.  
    <dependency>
  8.  
    <groupId>mysql</groupId>
  9.  
    <artifactId>mysql-connector-java</artifactId>
  10.  
    </dependency>

  2. 修改application.properties,內容如下:

 

 

  1.  
    spring.datasource.url=jdbc:mysql: //localhost:3306/demo?characterEncoding=utf8
  2.  
    spring.datasource.username=root
  3.  
    spring.datasource.password=root
  4.  
    spring.datasource.driver- class-name=com.mysql.jdbc.Driver
  5.  
     
  6.  
    mybatis.typeAliasesPackage=com.hanhf.demo.domain

  此處配置了數據庫URL、用戶名、密碼、驅動類,另外,還配置了實體類所在的包。

 

 

  3. 具體代碼:

  先來看一下項目的包結構:

  這里,創建了三個包:

  web:存放控制器

  service:存放業務邏輯層對象

  domain:存放實體類和DAO類

 

  (1) User類(很普通的POJO類):

 

  1.  
    public class User {
  2.  
    private int id;
  3.  
    private String name;
  4.  
    private Date birthday;
  5.  
    private String address;
  6.  
     
  7.  
    // 構造方法
  8.  
     
  9.  
    public User() {
  10.  
    super();
  11.  
    }
  12.  
     
  13.  
    public User(int id, String name, Date birthday, String address) {
  14.  
    super();
  15.  
    this.id = id;
  16.  
    this.name = name;
  17.  
    this.birthday = birthday;
  18.  
    this.address = address;
  19.  
    }
  20.  
     
  21.  
    // getter & setter
  22.  
     
  23.  
    public int getId() {
  24.  
    return id;
  25.  
    }
  26.  
     
  27.  
    public void setId(int id) {
  28.  
    this.id = id;
  29.  
    }
  30.  
     
  31.  
    public String getName() {
  32.  
    return name;
  33.  
    }
  34.  
     
  35.  
    public void setName(String name) {
  36.  
    this.name = name;
  37.  
    }
  38.  
     
  39.  
    public Date getBirthday() {
  40.  
    return birthday;
  41.  
    }
  42.  
     
  43.  
    public void setBirthday(Date birthday) {
  44.  
    this.birthday = birthday;
  45.  
    }
  46.  
     
  47.  
    public String getAddress() {
  48.  
    return address;
  49.  
    }
  50.  
     
  51.  
    public void setAddress(String address) {
  52.  
    this.address = address;
  53.  
    }
  54.  
    }

  (2) UserMapper接口:

 

  MyBatis需要你為DAO提供一個接口,它稱之為Mapper。然后具體的SQL語句可以注解在接口上,也可以單獨保存在一個xml文件中,我這里使用的是前者,即將SQL語句注解在接口上。

  代碼如下:

 

  1.  
    @Mapper
  2.  
    public interface UserMapper {
  3.  
    @Select("select * from user")
  4.  
    List<User> selectAll();
  5.  
    }

 

  增、刪、改、查各有一個注解。接口前的@Mapper注解表示它是一個Mapper。


  (3) UserService類

  代碼如下:

 

  1.  
    @Service
  2.  
    public class UserService {
  3.  
    @Autowired
  4.  
    private UserMapper userMapperp;
  5.  
     
  6.  
    public List<User> listAll() {
  7.  
    return userMapperp.selectAll();
  8.  
    }
  9.  
    }
  業務邏輯層的類需要加上@Service注解。

 

 

  (4) UserController類

  代碼如下:

 

  1.  
    @RequestMapping("user")
  2.  
    @RestController
  3.  
    public class UserController {
  4.  
    @Autowired
  5.  
    private UserService userService;
  6.  
     
  7.  
    @GetMapping("/list/all")
  8.  
    public List<User> listAll() {
  9.  
    return userService.listAll();
  10.  
    }
  11.  
    }
  控制器類前可以加@Controller注解,也可以加@RestController注解,如果是前者,則控制器方法返回的是一個頁面的代稱,如果是后者,則控制器方法返回的是JSON數據。

 

  @RequestMapping用於指定請求的URL,它還有幾個子注解:@GetMapping、@PostMapping、@PutMapping、@DeleteMapping,分別表示請求的GET、POST、PUT、DELETE等操作,這是為RESTFUL應用設置的新注解。

  如果類上有@RequestMapper("user"),方法上又有@GetMapping("/list/all"),則要訪問listAll()方法,請求的URL應為:http://localhost:8080/user/list/all。

 

  (5) 主應用程序

  最后,在主應用程序的類前加一個注解:

  @MapperScan("com.hanhf.demo.domain")

  這個注解表示掃描MyBatis的Mapper接口所在的包,找出所有加了@Mapper注解的接口。

  代碼如下:

 

  1.  
    @SpringBootApplication
  2.  
    @MapperScan("com.hanhf.demo.domain")
  3.  
    public class DemoApplication {
  4.  
    public static void main(String[] args) {
  5.  
    SpringApplication.run(DemoApplication.class, args);
  6.  
    }
  7.  
    }

 

  至此,項目完成。

 

  啟動程序,並輸入網址:http://localhost:8080/user/list/all,出現網頁:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM