Spring boot+mybatis+jedis库存服务的开发框架整合与搭建

发布 : 2017-06-20 分类 : 大数据 浏览 :
1
2
3
4
192.168.31.231 matrix-cache01
192.168.31.232 matrix-cache02
192.168.31.233 matrix-cache03
192.168.31.234 matrix-cache04

matrix-cache04上安装mysql

使用yum安装mysql server

1
[root@matrix-cache04 ~]# yum install -y mysql-server

Markdown

1
[root@matrix-cache04 ~]# service mysqld start

Markdown

1
[root@matrix-cache04 ~]# chkconfig mysqld on
1
[root@matrix-cache04 ~]# yum install -y mysql-connector-java

Markdown

1
[root@matrix-cache04 ~]# mysql

Markdown

创建Maven项目

Markdown

Markdown

Markdown

Markdown

项目结构图

Markdown

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<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.matrix.eshop</groupId>
<artifactId>eshop-inventory</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>eshop-inventory</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>

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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>

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

<repositories>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

Application.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.matrix.eshop.inventory;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
* 库存服务的Application入口类
* @author matrix
*
*/
@EnableAutoConfiguration // 注解
@SpringBootApplication // 启动类
@ComponentScan // 开启组件扫描
@MapperScan("com.matrix.eshop.inventory.mapper")
public class Application {

/**
* 构建数据源
* @param args
*/
@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource(){
return new org.apache.tomcat.jdbc.pool.DataSource();
}

/**
* 构建Mybatis入口类
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return sqlSessionFactoryBean.getObject();
}

/**
* 构建事务管理器
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(dataSource());
}

/**
* Spring Boot Application启动入口main方法
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Application.properties

1
2
3
4
spring.datasource.url=jdbc:mysql://192.168.31.234:3306/eshop
spring.datasource.username=eshop
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.matrix.eshop.inventory.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.eshop.inventory.model.User;
import com.matrix.eshop.inventory.service.UserService;

/**
* 用户Controller控制器
*
* @author matrix
*
*/
@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/getUserInfo")
@ResponseBody
public User getUserInfo() {
User user = userService.findUserInfo();
return user;
}

}

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.matrix.eshop.inventory.model;

/**
*
* 测试用户Model类
*
* @author matrix
*
*/
public class User {

/**
* 测试用户姓名
*/
private String name;

/**
* 测试用户年龄
*/
private Integer age;

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.matrix.eshop.inventory.mapper;

import com.matrix.eshop.inventory.model.User;

/**
* 测试用户的Mapper接口
*
* @author matrix
*
*/
public interface UserMapper {

/**
* 查询测试用户的信息
* @return
*/
public User findUserInfo();


}

UserMapper.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.matrix.eshop.inventory.mapper.UserMapper">

<select id="findUserInfo" resultType="com.matrix.eshop.inventory.model.User">
select name,age from user;
</select>

</mapper>

UserService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.matrix.eshop.inventory.service;

import com.matrix.eshop.inventory.model.User;

/**
* 用户service接口
* @author matrix
*
*/
public interface UserService {

/**
* 查询用户信息
* @return
*/
public User findUserInfo();
}

UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.matrix.eshop.inventory.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.matrix.eshop.inventory.mapper.UserMapper;
import com.matrix.eshop.inventory.model.User;
import com.matrix.eshop.inventory.service.UserService;

/**
* 用户Service实现类
* @author matrix
*
*/
@Service("userService")
public class UserServiceImpl implements UserService{

@Resource
private UserMapper userMapper;

@Override
public User findUserInfo() {
return userMapper.findUserInfo();
}
}

在matrix-cache04的Mysql中创建eshop数据库、eshop用户、密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@matrix-cache04 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database if not exists eshop;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on eshop.* to 'eshop'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
1
2
3
4
5
6
7
mysql> use eshop
Database changed

mysql> create table user(name varchar(255), age int);
Query OK, 0 rows affected (0.01 sec)

insert into user values('zhangsan', 25);

Markdown

Markdown

整合Jedis Cluster

项目结构图

Markdown

pom.xml中添加jedis依赖

1
2
3
4
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

在Application.java中添加

1
2
3
4
5
6
7
8
9
@Bean
public JedisCluster JedisClusterFactory() {
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("192.168.31.231", 7003));
jedisClusterNodes.add(new HostAndPort("192.168.31.232", 7004));
jedisClusterNodes.add(new HostAndPort("192.168.31.233", 7006));
JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);
return jedisCluster;
}

Markdown

RedisDao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.matrix.eshop.inventory.dao;

/**
* Redis本身有各种各样的API和功能
*
* 可以做出来很多非常花哨的功能
*
* @author matrix
*
*/
public interface RedisDao {

void set(String key,String value);

String get(String key);
}

RedisDaoImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.matrix.eshop.inventory.dao.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.matrix.eshop.inventory.dao.RedisDao;

import redis.clients.jedis.JedisCluster;

@Repository("redisDao")
public class RedisDaoImpl implements RedisDao {

@Resource
private JedisCluster jedisCluster;

@Override
public void set(String key, String value) {
jedisCluster.set(key, value);
}

@Override
public String get(String key) {
return jedisCluster.get(key);
}

}

UserService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.matrix.eshop.inventory.service;

import com.matrix.eshop.inventory.model.User;

/**
* 用户service接口
*
* @author matrix
*
*/
public interface UserService {

/**
* 查询用户信息
*
* @return
*/
public User findUserInfo();

/**
* 查询Redis中缓存的用户信息
*
* @return
*/
public User getCacheUserInfo();
}

UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.matrix.eshop.inventory.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.matrix.eshop.inventory.dao.RedisDao;
import com.matrix.eshop.inventory.mapper.UserMapper;
import com.matrix.eshop.inventory.model.User;
import com.matrix.eshop.inventory.service.UserService;

/**
* 用户Service实现类
*
* @author matrix
*
*/
@Service("userService")
public class UserServiceImpl implements UserService {

@Resource
private UserMapper userMapper;
@Resource
private RedisDao redisDao;

@Override
public User findUserInfo() {
return userMapper.findUserInfo();
}

@Override
public User getCacheUserInfo() {
redisDao.set("cache_user_lisi", "{\"name\":\"lisi\",\"age\":28}");
String userJson = redisDao.get("cache_user_lisi");
JSONObject userJsonObject = JSONObject.parseObject(userJson);
User user = new User();
user.setName(userJsonObject.getString("name"));
user.setAge(userJsonObject.getInteger("age"));
return user;
}

}

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.matrix.eshop.inventory.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.eshop.inventory.model.User;
import com.matrix.eshop.inventory.service.UserService;

/**
* 用户Controller控制器
*
* @author matrix
*
*/
@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/getUserInfo")
@ResponseBody
public User getUserInfo() {
User user = userService.findUserInfo();
return user;
}

@RequestMapping("/getCachedUserInfo")
@ResponseBody
public User getCachedUserInfo() {
User user = userService.getCacheUserInfo();
return user;
}

}

Markdown

Markdown

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/06/20/Spring boot+mybatis+jedis库存服务的开发框架整合与搭建/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹