SpringBoot 入门教程:集成mybatis,redis

SrpingBoot 相较于传统的项目具有配置简单,能快速进行开发的特点,花更少的时间在各类配置文件上,更多时间在具体业务逻辑上。

SPringBoot 采用纯注解方式进行配置,不喜欢 xml 配置的同学得仔细看了。

 

首先需要构建 SpringBoot 项目,除了传统的自己构建好修改 pom 中依赖外,spring 提供了更便捷的项目创建方式

勾选自己需要用到的东西,这里我们构建标准的 web 项目,同时里面使用到了 mybatis,mysql,redis 为实例进行创建,根据项目需要自己勾选相关项目,生成项目后并导入到 Eclipse 等开发工具中,

注意:打包方式有 jar 和 war, 如果要部署在 tomcat 中,建议选择 war

 

导入后的项目已经是标准的 web 项目,直接通过 tomcat 部署访问或者运行 application.java 的 main 方法,启动后访问一切正常。

 

增加数据库访问和 mybatis 相关操作配置:

1. 首先在 application.properties 中增加数据源配置

#datasource configuration
spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=10
spring.datasource.max-idle=5
spring.datasource.min-idle=0

2. 修改 Application.java,设置 mapper 接口扫描路径和 mybatis 对应的 xml 路径,还可以设置别名的包路径等

已经数据源,事物等

以前这些都需要自己在 xml 里面配置,现在直接代码操作就好了,极大的减少了

修改后的代码如下: 注意红色框内内容,根据需要酌情修改

 

3. 定义操作数据库的 mapper,这里需要注意的是,mapper 上面不再需要 @Repository 这样的注解标签了

package com.xiaochangwei.mapper;

import java.util.List;

import com.xiaochangwei.entity.User;
import com.xiaochangwei.vo.UserParamVo;

/**

  • @since 2017 年 2 月 7 日 下午 1:58:46

  • @author 肖昌伟 317409898@qq.com

  • @description
    */
    public interface UserMapper {
    public int dataCount(String tableName);

    public List<User> getUsers(UserParamVo param);
    }

4. 定义放在对应目录下的 mapper.xml 文件

 

5. 由于同时使用了 redis,在 application.properties 中也加入 redis 相关配置

#redis configuration
#redis 数据库名称  从 0 到 15, 默认为 db0
spring.redis.database=1
#redis 服务器名称
spring.redis.host=127.0.0.1
#redis 服务器密码
spring.redis.password=
#redis 服务器连接端口号
spring.redis.port=6379
#redis 连接池设置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
spring.redis.timeout=60000

 

6,最后写 Controller 代码,由于仅仅是测试,就没有什么规范可言了,直接调 dao,方式和以前一样,这里没啥变更

package com.xiaochangwei.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xiaochangwei.entity.User;
import com.xiaochangwei.mapper.UserMapper;
import com.xiaochangwei.vo.UserParamVo;

/**

  • @since 2017 年 2 月 7 日 下午 2:06:11
  • @author 肖昌伟 317409898@qq.com
  • @description
    */

@RestController
public class UserController {

</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Logger logger = LoggerFactory.getLogger(UserController.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);

@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> UserMapper userDao;

@RequestMapping(</span>"/count/{tableName}"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> dataCount(@PathVariable String tableName) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userDao.dataCount(tableName);
}

@RequestMapping(value </span>= "/users", method =<span style="color: rgba(0, 0, 0, 1)"> { RequestMethod.GET })
</span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;User&gt;<span style="color: rgba(0, 0, 0, 1)"> users(UserParamVo param) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userDao.getUsers(param);
}

@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisTemplate stringRedisTemplate;

@Resource(name </span>= "stringRedisTemplate"<span style="color: rgba(0, 0, 0, 1)">)
ValueOperations</span>&lt;String, String&gt;<span style="color: rgba(0, 0, 0, 1)"> valueOperationStr;

@RequestMapping(</span>"/redis/string/set"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String setKeyAndValue(String key, String value) {
    logger.debug(</span>"访问set:key={},value={}"<span style="color: rgba(0, 0, 0, 1)">, key, value);
    valueOperationStr.set(key, value);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Set Ok"<span style="color: rgba(0, 0, 0, 1)">;
}

@RequestMapping(</span>"/redis/string/get"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getKey(String key) {
    logger.debug(</span>"访问get:key={}"<span style="color: rgba(0, 0, 0, 1)">, key);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> valueOperationStr.get(key);
}

@Autowired
RedisTemplate</span>&lt;Object, Object&gt;<span style="color: rgba(0, 0, 0, 1)"> redisTemplate;

@Resource(name </span>= "redisTemplate"<span style="color: rgba(0, 0, 0, 1)">)
ValueOperations</span>&lt;Object, Object&gt;<span style="color: rgba(0, 0, 0, 1)"> valOps;

@RequestMapping(</span>"/redis/obj/set"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(User user) {
    valOps.set(user.getId(), user);
}

@RequestMapping(</span>"/redis/obj/get"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User getPerson(String id) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (User) valOps.get(id);
}

}

 

至此,代码就编写完了,启动项目后访问测试

1. 查询全部用户信息(无参数时)

2. 根据参数查询

 

3.redis 设值

4.redis 取值

至此,springboot 中使用 mybatis 操作 mysql 数据库和操作 redis 全部完成,需要源码的同学可以发邮件到的邮箱,我会尽快发送给你

代码现已托管到: http://git.oschina.net/xiaochangwei/spring-boot ,请需要的同学下载使用

本文仅做简易的学习测试,更多内容敬请期待后续相关文章

 

下一篇将讲解 springCloud 入门