spring-boot 集成mybatis配置使用

基本照搬了mybatis-spring-boot-samples中的代码,先上手。

添加maven依赖

在pom文件中增加如下内容


      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.1.1

创建表

在数据库中增加一张表用于测试,以下是建表语句

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(128) DEFAULT NULL,
  `money` decimal(10,2) DEFAULT NULL,
  `create_date` datetime NOT NULL,
  `modify_date` datetime NOT NULL,
  `is_deleted` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户';

添加相关配置信息

首先在resources目录下增加mybatis-config.xml




    
        
    
在resources目录下创建mybatis/mapper目录,并在其下创建UserMapper.xml




    
        
        
        
        
        
        
    
    

在application.propertise文件中添加以下配置,指定mybatis-config.xml的位置

mybatis.config-location=classpath:mybatis-config.xml
如果在application.propertise文件中添加以下配置,则mybatis-config.xml无需再配置中的内容

mybatis.mapper-locations=classpath:mybatis/mapper/*Mapper.xml

相关java代码

创建表user对应的实体类UserMo.java

package com.model;

import java.math.BigDecimal;
import java.util.Date;

public class UserMo {

    private Long id;
    private String name;
    private BigDecimal money;
    private Date createDate;
    private Date modifyDate;
    private Integer isDeleted;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public Integer getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }
}

创建对应的UserMapper.java

package com.mapper;

import com.model.UserMo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {

    UserMo selectUserById(@Param("id") Long id);
}
创建UserDao.java

package com.dao;

import com.model.UserMo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;

@Component
public class UserDao {

    private final SqlSession sqlSession;

    public UserDao(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    public UserMo findUserById(Long id) {
        return this.sqlSession.selectOne("selectUserById", id);
    }
}

测试

OK,准备工作都已就绪,开始测试,增加如下内容

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDao userDao;

    @RequestMapping("/find/mybatis/id")
    public String findMailByToFromMybatis(HttpServletRequest request, Long id) {
        UserMo userMo = userMapper.selectUserById(id);
        UserMo userMo1 = userDao.findUserById(id);
        return "userMapper: " + JSON.toJSONString(userMo) + ", userDao: " + JSON.toJSONString(userMo1);
    }
启动项目,在浏览器中输入 http://127.0.0.1:8080/find/mybatis/id?id=1,会看到如下

userMapper: {"createDate":1483586613000,"id":1,"isDeleted":0,"modifyDate":1483586613000,"money":1.20,"name":"sss"} 
userDao: {"createDate":1483586613000,"id":1,"isDeleted":0,"modifyDate":1483586613000,"money":1.20,"name":"sss"}
而且在控制台看到如下输出

INFO 1238 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
[http-nio-8080-exec-1] INFO  c.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
可以看到用的连接池正是之前配置的druid连接池。


其中很多代码入model,mapper等可以通过mybatis的逆向工程直接生成。

另一种方式

使用Mapper注解,不需要对应的mapper.xml文件,创建UserMapper1.java

package com.mapper;

import com.model.UserMo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper1 {

    @Select("select id, name, money, create_date as createDate, modify_date as modifyDate, is_deleted as isDeleted" +
            " from test_user where name = #{name}")
    UserMo selectUserByName(@Param("name") String name);
}
其中部分字段使用了别名,不这样的话无法得到其中的内容。

测试代码如下

    @Autowired
    private UserMapper1 userMapper1;

    @RequestMapping("/find/mybatis/name")
    public String findMailByToFromMybatis(HttpServletRequest request, String name) {
        UserMo userMo = userMapper1.selectUserByName(name);
        return "userMapper1: " + JSON.toJSONString(userMo);
    }
启动服务后,输入 http://127.0.0.1:8080/find/mybatis/name?name=sss,可以看到如下内容

userMapper1: {"createDate":1483586613000,"id":1,"isDeleted":0,"modifyDate":1483586613000,"money":1.20,"name":"sss"}

你可能感兴趣的:(spring-boot)