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 { protected static Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired
private UserMapper userDao; @RequestMapping("/count/{tableName}")
public int dataCount(@PathVariable String tableName) {
return userDao.dataCount(tableName);
} @RequestMapping(value = "/users", method = { RequestMethod.GET })
public List<User> users(UserParamVo param) {
return userDao.getUsers(param);
} @Autowired
private StringRedisTemplate stringRedisTemplate; @Resource(name = "stringRedisTemplate")
ValueOperations<String, String> valueOperationStr; @RequestMapping("/redis/string/set")
public String setKeyAndValue(String key, String value) {
logger.debug("访问set:key={},value={}", key, value);
valueOperationStr.set(key, value);
return "Set Ok";
} @RequestMapping("/redis/string/get")
public String getKey(String key) {
logger.debug("访问get:key={}", key);
return valueOperationStr.get(key);
} @Autowired
RedisTemplate<Object, Object> redisTemplate; @Resource(name = "redisTemplate")
ValueOperations<Object, Object> valOps; @RequestMapping("/redis/obj/set")
public void save(User user) {
valOps.set(user.getId(), user);
} @RequestMapping("/redis/obj/get")
public User getPerson(String id) {
return (User) valOps.get(id);
}
}
至此,代码就编写完了,启动项目后访问测试
1.查询全部用户信息(无参数时)

2.根据参数查询

3.redis设值

4.redis取值

至此,springboot中使用mybatis操作mysql数据库和操作redis全部完成,需要源码的同学可以发邮件到的邮箱,我会尽快发送给你
代码现已托管到: http://git.oschina.net/xiaochangwei/spring-boot ,请需要的同学下载使用
本文仅做简易的学习测试,更多内容敬请期待后续相关文章
下一篇将讲解springCloud入门
SpringBoot 入门教程:集成mybatis,redis的更多相关文章
- SpringBoot入门教程(四)MyBatis generator 注解方式和xml方式
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...
- SpringBoot系列之集成Mybatis教程
SpringBoot系列之集成Mybatis教程 环境准备:IDEA + maven 本博客通过例子的方式,介绍Springboot集成Mybatis的两种方法,一种是通过注解实现,一种是通过xml的 ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- springBoot入门教程(图文+源码+sql)
springBoot入门 1 springBoot 1.1 SpringBoot简介 Spring Boot让我们的Spring应用变的更轻量化.比如:你可以仅仅依靠一个Java类来运行一个Spr ...
- SpringBoot学习之集成mybatis
一.spring boot集成Mybatis gradle配置: //gradle配置: compile("org.springframework.boot:spring-boot-star ...
- SpringBoot入门教程(十五)集成Druid
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...
- springBoot系列教程03:redis的集成及使用
1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html 2.POM中引入redis <de ...
- SpringBoot入门之集成Druid
Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置.主要参考:https://github.com/alibaba/druid/wiki/常见问题 https: ...
- SpringBoot入门教程(八)配置logback日志
Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...
随机推荐
- 约瑟夫环(java面向对象实现)
/* * 作者:zhaop * 功能:约瑟夫环 * */ public class Joseph { public static void main(String[] args) { // TODO ...
- 常见的Js
//根据单独的值切换所有复选框 $("input[type='checkbox']").prop("checked", function( i, val ) { ...
- 配置 nginx location 实时查看 php-fpm 的状态
在Nginx.conf 里配置 location ~ ^/status$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_ ...
- DNS正、反解析查询指令host、dig、nslookup
一.host指令格式:host [-a] FQDN [server] host -l domain [server]选项:-a :代表列出该主机所有的相关信息,包括 IP.TTL 与除错讯息等等-l ...
- WebRequestManager对象的使用
WebRequestManager可以喝WebRequest对象一起使用. WebRequestManager对象的add_completedRequest方法,可以用来处理所有WebRequest对 ...
- [SQLite]SQL语法
SQLite常用SQL语句 创建表格 sql="CREATE TABLE IF NOT EXISTS MusicList (id integer primary key AutoIncrem ...
- VI 摘要
1 行首添加注释 1 :5,10s/^/#/g 2 sed '5,10s/^/#/' 2 替换某一个行 文本为 11111 22222 33333 替换: %s/\(.*\)/http:\/\/ ...
- shell字符串操作之cut---实现字符串截取
shell中(字符串截取) cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期发布) 2 cut一般以什么为依据呢? 也就是说,我怎么告诉cut我想定位到的剪切 ...
- Knockout 系列
由于近期要做的项目要兼容到IE6,原来的angular框架已经不能满足现在的兼容性要求. 解决方案: 1. 放弃框架,改为库 2. UI库采用 Knockout,css 3. css库 采用 boot ...
- 51nod贪心算法教程
51nod确实是一个好oj,题目质量不错,wa了还放数据,学习算法来说挺好的,这次我做了几个水的贪心,虽然水,但是确实都很典型. 教程链接:http://www.51nod.com/tutorial/ ...