diff --git a/codes/javadb/javadb-h2/pom.xml b/codes/javadb/javadb-h2/pom.xml index da00858..cac313f 100644 --- a/codes/javadb/javadb-h2/pom.xml +++ b/codes/javadb/javadb-h2/pom.xml @@ -14,7 +14,7 @@ ${java.version} ${java.version} - 4.12 + 4.13.1 @@ -39,7 +39,7 @@ com.h2database h2 - 1.4.197 + 2.0.206 test diff --git a/codes/javadb/javadb-hbase/pom.xml b/codes/javadb/javadb-hbase/pom.xml index cec9525..cec530c 100644 --- a/codes/javadb/javadb-hbase/pom.xml +++ b/codes/javadb/javadb-hbase/pom.xml @@ -15,7 +15,7 @@ ${java.version} 1.3.1 - 4.12 + 4.13.1 0.5.7 diff --git a/codes/javadb/javadb-mysql/pom.xml b/codes/javadb/javadb-mysql/pom.xml index 3b52b19..03292c8 100644 --- a/codes/javadb/javadb-mysql/pom.xml +++ b/codes/javadb/javadb-mysql/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -15,111 +15,32 @@ 1.0.0 jar - - UTF-8 - 1.8 - ${java.version} - ${java.version} - - 4.3.13.RELEASE - 1.2.3 - 4.12 - - org.springframework.boot spring-boot-starter-jdbc - + + org.springframework.boot + spring-boot-starter-test + test + mysql mysql-connector-java - 8.0.21 - org.apache.commons - commons-pool2 - 2.5.0 + org.projectlombok + lombok - - - - - ch.qos.logback - logback-classic - - - - - - org.springframework - spring-context-support - - - org.springframework - spring-test - test - - - - - - junit - junit - - - - - - org.springframework - spring-framework-bom - ${spring.version} - pom - import - - - - - redis.clients - jedis - ${jedis.version} - - - - - - ch.qos.logback - logback-parent - ${logback.version} - pom - import - - - - - - junit - junit - ${junit.version} - test - - - - - - ${project.artifactId} - - - true - src/main/resources - - logback.xml - - - + + + org.springframework.boot + spring-boot-maven-plugin + + diff --git a/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/SpringBootDataJdbcApplication.java b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/SpringBootDataJdbcApplication.java new file mode 100644 index 0000000..7ac12f2 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/SpringBootDataJdbcApplication.java @@ -0,0 +1,45 @@ +package io.github.dunwu.javadb; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.Connection; +import javax.sql.DataSource; + +@Slf4j +@SpringBootApplication +public class SpringBootDataJdbcApplication implements CommandLineRunner { + + private final JdbcTemplate jdbcTemplate; + + public SpringBootDataJdbcApplication(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public static void main(String[] args) { + SpringApplication.run(SpringBootDataJdbcApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + DataSource dataSource = jdbcTemplate.getDataSource(); + + Connection connection; + if (dataSource != null) { + connection = dataSource.getConnection(); + } else { + log.error("连接数据源失败!"); + return; + } + + if (connection != null) { + log.info("数据源 Url: {}", connection.getMetaData().getURL()); + } else { + log.error("连接数据源失败!"); + } + } + +} diff --git a/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/User.java b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/User.java new file mode 100644 index 0000000..c70c5f6 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/User.java @@ -0,0 +1,63 @@ +package io.github.dunwu.javadb; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.util.Objects; + +/** + * 用户实体,对应 user 表 + * + * @author Zhang Peng + * @since 2019-11-18 + */ +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class User { + + private Long id; + + private String name; + + private Integer age; + + private String address; + + private String email; + + public User(String name, Integer age, String address, String email) { + this.name = name; + this.age = age; + this.address = address; + this.email = email; + } + + @Override + public int hashCode() { + return Objects.hash(id, name); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + if (!(o instanceof User)) { + return false; + } + + User user = (User) o; + + if (id != null && id.equals(user.id)) { + return true; + } + + return name.equals(user.name); + } + +} diff --git a/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDao.java b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDao.java new file mode 100644 index 0000000..1bc9d97 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDao.java @@ -0,0 +1,41 @@ +package io.github.dunwu.javadb; + +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.List; + +/** + * user 表 Dao 接口 + * + * @author Zhang Peng + * @since 2019-11-18 + */ +public interface UserDao { + + // DML + // ------------------------------------------------------------------- + void insert(User user); + + void batchInsert(List users); + + void deleteByName(String name); + + void deleteAll(); + + void update(User user); + + Integer count(); + + List list(); + + User queryByName(String name); + + JdbcTemplate getJdbcTemplate(); + + // DDL + // ------------------------------------------------------------------- + void truncate(); + + void recreateTable(); + +} diff --git a/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoExecutor.java b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoExecutor.java new file mode 100644 index 0000000..44ff00f --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoExecutor.java @@ -0,0 +1,36 @@ +package io.github.dunwu.javadb; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.List; +import javax.annotation.PostConstruct; + +/** + * @author Zhang Peng + * @since 2020-10-11 + */ +@Slf4j +@Component +public class UserDaoExecutor { + + private final UserDao userDao; + + public UserDaoExecutor(UserDao userDao) { + this.userDao = userDao; + } + + @PostConstruct + public void method() { + if (userDao != null) { + log.info("Connect to datasource success."); + } else { + log.error("Connect to datasource failed!"); + return; + } + + List list = userDao.list(); + list.forEach(item -> log.info(item.toString())); + } + +} diff --git a/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoImpl.java b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoImpl.java new file mode 100644 index 0000000..f558f9d --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/java/io/github/dunwu/javadb/UserDaoImpl.java @@ -0,0 +1,114 @@ +package io.github.dunwu.javadb; + +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +/** + * user 表 Dao 接口实现类 + * + * @author Zhang Peng + * @since 2019-11-18 + */ +@Repository +public class UserDaoImpl implements UserDao { + + private final JdbcTemplate jdbcTemplate; + + public UserDaoImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public void insert(User user) { + jdbcTemplate.update("INSERT INTO user(name, age, address, email) VALUES(?, ?, ?, ?)", + user.getName(), user.getAge(), user.getAddress(), user.getEmail()); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void batchInsert(List users) { + String sql = "INSERT INTO user(name, age, address, email) VALUES(?, ?, ?, ?)"; + + List params = new ArrayList<>(); + + users.forEach(user -> { + params.add(new Object[] { user.getName(), user.getAge(), user.getAddress(), user.getEmail() }); + }); + jdbcTemplate.batchUpdate(sql, params); + } + + @Override + public void deleteByName(String name) { + jdbcTemplate.update("DELETE FROM user WHERE name = ?", name); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void deleteAll() { + jdbcTemplate.execute("DELETE FROM user"); + } + + @Override + public void update(User user) { + jdbcTemplate.update("UPDATE user SET name=?, age=?, address=?, email=? WHERE id=?", + user.getName(), user.getAge(), user.getAddress(), user.getEmail(), user.getId()); + } + + @Override + public Integer count() { + try { + return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM user", Integer.class); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + @Override + public List list() { + return jdbcTemplate.query("SELECT * FROM user", new BeanPropertyRowMapper<>(User.class)); + } + + @Override + public User queryByName(String name) { + try { + return jdbcTemplate.queryForObject("SELECT * FROM user WHERE name = ?", + new BeanPropertyRowMapper<>(User.class), name); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + @Override + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + @Override + public void truncate() { + jdbcTemplate.execute("TRUNCATE TABLE user"); + } + + @Override + public void recreateTable() { + jdbcTemplate.execute("DROP TABLE IF EXISTS user"); + + String sqlStatement = + "CREATE TABLE user (\n" + + " id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',\n" + + " name VARCHAR(255) NOT NULL DEFAULT '' COMMENT '用户名',\n" + + " age INT(3) NOT NULL DEFAULT 0 COMMENT '年龄',\n" + + " address VARCHAR(255) NOT NULL DEFAULT '' COMMENT '地址',\n" + + " email VARCHAR(255) NOT NULL DEFAULT '' COMMENT '邮件',\n" + + " PRIMARY KEY (id),\n" + + " UNIQUE (name)\n" + + ");"; + jdbcTemplate.execute(sqlStatement); + } + +} diff --git a/codes/javadb/javadb-mysql/src/main/resources/application.properties b/codes/javadb/javadb-mysql/src/main/resources/application.properties new file mode 100644 index 0000000..e88c569 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.datasource.url = jdbc:mysql://localhost:3306/db_tutorial?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 +spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver +spring.datasource.username = root +spring.datasource.password = root +# 强制每次启动使用 sql 初始化数据,本项目仅为了演示方便,真实环境应避免这种模式 +spring.datasource.initialization-mode = ALWAYS +spring.datasource.schema = classpath:sql/schema.sql +spring.datasource.data = classpath:sql/data.sql diff --git a/codes/javadb/javadb-mysql/src/main/resources/banner.txt b/codes/javadb/javadb-mysql/src/main/resources/banner.txt new file mode 100644 index 0000000..449413d --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/resources/banner.txt @@ -0,0 +1,12 @@ +${AnsiColor.BRIGHT_YELLOW}${AnsiStyle.BOLD} + ________ ___ ___ ________ ___ __ ___ ___ +|\ ___ \|\ \|\ \|\ ___ \|\ \ |\ \|\ \|\ \ +\ \ \_|\ \ \ \\\ \ \ \\ \ \ \ \ \ \ \ \ \\\ \ + \ \ \ \\ \ \ \\\ \ \ \\ \ \ \ \ __\ \ \ \ \\\ \ + \ \ \_\\ \ \ \\\ \ \ \\ \ \ \ \|\__\_\ \ \ \\\ \ + \ \_______\ \_______\ \__\\ \__\ \____________\ \_______\ + \|_______|\|_______|\|__| \|__|\|____________|\|_______| +${AnsiColor.CYAN}${AnsiStyle.BOLD} +:: Java :: (v${java.version}) +:: Spring Boot :: (v${spring-boot.version}) +${AnsiStyle.NORMAL} diff --git a/codes/javadb/javadb-mysql/src/main/resources/logback.xml b/codes/javadb/javadb-mysql/src/main/resources/logback.xml new file mode 100644 index 0000000..8ec74a3 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/resources/logback.xml @@ -0,0 +1,15 @@ + + + + + %d{HH:mm:ss.SSS} [%boldYellow(%thread)] [%highlight(%-5level)] %boldGreen(%c{36}.%M) - %boldBlue(%m%n) + + + + + + + + + + diff --git a/codes/javadb/javadb-mysql/src/main/resources/sql/data.sql b/codes/javadb/javadb-mysql/src/main/resources/sql/data.sql new file mode 100644 index 0000000..7a73cb7 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/resources/sql/data.sql @@ -0,0 +1,10 @@ +-- ------------------------------------------------------------------- +-- 运行本项目的初始化 DML 脚本 +-- Mysql 知识点可以参考: +-- https://dunwu.github.io/db-tutorial/#/sql/mysql/README +-- ------------------------------------------------------------------- + +INSERT INTO `user` (`name`, `age`, `address`, `email`) +VALUES ('张三', 18, '北京', 'xxx@163.com'); +INSERT INTO `user` (`name`, `age`, `address`, `email`) +VALUES ('李四', 19, '上海', 'xxx@163.com'); diff --git a/codes/javadb/javadb-mysql/src/main/resources/sql/schema.sql b/codes/javadb/javadb-mysql/src/main/resources/sql/schema.sql new file mode 100644 index 0000000..9d09488 --- /dev/null +++ b/codes/javadb/javadb-mysql/src/main/resources/sql/schema.sql @@ -0,0 +1,18 @@ +-- ------------------------------------------------------------------- +-- 运行本项目的初始化 DDL 脚本 +-- Mysql 知识点可以参考: +-- https://dunwu.github.io/db-tutorial/#/sql/mysql/README +-- ------------------------------------------------------------------- + +-- 创建用户表 +DROP TABLE IF EXISTS `user`; +CREATE TABLE `user` ( + `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '用户名', + `age` INT(3) NOT NULL DEFAULT 0 COMMENT '年龄', + `address` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '地址', + `email` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '邮件', + PRIMARY KEY (`id`), + UNIQUE (`name`) +); + diff --git a/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java b/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java index 35a15ec..03fbcff 100644 --- a/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java +++ b/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/MysqlDemoTest.java @@ -16,66 +16,63 @@ import java.sql.*; */ public class MysqlDemoTest { - private static final String DB_HOST = "localhost"; + private static final String DB_HOST = "localhost"; - private static final String DB_PORT = "3306"; + private static final String DB_PORT = "3306"; - private static final String DB_SCHEMA = "sakila"; + private static final String DB_SCHEMA = "db_tutorial"; - private static final String DB_USER = "root"; + private static final String DB_USER = "root"; - private static final String DB_PASSWORD = "root"; + private static final String DB_PASSWORD = "root"; - private static Logger logger = LoggerFactory.getLogger(MysqlDemoTest.class); + private static Logger logger = LoggerFactory.getLogger(MysqlDemoTest.class); - private static Statement statement; + private static Statement statement; - private static Connection connection; + private static Connection connection; - @BeforeClass - public static void beforeClass() { - try { - final String DB_URL = String.format("jdbc:mysql://%s:%s/%s", DB_HOST, DB_PORT, DB_SCHEMA); - connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); - // connection = - // DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?" + - // "user=root&password=root"); - statement = connection.createStatement(); - } catch (SQLException e) { - e.printStackTrace(); - } - } + @BeforeClass + public static void beforeClass() { + try { + final String DB_URL = String.format("jdbc:mysql://%s:%s/%s", DB_HOST, DB_PORT, DB_SCHEMA); + connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); + statement = connection.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + } - @AfterClass - public static void afterClass() { - try { - if (connection != null) { - connection.close(); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } + @AfterClass + public static void afterClass() { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } - @Test - public void testString() { - final String sql = "select * from actor limit 10"; - try { - ResultSet rs = statement.executeQuery(sql); - // 展开结果集数据库 - while (rs.next()) { - // 通过字段检索 - int id = rs.getInt("actor_id"); - String firstName = rs.getString("first_name"); - String lastName = rs.getString("last_name"); - Date lastUpdate = rs.getDate("last_update"); - // 输出数据 - logger.debug("actor_id: {}, first_name: {}, last_name: {}, last_update: {}", id, firstName, lastName, - lastUpdate.toLocalDate()); - } - } catch (SQLException e) { - e.printStackTrace(); - } - } + @Test + public void testQuery() { + final String sql = "SELECT * FROM `user` LIMIT 10"; + try { + ResultSet rs = statement.executeQuery(sql); + // 展开结果集数据库 + while (rs.next()) { + // 通过字段检索 + int id = rs.getInt("id"); + String name = rs.getString("name"); + int age = rs.getInt("age"); + String address = rs.getString("address"); + String email = rs.getString("email"); + // 输出数据 + logger.info("id: {}, name: {}, age: {}, address: {}, email: {}", id, name, age, address, email); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } } diff --git a/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/SpringBootDataJdbcTest.java b/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/SpringBootDataJdbcTest.java new file mode 100644 index 0000000..9c01dfa --- /dev/null +++ b/codes/javadb/javadb-mysql/src/test/java/io/github/dunwu/javadb/SpringBootDataJdbcTest.java @@ -0,0 +1,87 @@ +package io.github.dunwu.javadb; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +@Rollback +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { SpringBootDataJdbcApplication.class }) +public class SpringBootDataJdbcTest { + + private static final Logger log = LoggerFactory.getLogger(SpringBootDataJdbcTest.class); + + @Autowired + private UserDao userDAO; + + @Before + public void before() { + userDAO.truncate(); + } + + @Test + public void insert() { + userDAO.insert(new User("张三", 18, "北京", "user1@163.com")); + User linda = userDAO.queryByName("张三"); + assertThat(linda).isNotNull(); + } + + @Test + public void batchInsert() { + List users = new ArrayList<>(); + users.add(new User("张三", 18, "北京", "user1@163.com")); + users.add(new User("李四", 19, "上海", "user1@163.com")); + users.add(new User("王五", 18, "南京", "user1@163.com")); + users.add(new User("赵六", 20, "武汉", "user1@163.com")); + + userDAO.batchInsert(users); + int count = userDAO.count(); + assertThat(count).isEqualTo(4); + + List list = userDAO.list(); + assertThat(list).isNotEmpty().hasSize(4); + list.forEach(user -> { + log.info(user.toString()); + }); + } + + @Test + public void delete() { + List users = new ArrayList<>(); + users.add(new User("张三", 18, "北京", "user1@163.com")); + users.add(new User("李四", 19, "上海", "user1@163.com")); + users.add(new User("王五", 18, "南京", "user1@163.com")); + users.add(new User("赵六", 20, "武汉", "user1@163.com")); + userDAO.batchInsert(users); + + userDAO.deleteByName("张三"); + User user = userDAO.queryByName("张三"); + assertThat(user).isNull(); + + userDAO.deleteAll(); + List list = userDAO.list(); + assertThat(list).isEmpty(); + } + + @Test + public void update() { + userDAO.insert(new User("张三", 18, "北京", "user1@163.com")); + User oldUser = userDAO.queryByName("张三"); + oldUser.setName("张三丰"); + userDAO.update(oldUser); + User newUser = userDAO.queryByName("张三丰"); + assertThat(newUser).isNotNull(); + } + +} diff --git a/codes/javadb/javadb-mysql/src/test/resources/logback.xml b/codes/javadb/javadb-mysql/src/test/resources/logback.xml deleted file mode 100644 index d782516..0000000 --- a/codes/javadb/javadb-mysql/src/test/resources/logback.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n - - - - - - - - ${user.dir}/logs/${FILE_NAME}-all.%d{yyyy-MM-dd}.log - 30 - - - - - 30MB - - - - %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n - - - - - - - - - - - - - - - - diff --git a/codes/javadb/javadb-redis/pom.xml b/codes/javadb/javadb-redis/pom.xml index d79db7a..836fdfb 100644 --- a/codes/javadb/javadb-redis/pom.xml +++ b/codes/javadb/javadb-redis/pom.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0"> 4.0.0 io.github.dunwu javadb-redis @@ -14,11 +14,11 @@ ${java.version} ${java.version} - 4.3.13.RELEASE + 5.2.8.RELEASE 1.2.3 2.9.0 3.7.2 - 4.12 + 4.13.1 diff --git a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisDemoTest.java b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisDemoTest.java index 31ffc98..79848d7 100644 --- a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisDemoTest.java +++ b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisDemoTest.java @@ -21,141 +21,141 @@ import java.util.Set; */ public class JedisDemoTest { - private static final String REDIS_HOST = "192.168.28.32"; + private static final String REDIS_HOST = "192.168.28.32"; - private static final int REDIS_PORT = 6379; + private static final int REDIS_PORT = 6379; - private static Jedis jedis = null; + private static Jedis jedis = null; - private static Logger logger = LoggerFactory.getLogger(JedisDemoTest.class); + private static Logger logger = LoggerFactory.getLogger(JedisDemoTest.class); - @BeforeClass - public static void beforeClass() { - // Jedis 有多种构造方法,这里选用最简单的一种情况 - jedis = new Jedis(REDIS_HOST, REDIS_PORT); + @BeforeClass + public static void beforeClass() { + // Jedis 有多种构造方法,这里选用最简单的一种情况 + jedis = new Jedis(REDIS_HOST, REDIS_PORT); - // 触发 ping 命令 - try { - jedis.ping(); - logger.debug("jedis 连接成功。"); - } catch (JedisConnectionException e) { - e.printStackTrace(); - } - } + // 触发 ping 命令 + try { + jedis.ping(); + logger.debug("jedis 连接成功。"); + } catch (JedisConnectionException e) { + e.printStackTrace(); + } + } - @AfterClass - public static void afterClass() { - if (null != jedis) { - jedis.close(); - logger.debug("jedis 关闭连接。"); - } - } + @AfterClass + public static void afterClass() { + if (null != jedis) { + jedis.close(); + logger.debug("jedis 关闭连接。"); + } + } - /** - * 增删改 string 类型 - */ - @Test - public void testString() { - final String key = "word"; - final String value1 = "first"; - final String value2 = "second"; + /** + * 增删改 string 类型 + */ + @Test + public void testString() { + final String key = "word"; + final String value1 = "first"; + final String value2 = "second"; - // 新增 key - jedis.set(key, value1); - Assert.assertEquals(value1, jedis.get(key)); + // 新增 key + jedis.set(key, value1); + Assert.assertEquals(value1, jedis.get(key)); - // 修改 key - jedis.set(key, value2); - Assert.assertEquals(value2, jedis.get(key)); + // 修改 key + jedis.set(key, value2); + Assert.assertEquals(value2, jedis.get(key)); - Assert.assertEquals(true, jedis.exists(key)); + Assert.assertEquals(true, jedis.exists(key)); - // 删除 key - jedis.del(key); - Assert.assertEquals(null, jedis.get(key)); - Assert.assertEquals(false, jedis.exists(key)); - } + // 删除 key + jedis.del(key); + Assert.assertEquals(null, jedis.get(key)); + Assert.assertEquals(false, jedis.exists(key)); + } - /** - * 增删改 byte[] 类型(本质也是 string 类型) - */ - @Test - public void testBytes() { - final byte[] key = "word".getBytes(); - final byte[] value1 = "first".getBytes(); - final byte[] value2 = "second".getBytes(); + /** + * 增删改 byte[] 类型(本质也是 string 类型) + */ + @Test + public void testBytes() { + final byte[] key = "word".getBytes(); + final byte[] value1 = "first".getBytes(); + final byte[] value2 = "second".getBytes(); - // 新增 key - jedis.set(key, value1); - Assert.assertArrayEquals(value1, jedis.get(key)); + // 新增 key + jedis.set(key, value1); + Assert.assertArrayEquals(value1, jedis.get(key)); - // 修改 key - jedis.set(key, value2); - Assert.assertArrayEquals(value2, jedis.get(key)); + // 修改 key + jedis.set(key, value2); + Assert.assertArrayEquals(value2, jedis.get(key)); - // 删除 key - jedis.del(key); - Assert.assertArrayEquals(null, jedis.get(key)); - } + // 删除 key + jedis.del(key); + Assert.assertArrayEquals(null, jedis.get(key)); + } - /** - * 增删改 Hash 类型 - */ - @Test - public void testHash() { - final String key = "zpkey"; - final String field1 = "first"; - final String value1 = "一"; - final String value1_1 = "1"; - final String field2 = "second"; - final String value2 = "二"; + /** + * 增删改 Hash 类型 + */ + @Test + public void testHash() { + final String key = "zpkey"; + final String field1 = "first"; + final String value1 = "一"; + final String value1_1 = "1"; + final String field2 = "second"; + final String value2 = "二"; - // 新增 field - jedis.hset(key, field1, value1); - jedis.hset(key, field2, value2); - Assert.assertEquals(value1, jedis.hget(key, field1)); - Assert.assertEquals(value2, jedis.hget(key, field2)); + // 新增 field + jedis.hset(key, field1, value1); + jedis.hset(key, field2, value2); + Assert.assertEquals(value1, jedis.hget(key, field1)); + Assert.assertEquals(value2, jedis.hget(key, field2)); - // 修改 field - jedis.hset(key, field1, value1_1); - Assert.assertEquals(value1_1, jedis.hget(key, field1)); + // 修改 field + jedis.hset(key, field1, value1_1); + Assert.assertEquals(value1_1, jedis.hget(key, field1)); - jedis.hdel(key, field1, value1_1); - Assert.assertEquals(null, jedis.hget(key, field1)); + jedis.hdel(key, field1, value1_1); + Assert.assertEquals(null, jedis.hget(key, field1)); - Assert.assertEquals(false, jedis.hexists(key, field1)); - Assert.assertEquals(true, jedis.hexists(key, field2)); + Assert.assertEquals(false, jedis.hexists(key, field1)); + Assert.assertEquals(true, jedis.hexists(key, field2)); - Map results = jedis.hgetAll(key); - Assert.assertEquals(1, results.size()); - } + Map results = jedis.hgetAll(key); + Assert.assertEquals(1, results.size()); + } - /** - * set & get 命令 - */ - @Test - public void testList() { - final String key = "colors"; - // 存储数据到列表中 - jedis.lpush(key, "Red"); - jedis.lpush(key, "Yellow"); - jedis.lpush(key, "Blue"); - Assert.assertEquals(3L, jedis.llen(key).longValue()); + /** + * set & get 命令 + */ + @Test + public void testList() { + final String key = "colors"; + // 存储数据到列表中 + jedis.lpush(key, "Red"); + jedis.lpush(key, "Yellow"); + jedis.lpush(key, "Blue"); + Assert.assertEquals(3L, jedis.llen(key).longValue()); - // 获取存储的数据并输出 - List list = jedis.lrange("colors", 0, 2); - for (String aList : list) { - System.out.println("列表项为: " + aList); - } - } + // 获取存储的数据并输出 + List list = jedis.lrange("colors", 0, 2); + for (String aList : list) { + System.out.println("列表项为: " + aList); + } + } - @Test - public void testKeys() { - // 存储数据到列表中 - Set keys = jedis.keys("*"); - for (String key : keys) { - System.out.println(key); - } - } + @Test + public void testKeys() { + // 存储数据到列表中 + Set keys = jedis.keys("*"); + for (String key : keys) { + System.out.println(key); + } + } } diff --git a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisPoolDemoTest.java b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisPoolDemoTest.java index 316359d..a4c2e9b 100644 --- a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisPoolDemoTest.java +++ b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/JedisPoolDemoTest.java @@ -18,55 +18,55 @@ import java.util.Set; /** * @author Zhang Peng */ -@ActiveProfiles("test") +@ActiveProfiles("dev") @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = {"classpath:/applicationContext.xml"}) +@ContextConfiguration(locations = { "classpath:/applicationContext.xml" }) public class JedisPoolDemoTest { - private static Logger logger = LoggerFactory.getLogger(JedisPoolDemoTest.class); + private static Logger logger = LoggerFactory.getLogger(JedisPoolDemoTest.class); - @Autowired - private JedisPool jedisPool; + @Autowired + private JedisPool jedisPool; - @Test - public void testSet() { - Jedis jedis = jedisPool.getResource(); - jedis.set("first", "hello world"); - System.out.println("first:" + jedis.get("first")); - logger.debug("first: {}", jedis.get("first")); - jedis.close(); - } + @Test + public void testSet() { + Jedis jedis = jedisPool.getResource(); + jedis.set("first", "hello world"); + System.out.println("first:" + jedis.get("first")); + logger.debug("first: {}", jedis.get("first")); + jedis.close(); + } - @Test - public void testLpush() { - Jedis jedis = jedisPool.getResource(); + @Test + public void testLpush() { + Jedis jedis = jedisPool.getResource(); - // 存储数据到列表中 - jedis.lpush("colors", "Red"); - jedis.lpush("colors", "Yellow"); - jedis.lpush("colors", "Blue"); - // 获取存储的数据并输出 - List list = jedis.lrange("colors", 0, 2); - for (int i = 0; i < list.size(); i++) { - System.out.println("列表项为: " + list.get(i)); - } + // 存储数据到列表中 + jedis.lpush("colors", "Red"); + jedis.lpush("colors", "Yellow"); + jedis.lpush("colors", "Blue"); + // 获取存储的数据并输出 + List list = jedis.lrange("colors", 0, 2); + for (int i = 0; i < list.size(); i++) { + System.out.println("列表项为: " + list.get(i)); + } - jedis.close(); - } + jedis.close(); + } - @Test - public void testKeys() { - Jedis jedis = jedisPool.getResource(); + @Test + public void testKeys() { + Jedis jedis = jedisPool.getResource(); - // 存储数据到列表中 - Set keys = jedis.keys("*"); - Iterator it = keys.iterator(); - while (it.hasNext()) { - String key = it.next(); - System.out.println(key); - } + // 存储数据到列表中 + Set keys = jedis.keys("*"); + Iterator it = keys.iterator(); + while (it.hasNext()) { + String key = it.next(); + System.out.println(key); + } - jedis.close(); - } + jedis.close(); + } } diff --git a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/RedissonStandaloneTest.java b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/RedissonStandaloneTest.java index 014f030..e6ae668 100644 --- a/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/RedissonStandaloneTest.java +++ b/codes/javadb/javadb-redis/src/test/java/io/github/dunwu/javadb/RedissonStandaloneTest.java @@ -11,14 +11,14 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; */ public class RedissonStandaloneTest { - public static void main(String[] args) { - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:redisson-standalone.xml"); - RedissonClient redisson = (RedissonClient) applicationContext.getBean("standalone"); - // 首先获取redis中的key-value对象,key不存在没关系 - RBucket keyObject = redisson.getBucket("key"); - // 如果key存在,就设置key的值为新值value - // 如果key不存在,就设置key的值为value - keyObject.set("value"); - } + public static void main(String[] args) { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:redisson-standalone.xml"); + RedissonClient redisson = (RedissonClient) applicationContext.getBean("standalone"); + // 首先获取redis中的key-value对象,key不存在没关系 + RBucket keyObject = redisson.getBucket("key"); + // 如果key存在,就设置key的值为新值value + // 如果key不存在,就设置key的值为value + keyObject.set("value"); + } } diff --git a/codes/javadb/javadb-redis/src/test/resources/applicationContext.xml b/codes/javadb/javadb-redis/src/test/resources/applicationContext.xml index 14c081d..ead377d 100644 --- a/codes/javadb/javadb-redis/src/test/resources/applicationContext.xml +++ b/codes/javadb/javadb-redis/src/test/resources/applicationContext.xml @@ -1,12 +1,12 @@ + xmlns="http://www.springframework.org/schema/beans" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" + default-lazy-init="false"> - Spring基础配置 + Spring基础配置 - - + + diff --git a/codes/javadb/javadb-redis/src/test/resources/config.xml b/codes/javadb/javadb-redis/src/test/resources/config.xml index 0a0008c..d8024bd 100644 --- a/codes/javadb/javadb-redis/src/test/resources/config.xml +++ b/codes/javadb/javadb-redis/src/test/resources/config.xml @@ -1,20 +1,20 @@ - - - + - + - - - + - + diff --git a/codes/javadb/javadb-redis/src/test/resources/logback.xml b/codes/javadb/javadb-redis/src/test/resources/logback.xml index 54865e8..2e63037 100644 --- a/codes/javadb/javadb-redis/src/test/resources/logback.xml +++ b/codes/javadb/javadb-redis/src/test/resources/logback.xml @@ -3,43 +3,43 @@ - + - - - - %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n - - + + + + %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n + + - - - - - ${user.dir}/logs/${FILE_NAME}.%d{yyyy-MM-dd}.log - 30 - + + + + + ${user.dir}/logs/${FILE_NAME}.%d{yyyy-MM-dd}.log + 30 + - - - 30MB - + + + 30MB + - - %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n - - - + + %d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n + + + - - - - - + + + + + - - - - + + + + diff --git a/codes/javadb/javadb-redis/src/test/resources/redis.xml b/codes/javadb/javadb-redis/src/test/resources/redis.xml index 468b1cd..443f4e2 100644 --- a/codes/javadb/javadb-redis/src/test/resources/redis.xml +++ b/codes/javadb/javadb-redis/src/test/resources/redis.xml @@ -1,21 +1,21 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - redis configuration + redis configuration - - - - - - - + + + + + + + - - - - - - + + + + + + diff --git a/codes/javadb/javadb-redis/src/test/resources/redisson-standalone.xml b/codes/javadb/javadb-redis/src/test/resources/redisson-standalone.xml index 04a334f..dac42b3 100644 --- a/codes/javadb/javadb-redis/src/test/resources/redisson-standalone.xml +++ b/codes/javadb/javadb-redis/src/test/resources/redisson-standalone.xml @@ -1,21 +1,21 @@ - - - - + + + + diff --git a/codes/middleware/flyway/pom.xml b/codes/middleware/flyway/pom.xml index 39129fd..b5c40c8 100644 --- a/codes/middleware/flyway/pom.xml +++ b/codes/middleware/flyway/pom.xml @@ -26,7 +26,7 @@ com.h2database h2 - 1.4.197 + 2.0.206 @@ -45,7 +45,7 @@ com.h2database h2 - 1.4.197 + 2.0.206 diff --git a/codes/redis/redis-in-action/pom.xml b/codes/redis/redis-in-action/pom.xml index 5a19dba..3c2c2ed 100644 --- a/codes/redis/redis-in-action/pom.xml +++ b/codes/redis/redis-in-action/pom.xml @@ -16,7 +16,7 @@ 1.2.3 2.9.0 - 4.12 + 4.13.1