SrpingBoot相较于传统的项目具备配置简单,能快速进行开发的特色,花更少的时间在各种配置文件上,更多时间在具体业务逻辑上。java
SPringBoot采用纯注解方式进行配置,不喜欢xml配置的同窗得仔细看了。node
首先须要构建SpringBoot项目,除了传统的本身构建好修改pom中依赖外,spring提供了更便捷的项目建立方式mysql
勾选本身须要用到的东西,这里咱们构建标准的web项目,同时里面使用到了mybatis,mysql,redis为实例进行建立,根据项目须要本身勾选相关项目,生成项目后并导入到Eclipse等开发工具中,git
注意:打包方式有jar和war, 若是要部署在tomcat中,建议选择warweb
导入后的项目已是标准的web项目,直接经过tomcat部署访问或者运行application.java的main方法,启动后访问一切正常。redis
增长数据库访问和mybatis相关操做配置:spring
1.首先在application.properties中增长数据源配置sql
#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路径,还能够设置别名的包路径等数据库
已经数据源,事物等tomcat
之前这些都须要本身在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入门