版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1782/article/details/79762610
先随便装点东西吧
1.Redis
$ wget http://download.redis.io/releases/redis-4.0.9.tar.gz
$ tar xzf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make
$ cd src
$ make install
安装的时候遇见了几个问题:
1.
在linux系统上对redis源码进行编译时提示提示“make cc Command not found,make: *** [adlist.o] Error 127”。在进行linux系统安装时,尤其是进行linux服务器安装时,系统工程师往往会最小化安装相应的在linux系统。那么,在这样的linux系统上进行源码文件编译安装时,通常都会出现cc: Command not found,这说明系统上没有安装C语言环境,需要安装,在linux系统上的C环境是gcc,因此需要安装gcc。
解决办法:执行命令- yum install gcc
2.
error: jemalloc/jemalloc.h: No such file or directory
说关于分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。但是如果你又没有jemalloc 而只有 libc 当然 make 出错。 所以加这么一个参数。
解决办法 :make MALLOC=libc
3.
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
./redis-server ../redis.conf 之后执行命令./redis-cli发现没反应。直接运行bin/redis-server将以前端模式启动,前端模式启动的缺点是ssh命令窗口关闭则redis-server程序结束,不推荐使用此方法。
解决办法:设置后端启动:修改redis.conf配置文件, daemonize yes 以后端模式启动。
Redis安装好之后就要考虑怎么使用它啦,先用Java配置一下RedisPool,通过它来对Redis进行操作。redisTemplate参考之前的代码,要记得connectionFactory的要设置连接的地址和验证密码(修改redis.conf的配置 reqirepass 设置密码 bind 127.0.0.1注掉 让自己的ip也可以访问)
package com.xpj.redis;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis 工具类
*/
public class RedisUtil {
protected static ReentrantLock lockPool = new ReentrantLock();
protected static ReentrantLock lockJedis = new ReentrantLock();
protected static Logger logger = Logger.getLogger(RedisUtil.class);
/** Redis服务器IP */
private static String ADDR_ARRAY = "Ip1,Ip2";
/** Redis的端口号 */
private static int PORT = 6379;
/** 访问密码 */
private static String AUTH = "passward";
/**
* 可用连接实例的最大数目,默认值为8;
* 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
*/
private static int MAX_ACTIVE = 8;
/** 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 */
private static int MAX_IDLE = 8;
/**
* 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。 如果超过等待时间,则直接抛出JedisConnectionException;
*/
private static int MAX_WAIT = 3000;
/** 超时时间 */
private static int TIMEOUT = 10000;
/** 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; */
private static boolean TEST_ON_BORROW = false;
private static JedisPool jedisPool = null;
/**
* redis过期时间,以秒为单位
*/
public final static int EXRP_HOUR = 60 * 60; // 一小时
public final static int EXRP_DAY = 60 * 60 * 24; // 一天
public final static int EXRP_MONTH = 60 * 60 * 24 * 30; // 一个月
/**
* 初始化Redis连接池
*/
private static void initialPool() {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[0], PORT, TIMEOUT, AUTH);
} catch (Exception e) {
logger.error("First create JedisPool error : " + e);
try {
// 如果第一个IP异常,则访问第二个IP
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[1], PORT, TIMEOUT, AUTH);
} catch (Exception e2) {
logger.error("Second create JedisPool error : " + e2);
}
}
}
/**
* 在多线程环境同步初始化
*/
private static void poolInit() {
lockPool.lock();
try {
if (jedisPool == null) {
initialPool();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lockPool.unlock();
}
}
/**
* 得到Jedis连接
* @return
*/
public static Jedis getJedis() {
lockJedis.lock();
if (jedisPool == null) {
poolInit();
}
Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Get jedis error : " + e);
} finally {
returnResource(jedis);
lockJedis.unlock();
}
return jedis;
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null && jedisPool != null) {
jedisPool.close();
}
}
/**
* 设置 String
*
* @param key
* @param value
*/
public synchronized static void setString(String key, String value) {
try {
value = StringUtils.isEmpty(value) ? "" : value;
getJedis().set(key, value);
} catch (Exception e) {
logger.error("Set key error : " + e);
}
}
/**
* 设置 过期时间
*
* @param key
* @param seconds
* 以秒为单位
* @param value
*/
public synchronized static void setString(String key, int seconds, String value) {
try {
value = StringUtils.isEmpty(value) ? "" : value;
getJedis().setex(key, seconds, value);
} catch (Exception e) {
logger.error("Set keyex error : " + e);
}
}
/**
* 获取String值
*
* @param key
* @return value
*/
public synchronized static String getString(String key) {
if (getJedis() == null || !getJedis().exists(key)) {
return null;
}
return getJedis().get(key);
}
}
上面的代码记得有参考过某个大神的代码来着,侵删。
2.Mysql(红色为命令)
1.yum install mysql-server
Loading mirror speeds from cached hostfile
No package mysql-server available.
Error: Nothing to do
错误:
yum list | grep mysql
来查找yum源中是否有mysql,然后发现找不到,所以要自己去下载mysql安装源。
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm (注意这里好像不是最新的版本 哈哈)
yum install mysql mysql-server mysql-libs mysql-server(途中确认一下就好)
错误:Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
systemctl start mysqld(
启动MySQL服务)
systemctl enable mysqld
systemctl daemon-reload(
设置mysql服务开机自启动)
输入mysql 进入mysql命令行的模式;
set password for 'root'@'localhost'=password('newPassword')(修改密码)
grant all privileges on *.* to 'root'@'%' with grant option;(修改权限可以使其他机器登录:)
flush privileges;
grant all privileges on 'root'@'%' identified by 'xymt1234';(解决问题:
Access denied for user ‘root’@’localhost’ (using password:YES))
flush privileges;
至此终于可以在本地连接上Mysql数据库啦,查看了一下可以设置数据类型为json和enum,所以大概还是可用的版本。下一步就是用mybatisgenegrator来进行和数据的同步工作啦。
3.Mybatis
1.
如果在工程中使用了maven构建工具,那么就会出现一个问题:我们知道在典型的maven工程中,目录结构有:src/main/java和src/main/resources,前者是用来存放java源代码的,后者则是存放一些资源文件,比如配置文件等,在默认的情况下maven打包的时候,对于src/main/java目录只打包源代码,而不会打包其他文件。所以此时如果把对应的mapper文件放到src/main/java目录下时,不会打包到最终的jar文件夹中,也不会输出到target文件夹中,由于在进行单元测试的时候执行的是/target目录下/test-classes下的代码,所以在测试的时候也不会成功。
为了实现在maven默认环境下打包时,Mybatis的接口和mapper文件在同一包中,可以通过将接口文件放在src/main/java某个包中,而在src/main/resources目录中建立同样的包,这是一种约定优于配置的方式,这样在maven打包的时候就会将src/main/java和src/main/resources相同包下的文件合并到同一包中。
在默认maven打包的环境下,不要将接口文件和mapper文件全部放到src/main/java,这样也不会把mapper文件打包进去
如果不想将接口和mapper文件分别放到
src/main/java和
src/main/resources中,而是全部放到
src/main/java,那么在构建的时候需要指定maven打包需要包括xml文件,具体配置如下:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
检讨一下自己犯得错误:
1.自动生成的全部放在了src/main/java的包里面,但没有修改打包配置
2.interface和mapper.xml分别放在了不同名字的包里面,导致产生了nvalid bound statement (not found): com.xpj.dao.mapper.UserMapper.selectBy的错误
用自动化插件之后能顺利的封装增删查过的操作啦,而且顺利的测试了前面的redisUtil和cacheable的应用,服务器里面redis里面也存储了相应的数据,注意@cacheable一定要有对应的名称,不然会报No cache could be resolved for 'Builder'的错误。下面展示一下生成自动的代码,总感觉和之前比先进了一点。就是自动生成的,所以没配置localdatetime.
import java.io.Serializable;
import java.util.Date;
import com.xpj.config.SpringContextHolder;
import com.xpj.mapper.UserMapper;
public class User implements Serializable {
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.id
*
* @mbg.generated
*/
private Integer id;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.sokeNo
*
* @mbg.generated
*/
private Integer sokeno;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.intro
*
* @mbg.generated
*/
private String intro;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.hobby
*
* @mbg.generated
*/
private String hobby;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.label
*
* @mbg.generated
*/
private String label;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.crt_time
*
* @mbg.generated
*/
private Date crtTime;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to
* the database column usert.upt_time
*
* @mbg.generated
*/
private Date uptTime;
/**
* This field was generated by MyBatis Generator. This field corresponds to
* the database table usert
*
* @mbg.generated
*/
private static final long serialVersionUID = 1L;
/**
* This method was generated by MyBatis Generator. This method corresponds
* to the database table usert
*
* @mbg.generated
*/
public User(Integer id, Integer sokeno, String intro, String hobby, String label, Date crtTime, Date uptTime) {
this.id = id;
this.sokeno = sokeno;
this.intro = intro;
this.hobby = hobby;
this.label = label;
this.crtTime = crtTime;
this.uptTime = uptTime;
}
/**
* This method was generated by MyBatis Generator. This method corresponds
* to the database table usert
*
* @mbg.generated
*/
public User() {
super();
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.id
*
* @return the value of usert.id
*
* @mbg.generated
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.id
*
* @param id
* the value for usert.id
*
* @mbg.generated
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.sokeNo
*
* @return the value of usert.sokeNo
*
* @mbg.generated
*/
public Integer getSokeno() {
return sokeno;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.sokeNo
*
* @param sokeno
* the value for usert.sokeNo
*
* @mbg.generated
*/
public void setSokeno(Integer sokeno) {
this.sokeno = sokeno;
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.intro
*
* @return the value of usert.intro
*
* @mbg.generated
*/
public String getIntro() {
return intro;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.intro
*
* @param intro
* the value for usert.intro
*
* @mbg.generated
*/
public void setIntro(String intro) {
this.intro = intro == null ? null : intro.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.hobby
*
* @return the value of usert.hobby
*
* @mbg.generated
*/
public String getHobby() {
return hobby;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.hobby
*
* @param hobby
* the value for usert.hobby
*
* @mbg.generated
*/
public void setHobby(String hobby) {
this.hobby = hobby == null ? null : hobby.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.label
*
* @return the value of usert.label
*
* @mbg.generated
*/
public String getLabel() {
return label;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.label
*
* @param label
* the value for usert.label
*
* @mbg.generated
*/
public void setLabel(String label) {
this.label = label == null ? null : label.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.crt_time
*
* @return the value of usert.crt_time
*
* @mbg.generated
*/
public Date getCrtTime() {
return crtTime;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.crt_time
*
* @param crtTime
* the value for usert.crt_time
*
* @mbg.generated
*/
public void setCrtTime(Date crtTime) {
this.crtTime = crtTime;
}
/**
* This method was generated by MyBatis Generator. This method returns the
* value of the database column usert.upt_time
*
* @return the value of usert.upt_time
*
* @mbg.generated
*/
public Date getUptTime() {
return uptTime;
}
/**
* This method was generated by MyBatis Generator. This method sets the
* value of the database column usert.upt_time
*
* @param uptTime
* the value for usert.upt_time
*
* @mbg.generated
*/
public void setUptTime(Date uptTime) {
this.uptTime = uptTime;
}
/**
* This method was generated by MyBatis Generator. This method corresponds
* to the database table usert
*
* @mbg.generated
*/
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
User other = (User) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getSokeno() == null ? other.getSokeno() == null : this.getSokeno().equals(other.getSokeno()))
&& (this.getIntro() == null ? other.getIntro() == null : this.getIntro().equals(other.getIntro()))
&& (this.getHobby() == null ? other.getHobby() == null : this.getHobby().equals(other.getHobby()))
&& (this.getLabel() == null ? other.getLabel() == null : this.getLabel().equals(other.getLabel()))
&& (this.getCrtTime() == null ? other.getCrtTime() == null
: this.getCrtTime().equals(other.getCrtTime()))
&& (this.getUptTime() == null ? other.getUptTime() == null
: this.getUptTime().equals(other.getUptTime()));
}
/**
* This method was generated by MyBatis Generator. This method corresponds
* to the database table usert
*
* @mbg.generated
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getSokeno() == null) ? 0 : getSokeno().hashCode());
result = prime * result + ((getIntro() == null) ? 0 : getIntro().hashCode());
result = prime * result + ((getHobby() == null) ? 0 : getHobby().hashCode());
result = prime * result + ((getLabel() == null) ? 0 : getLabel().hashCode());
result = prime * result + ((getCrtTime() == null) ? 0 : getCrtTime().hashCode());
result = prime * result + ((getUptTime() == null) ? 0 : getUptTime().hashCode());
return result;
}
public static UserMapper mapper() {
return SpringContextHolder.getBean(UserMapper.class);
}
public static User findById(Integer id) {
return mapper().selectByPrimaryKey(id);
}
}