Spring Boot集成Mybatis 入门第二篇

对于多表多条件的分页查询,spring boot稍微有点欠缺,于是楼主研究一下把mybatis集成进来,这个过程并不复杂。
接上篇,
首先,把druid连接池Jar包的引用添加到pom.xml文件里


    com.alibaba
    druid
    1.1.0

如果你在上篇的搭建中有把mybatis jar包引用路径屏蔽掉的话,在这里也要放开。
展示一下最终的pom.xml文件



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.alibaba
            druid
            1.1.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




jar包引入完后,我们来把配置文件修改一下,把mybatis集成进来到application.yml配置文件里来

#默认使用配置
spring:
  profiles:
    active: dev

#公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.example.demo.model
  mapperLocations: classpath:mapper/*.xml


---

#开发配置
spring:
  profiles: dev

  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource

中间加的两行代码即为mybatis的引用,在这里忍不住要说一下,spring boot的配置方式是真的简单,比SSM简单多了。这样在框架上算是集成完了,接下来添加上其他几种需要的文件,来测试一下。
用mybatis的一大好处就是实体类,mapper和XML文件可以自动生成,关于mybatis的自动生成,可以参考一下这篇博文http://blog.csdn.net/zhshulin/article/details/23912615

上面我把我已经生成好的文件贴一下
UserMapper文件

package com.example.demo.mapper;

import com.example.demo.model.User;
import org.springframework.stereotype.Repository;


public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

UserMapper.xml




    
        
        
        
        
    
    
        id, userName, password, age
    
    
    
        delete from User
        where id = #{id,jdbcType=INTEGER}
    
    
        insert into User (id, userName, password,
        age)
        values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
        #{age,jdbcType=INTEGER})
    
    
        insert into User
        
            
                id,
            
            
                userName,
            
            
                password,
            
            
                age,
            
        
        
            
                #{id,jdbcType=INTEGER},
            
            
                #{userName,jdbcType=VARCHAR},
            
            
                #{password,jdbcType=VARCHAR},
            
            
                #{age,jdbcType=INTEGER},
            
        
    
    
        update User
        
            
                userName = #{userName,jdbcType=VARCHAR},
            
            
                password = #{password,jdbcType=VARCHAR},
            
            
                age = #{age,jdbcType=INTEGER},
            
        
        where id = #{id,jdbcType=INTEGER}
    
    
        update User
        set userName = #{userName,jdbcType=VARCHAR},
        password = #{password,jdbcType=VARCHAR},
        age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
    

实体类User

package com.example.demo.model;


public class User {
    private Integer id;

    private String userName;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

启动文件DemoApplication.java修改一下,让其扫描dao层接口。

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

UserController

package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/findUser")
    @ResponseBody
    public User toIndex(HttpServletRequest request, Model model){
        int userId = Integer.parseInt(request.getParameter("id"));
        User user = this.userService.getUserById(userId);
        return user;
    }
}

UserService

package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service("userService")
public class UserService {
    @Resource
    private UserMapper userMapper;


    public User getUserById(int userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public boolean addUser(User record){
        boolean result = false;
        try {
            userMapper.insertSelective(record);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

打开你的浏览器,访问http://localhost:8080/user/findUser?id=1

image.png

这样,就把mybatis集成进来了。

你可能感兴趣的:(Spring Boot集成Mybatis 入门第二篇)