Skip to content

Commit

Permalink
使用NoSQL数据库-redis
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyws committed Sep 17, 2017
1 parent 40571f9 commit a8d9769
Show file tree
Hide file tree
Showing 35 changed files with 1,279 additions and 0 deletions.
75 changes: 75 additions & 0 deletions spring-boot-demo-17-1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.roncoo.education</groupId>
<artifactId>spring-boot-demo-17-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-17-1</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>

<!-- 数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.roncoo.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo171Application {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemo171Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.roncoo.example.bean;

import java.util.Date;

public class RoncooUser {
private int id;
private String name;
private Date createTime;

public int getId() {
return id;
}

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

public String getName() {
return name;
}

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

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

@Override
public String toString() {
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.roncoo.example.bean;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class RoncooUserLog {

@Id
@GeneratedValue
private Integer id;

@Column
private Date createTime;

@Column
private String userName;

@Column
private String userIp;

public Integer getId() {
return id;
}

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

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public String getUserName() {
return userName;
}

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

public String getUserIp() {
return userIp;
}

public void setUserIp(String userIp) {
this.userIp = userIp;
}

@Override
public String toString() {
return "RoncooUserLog [id=" + id + ", createTime=" + createTime + ", userName=" + userName + ", userIp=" + userIp + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.roncoo.example.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
* @author wujing
*/
@Component
public class RoncooRedisComponent {

@Autowired
private StringRedisTemplate stringRedisTemplate;

public void set(String key, String value) {
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
if (!this.stringRedisTemplate.hasKey(key)) {
ops.set(key, value);
System.out.println("set key success");
} else {
// 存在则打印之前的value值
System.out.println("this key = " + ops.get(key));
}
}

public String get(String key) {
return this.stringRedisTemplate.opsForValue().get(key);
}

public void del(String key) {
this.stringRedisTemplate.delete(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.roncoo.example.controller;

import java.util.HashMap;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ApiController {

@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping(value = "/get", method = RequestMethod.POST)
public HashMap<String, Object> get(@RequestParam String name) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.roncoo.example.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "/file")
public class FileController {

private static final Logger logger = LoggerFactory.getLogger(FileController.class);

@RequestMapping(value = "upload")
@ResponseBody
public String upload(@RequestParam("roncooFile") MultipartFile file) {
if (file.isEmpty()) {
return "文件为空";
}

// 获取文件名
String fileName = file.getOriginalFilename();
logger.info("上传的文件名为:" + fileName);

// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
logger.info("上传的后缀名为:" + suffixName);

// 文件上传路径
String filePath = "d:/roncoo/education/";

// 解决中文问题,liunx下中文路径,图片显示问题
fileName = UUID.randomUUID() + suffixName;

File dest = new File(filePath + fileName);

// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}

try {
file.transferTo(dest);
return "上传成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.roncoo.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/web")
public class WebController {

private static final Logger logger = LoggerFactory.getLogger(WebController.class);

@RequestMapping("index")
public String index(ModelMap map){
logger.info("这里是controller");
map.put("title", "hello world");
return "index"; // 注意,不要在最前面加上/,linux下面会出错
}

@RequestMapping("error")
public String error(ModelMap map){
throw new RuntimeException("测试异常");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.roncoo.example.dao;

import com.roncoo.example.bean.RoncooUser;
import com.roncoo.example.util.base.Page;

public interface RoncooUserDao {

int insert(RoncooUser roncooUser);

int deleteById(int id);

int updateById(RoncooUser roncooUser);

RoncooUser selectById(int id);

Page<RoncooUser> queryForPage(int i, int j, String string);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.roncoo.example.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.roncoo.example.bean.RoncooUserLog;

public interface RoncooUserLogDao extends JpaRepository<RoncooUserLog, Integer>{

@Query(value="select u from RoncooUserLog u where u.userName=?1")
RoncooUserLog findByUserName(String string);

RoncooUserLog findByUserNameAndUserIp(String string, String ip);

Page<RoncooUserLog> findByUserName(String string, Pageable pageable);
}
Loading

0 comments on commit a8d9769

Please sign in to comment.