zoukankan      html  css  js  c++  java
  • 4. 整合MyBatis

    mybatis既有jdbc的灵活,有具有orm工具的方便,是一套很好用的工具,这儿就使用mybatis来作为数据访问工具,具体添加过程如下:

    1. 添加mybatis依赖,并更新项目

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

    2. 在application.properties中添加datasource
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    3. 添加实体类

     1 package com.lvniao.blog.model;
     2 
     3 public class User {
     4 
     5     private Long id;
     6     
     7     private String name;
     8     
     9     private String password;
    10 
    11     public Long getId() {
    12         return id;
    13     }
    14 
    15     public void setId(Long id) {
    16         this.id = id;
    17     }
    18 
    19     public String getName() {
    20         return name;
    21     }
    22 
    23     public void setName(String name) {
    24         this.name = name;
    25     }
    26 
    27     public String getPassword() {
    28         return password;
    29     }
    30 
    31     public void setPassword(String password) {
    32         this.password = password;
    33     }
    34 }
    User.java

    4. 添加Mapper类

    package com.lvniao.blog.mapper;
    
    import java.util.List;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import com.lvniao.blog.model.User;
    
    @Mapper
    public interface UserMapper {
    
        @Select("select id, name, password from users")
        public List<User> getUsers();
    }
    UserMapper.java

    5. 在控制器中添加Mapper变量

     1 package com.lvniao.blog.admin.controller;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.ui.Model;
     6 import org.springframework.web.bind.annotation.RequestMapping;
     7 
     8 import com.lvniao.blog.mapper.UserMapper;
     9 
    10 @Controller
    11 @RequestMapping("/admin")
    12 public class AdminController {
    13     
    14     @Autowired
    15     private UserMapper userMapper;
    16     
    17     @RequestMapping("/")
    18     public String index(Model model) {
    19         model.addAttribute("users", userMapper.getUsers());
    20         return "admin/index";
    21     }
    22 }
    AdminController

    6. 在视图中添加对应的处理

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4 <title>lvniao</title>
     5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     6 </head>
     7 <body>
     8     <table border="1">
     9         <tr><th>id</th><th>name</th><th>password</th></tr>
    10         <tr th:each="user:${users}">
    11             <td th:text="${user.id}"></td>
    12             <td th:text="${user.name}"></td>
    13             <td th:text="${user.password}"></td>
    14         </tr>
    15     </table>
    16 </body>
    17 </html>
    index.html

    如上就完成了spring和mybatis的整合,其中配置SqlSessionFactory的工作都是由mybatis提供了MybatisAutoConfiguration类完成的,而使用时只需在Mapper接口中添加对应的sql即可,极大地提高了开发速度。

  • 相关阅读:
    数学之路-python计算实战(5)-初识numpy以及pypy下执行numpy
    从Set里面取出有序的记录
    freemarker 模板中定义变量
    MySql的事务操作与演示样例
    两个栈实现一个队列的加入、删除功能
    UVA 1156
    《品牌洗脑》:利用人类潜意识帮助营销的一些方法和实例。4星。
    《大转换》,计算会像电力一样变成基础设施,基本是作者10年前写的《IT不再重要》的修订版,3星。
    《造梦者:迪斯尼如何点亮神奇的创意》:各个迪斯尼乐园的建造过程,不含上海和香港的,没意思。2星。
    《新媒体营销圣经:引诱,引诱,引诱,出击!》:美国主要新媒体上的营销策略与实例点评,离中国有点远。2星
  • 原文地址:https://www.cnblogs.com/lvniao/p/9038784.html
Copyright © 2011-2022 走看看