@@ -2,110 +2,116 @@ | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | <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"> | 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> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<modules> | |||||
<module>spring-boot-demo-helloworld</module> | |||||
<module>spring-boot-demo-properties</module> | |||||
<module>spring-boot-demo-actuator</module> | |||||
<module>spring-boot-demo-admin-client</module> | |||||
<module>spring-boot-demo-admin-server</module> | |||||
<module>spring-boot-demo-logback</module> | |||||
<module>spring-boot-demo-log-aop</module> | |||||
<module>spring-boot-demo-exception-handler</module> | |||||
<module>spring-boot-demo-template-freemarker</module> | |||||
<module>spring-boot-demo-template-thymeleaf</module> | |||||
<module>spring-boot-demo-template-beetl</module> | |||||
<module>spring-boot-demo-template-enjoy</module> | |||||
<module>spring-boot-demo-orm-jdbctemplate</module> | |||||
</modules> | |||||
<packaging>pom</packaging> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<modules> | |||||
<module>spring-boot-demo-helloworld</module> | |||||
<module>spring-boot-demo-properties</module> | |||||
<module>spring-boot-demo-actuator</module> | |||||
<module>spring-boot-demo-admin-client</module> | |||||
<module>spring-boot-demo-admin-server</module> | |||||
<module>spring-boot-demo-logback</module> | |||||
<module>spring-boot-demo-log-aop</module> | |||||
<module>spring-boot-demo-exception-handler</module> | |||||
<module>spring-boot-demo-template-freemarker</module> | |||||
<module>spring-boot-demo-template-thymeleaf</module> | |||||
<module>spring-boot-demo-template-beetl</module> | |||||
<module>spring-boot-demo-template-enjoy</module> | |||||
<module>spring-boot-demo-orm-jdbctemplate</module> | |||||
</modules> | |||||
<packaging>pom</packaging> | |||||
<name>spring-boot-demo</name> | |||||
<url>http://xkcoding.com</url> | |||||
<name>spring-boot-demo</name> | |||||
<url>http://xkcoding.com</url> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<maven.compiler.source>1.8</maven.compiler.source> | |||||
<maven.compiler.target>1.8</maven.compiler.target> | |||||
<spring.boot.version>2.0.5.RELEASE</spring.boot.version> | |||||
<hutool.version>4.1.17</hutool.version> | |||||
<user.agent.version>1.20</user.agent.version> | |||||
</properties> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<maven.compiler.source>1.8</maven.compiler.source> | |||||
<maven.compiler.target>1.8</maven.compiler.target> | |||||
<spring.boot.version>2.0.5.RELEASE</spring.boot.version> | |||||
<mysql.version>8.0.12</mysql.version> | |||||
<hutool.version>4.1.17</hutool.version> | |||||
<user.agent.version>1.20</user.agent.version> | |||||
</properties> | |||||
<dependencyManagement> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-dependencies</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
<type>pom</type> | |||||
<scope>import</scope> | |||||
</dependency> | |||||
<!-- hutool工具类 --> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
<version>${hutool.version}</version> | |||||
</dependency> | |||||
<!-- 解析 UserAgent 信息 --> | |||||
<dependency> | |||||
<groupId>eu.bitwalker</groupId> | |||||
<artifactId>UserAgentUtils</artifactId> | |||||
<version>${user.agent.version}</version> | |||||
</dependency> | |||||
</dependencies> | |||||
</dependencyManagement> | |||||
<dependencyManagement> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-dependencies</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
<type>pom</type> | |||||
<scope>import</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>mysql</groupId> | |||||
<artifactId>mysql-connector-java</artifactId> | |||||
<version>${mysql.version}</version> | |||||
</dependency> | |||||
<!-- hutool工具类 --> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
<version>${hutool.version}</version> | |||||
</dependency> | |||||
<!-- 解析 UserAgent 信息 --> | |||||
<dependency> | |||||
<groupId>eu.bitwalker</groupId> | |||||
<artifactId>UserAgentUtils</artifactId> | |||||
<version>${user.agent.version}</version> | |||||
</dependency> | |||||
</dependencies> | |||||
</dependencyManagement> | |||||
<build> | |||||
<pluginManagement> | |||||
<plugins> | |||||
<plugin> | |||||
<artifactId>maven-clean-plugin</artifactId> | |||||
<version>3.0.0</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-resources-plugin</artifactId> | |||||
<version>3.0.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-compiler-plugin</artifactId> | |||||
<version>3.7.0</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<version>2.20.1</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-jar-plugin</artifactId> | |||||
<version>3.0.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-install-plugin</artifactId> | |||||
<version>2.5.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-deploy-plugin</artifactId> | |||||
<version>2.8.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
<executions> | |||||
<execution> | |||||
<goals> | |||||
<goal>repackage</goal> | |||||
</goals> | |||||
</execution> | |||||
</executions> | |||||
</plugin> | |||||
</plugins> | |||||
</pluginManagement> | |||||
</build> | |||||
<build> | |||||
<pluginManagement> | |||||
<plugins> | |||||
<plugin> | |||||
<artifactId>maven-clean-plugin</artifactId> | |||||
<version>3.0.0</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-resources-plugin</artifactId> | |||||
<version>3.0.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-compiler-plugin</artifactId> | |||||
<version>3.7.0</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<version>2.20.1</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-jar-plugin</artifactId> | |||||
<version>3.0.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-install-plugin</artifactId> | |||||
<version>2.5.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<artifactId>maven-deploy-plugin</artifactId> | |||||
<version>2.8.2</version> | |||||
</plugin> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
<version>${spring.boot.version}</version> | |||||
<executions> | |||||
<execution> | |||||
<goals> | |||||
<goal>repackage</goal> | |||||
</goals> | |||||
</execution> | |||||
</executions> | |||||
</plugin> | |||||
</plugins> | |||||
</pluginManagement> | |||||
</build> | |||||
</project> | </project> |
@@ -0,0 +1,2 @@ | |||||
# spring-boot-demo-orm-jdbctemplate | |||||
> 本 demo 主要演示了Spring Boot如何使用 JdbcTemplate 操作数据库,并且简易地封装了一个通用的 Dao 层。 |
@@ -1,53 +1,69 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | <?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" | <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"> | 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> | |||||
<artifactId>spring-boot-demo-orm-jdbctemplate</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-orm-jdbctemplate</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
</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-starter-jdbc</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-orm-jdbctemplate</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>spring-boot-demo-orm-jdbctemplate</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-orm-jdbctemplate</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
</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-starter-jdbc</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>mysql</groupId> | |||||
<artifactId>mysql-connector-java</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-orm-jdbctemplate</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> | </project> |
@@ -1,12 +1,27 @@ | |||||
package com.xkcoding.orm.jdbctemplate; | package com.xkcoding.orm.jdbctemplate; | ||||
import cn.hutool.core.util.IdUtil; | |||||
import cn.hutool.crypto.SecureUtil; | |||||
import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
/** | |||||
* <p> | |||||
* 启动类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate | |||||
* @description: 启动类 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 9:50 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@SpringBootApplication | @SpringBootApplication | ||||
public class SpringBootDemoOrmJdbctemplateApplication { | public class SpringBootDemoOrmJdbctemplateApplication { | ||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoOrmJdbctemplateApplication.class, args); | |||||
} | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoOrmJdbctemplateApplication.class, args); | |||||
} | |||||
} | } |
@@ -0,0 +1,24 @@ | |||||
package com.xkcoding.orm.jdbctemplate.annotation; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* <p> | |||||
* 自增列注解 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.annotation | |||||
* @description: 自增列注解 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 11:23 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
@Target({ElementType.FIELD}) | |||||
public @interface Auto { | |||||
} |
@@ -0,0 +1,30 @@ | |||||
package com.xkcoding.orm.jdbctemplate.annotation; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* <p> | |||||
* 列注解 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.annotation | |||||
* @description: 列注解 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 11:23 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
@Target({ElementType.FIELD}) | |||||
public @interface Column { | |||||
/** | |||||
* 列名 | |||||
* | |||||
* @return 列名 | |||||
*/ | |||||
String name(); | |||||
} |
@@ -0,0 +1,24 @@ | |||||
package com.xkcoding.orm.jdbctemplate.annotation; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* <p> | |||||
* 需要忽略的字段 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.annotation | |||||
* @description: 需要忽略的字段 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 1:25 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
@Target({ElementType.FIELD}) | |||||
public @interface Ignore { | |||||
} |
@@ -0,0 +1,30 @@ | |||||
package com.xkcoding.orm.jdbctemplate.annotation; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* <p> | |||||
* 表注解 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.annotation | |||||
* @description: 表注解 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 11:23 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
@Target({ElementType.TYPE}) | |||||
public @interface Table { | |||||
/** | |||||
* 表名 | |||||
* | |||||
* @return 表名 | |||||
*/ | |||||
String name(); | |||||
} |
@@ -0,0 +1,18 @@ | |||||
package com.xkcoding.orm.jdbctemplate.constant; | |||||
/** | |||||
* <p> | |||||
* 常量池 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.constant | |||||
* @description: 常量池 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 10:59 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface Const { | |||||
String SALT_PREFIX = "::SpringBootDemo::"; | |||||
} |
@@ -0,0 +1,40 @@ | |||||
package com.xkcoding.orm.jdbctemplate.controller; | |||||
import cn.hutool.core.lang.Dict; | |||||
import com.xkcoding.orm.jdbctemplate.entity.User; | |||||
import com.xkcoding.orm.jdbctemplate.service.IUserService; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.web.bind.annotation.PostMapping; | |||||
import org.springframework.web.bind.annotation.RequestBody; | |||||
import org.springframework.web.bind.annotation.RestController; | |||||
/** | |||||
* <p> | |||||
* User Controller | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.controller | |||||
* @description: User Controller | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 1:58 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@RestController | |||||
@Slf4j | |||||
public class UserController { | |||||
private final IUserService userService; | |||||
@Autowired | |||||
public UserController(IUserService userService) { | |||||
this.userService = userService; | |||||
} | |||||
@PostMapping("/user") | |||||
public Dict save(@RequestBody User user) { | |||||
Boolean save = userService.save(user); | |||||
return Dict.create().set("code", save ? 200 : 500).set("msg", save ? "成功" : "失败").set("data", save ? user : null); | |||||
} | |||||
} |
@@ -0,0 +1,39 @@ | |||||
package com.xkcoding.orm.jdbctemplate.dao; | |||||
import com.xkcoding.orm.jdbctemplate.dao.base.BaseDao; | |||||
import com.xkcoding.orm.jdbctemplate.entity.User; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.jdbc.core.JdbcTemplate; | |||||
import org.springframework.stereotype.Repository; | |||||
/** | |||||
* <p> | |||||
* User Dao | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.dao | |||||
* @description: User Dao | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 11:15 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Repository | |||||
public class UserDao extends BaseDao<User> { | |||||
@Autowired | |||||
public UserDao(JdbcTemplate jdbcTemplate) { | |||||
super(jdbcTemplate); | |||||
} | |||||
/** | |||||
* 保存用户 | |||||
* | |||||
* @param user 用户对象 | |||||
* @return 操作影响行数 | |||||
*/ | |||||
public Integer insert(User user) { | |||||
return super.insert(user, true); | |||||
} | |||||
} |
@@ -0,0 +1,97 @@ | |||||
package com.xkcoding.orm.jdbctemplate.dao.base; | |||||
import cn.hutool.core.collection.CollUtil; | |||||
import cn.hutool.core.lang.Dict; | |||||
import cn.hutool.core.util.ObjectUtil; | |||||
import cn.hutool.core.util.ReflectUtil; | |||||
import cn.hutool.core.util.StrUtil; | |||||
import cn.hutool.json.JSONUtil; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Auto; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Column; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Ignore; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Table; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.jdbc.core.JdbcTemplate; | |||||
import java.lang.reflect.Field; | |||||
import java.util.List; | |||||
import java.util.stream.Collectors; | |||||
import java.util.stream.Stream; | |||||
/** | |||||
* <p> | |||||
* Dao基类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.dao.base | |||||
* @description: Dao基类 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 11:28 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Slf4j | |||||
public class BaseDao<T> { | |||||
private JdbcTemplate jdbcTemplate; | |||||
public BaseDao(JdbcTemplate jdbcTemplate) { | |||||
this.jdbcTemplate = jdbcTemplate; | |||||
} | |||||
/** | |||||
* 通用插入 | |||||
* | |||||
* @param t 对象 | |||||
* @param ignoreNull 是否忽略 null 值 | |||||
* @return 操作的行数 | |||||
*/ | |||||
public Integer insert(T t, Boolean ignoreNull) { | |||||
Table tableAnnotation = t.getClass().getAnnotation(Table.class); | |||||
String table; | |||||
if (ObjectUtil.isNotNull(tableAnnotation)) { | |||||
table = tableAnnotation.name(); | |||||
} else { | |||||
table = t.getClass().getName().toLowerCase(); | |||||
} | |||||
// 获取所有字段,包含父类中的字段 | |||||
Field[] fields = ReflectUtil.getFields(t.getClass()); | |||||
// 过滤数据库中不存在的字段,以及自增列 | |||||
List<Field> filterField; | |||||
Stream<Field> fieldStream = CollUtil.toList(fields).stream().filter(field -> ObjectUtil.isNull(field.getAnnotation(Ignore.class)) || ObjectUtil.isNull(field.getAnnotation(Auto.class))); | |||||
// 是否过滤字段值为null的字段 | |||||
if (ignoreNull) { | |||||
filterField = fieldStream.filter(field -> ObjectUtil.isNotNull(ReflectUtil.getFieldValue(t, field))).collect(Collectors.toList()); | |||||
} else { | |||||
filterField = fieldStream.collect(Collectors.toList()); | |||||
} | |||||
// 构造列 | |||||
List<String> columnList = CollUtil.newArrayList(); | |||||
for (Field field : filterField) { | |||||
Column columnAnnotation = field.getAnnotation(Column.class); | |||||
String columnName; | |||||
if (ObjectUtil.isNotNull(columnAnnotation)) { | |||||
columnName = columnAnnotation.name(); | |||||
} else { | |||||
columnName = field.getName(); | |||||
} | |||||
columnList.add(StrUtil.format("`{}`", columnName)); | |||||
} | |||||
String columns = StrUtil.join(",", columnList); | |||||
// 构造占位符 | |||||
String params = StrUtil.repeatAndJoin("?", columnList.size(), ","); | |||||
// 构造值 | |||||
Object[] values = filterField.stream().map(field -> ReflectUtil.getFieldValue(t, field)).toArray(); | |||||
String sql = StrUtil.format("INSERT INTO `{table}`({columns}) VALUES ({params})", Dict.create().set("table", table).set("columns", columns).set("params", params)); | |||||
log.debug("【执行SQL】SQL:{}", sql); | |||||
log.debug("【执行SQL】参数:{}", JSONUtil.toJsonStr(values)); | |||||
return jdbcTemplate.update(sql, values); | |||||
} | |||||
} |
@@ -0,0 +1,81 @@ | |||||
package com.xkcoding.orm.jdbctemplate.entity; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Auto; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Column; | |||||
import com.xkcoding.orm.jdbctemplate.annotation.Table; | |||||
import lombok.Data; | |||||
import java.io.Serializable; | |||||
import java.util.Date; | |||||
/** | |||||
* <p> | |||||
* 用户实体类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.entity | |||||
* @description: 用户实体类 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 10:45 AM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Data | |||||
@Table(name = "orm_user") | |||||
public class User implements Serializable { | |||||
/** | |||||
* 主键 | |||||
*/ | |||||
@Auto | |||||
private Long id; | |||||
/** | |||||
* 用户名 | |||||
*/ | |||||
private String name; | |||||
/** | |||||
* 加密后的密码 | |||||
*/ | |||||
private String password; | |||||
/** | |||||
* 加密使用的盐 | |||||
*/ | |||||
private String salt; | |||||
/** | |||||
* 邮箱 | |||||
*/ | |||||
private String email; | |||||
/** | |||||
* 手机号码 | |||||
*/ | |||||
@Column(name = "phone_number") | |||||
private String phoneNumber; | |||||
/** | |||||
* 状态,-1:逻辑删除,0:禁用,1:启用 | |||||
*/ | |||||
private Integer status; | |||||
/** | |||||
* 创建时间 | |||||
*/ | |||||
@Column(name = "create_time") | |||||
private Date createTime; | |||||
/** | |||||
* 上次登录时间 | |||||
*/ | |||||
@Column(name = "last_login_time") | |||||
private Date lastLoginTime; | |||||
/** | |||||
* 上次更新时间 | |||||
*/ | |||||
@Column(name = "last_update_time") | |||||
private Date lastUpdateTime; | |||||
} |
@@ -0,0 +1,26 @@ | |||||
package com.xkcoding.orm.jdbctemplate.service; | |||||
import com.xkcoding.orm.jdbctemplate.entity.User; | |||||
/** | |||||
* <p> | |||||
* User Service | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.service | |||||
* @description: User Service | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 1:51 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
public interface IUserService { | |||||
/** | |||||
* 保存用户 | |||||
* | |||||
* @param user 用户实体 | |||||
* @return 保存成功 {@code true} 保存失败 {@code false} | |||||
*/ | |||||
Boolean save(User user); | |||||
} |
@@ -0,0 +1,49 @@ | |||||
package com.xkcoding.orm.jdbctemplate.service.impl; | |||||
import cn.hutool.core.util.IdUtil; | |||||
import cn.hutool.crypto.SecureUtil; | |||||
import com.xkcoding.orm.jdbctemplate.constant.Const; | |||||
import com.xkcoding.orm.jdbctemplate.dao.UserDao; | |||||
import com.xkcoding.orm.jdbctemplate.entity.User; | |||||
import com.xkcoding.orm.jdbctemplate.service.IUserService; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.stereotype.Service; | |||||
/** | |||||
* <p> | |||||
* User Service Implement | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.orm.jdbctemplate.service.impl | |||||
* @description: User Service Implement | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/15 1:53 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Service | |||||
public class UserServiceImpl implements IUserService { | |||||
private final UserDao userDao; | |||||
@Autowired | |||||
public UserServiceImpl(UserDao userDao) { | |||||
this.userDao = userDao; | |||||
} | |||||
/** | |||||
* 保存用户 | |||||
* | |||||
* @param user 用户实体 | |||||
* @return 保存成功 {@code true} 保存失败 {@code false} | |||||
*/ | |||||
@Override | |||||
public Boolean save(User user) { | |||||
String rawPass = user.getPassword(); | |||||
String salt = IdUtil.simpleUUID(); | |||||
String pass = SecureUtil.md5(rawPass+ Const.SALT_PREFIX+salt); | |||||
user.setPassword(pass); | |||||
user.setSalt(salt); | |||||
return userDao.insert(user) > 0; | |||||
} | |||||
} |
@@ -0,0 +1,29 @@ | |||||
server: | |||||
port: 8080 | |||||
servlet: | |||||
context-path: /demo | |||||
spring: | |||||
datasource: | |||||
url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 | |||||
username: root | |||||
password: root | |||||
driver-class-name: com.mysql.cj.jdbc.Driver | |||||
type: com.zaxxer.hikari.HikariDataSource | |||||
initialization-mode: always | |||||
continue-on-error: true | |||||
schema: | |||||
- "classpath:db/schema.sql" | |||||
data: | |||||
- "classpath:db/data.sql" | |||||
hikari: | |||||
minimum-idle: 5 | |||||
connection-test-query: SELECT 1 FROM DUAL | |||||
maximum-pool-size: 20 | |||||
auto-commit: true | |||||
idle-timeout: 30000 | |||||
pool-name: SpringBootDemoHikariCP | |||||
max-lifetime: 60000 | |||||
connection-timeout: 30000 | |||||
logging: | |||||
level: | |||||
com.xkcoding: debug |
@@ -0,0 +1,2 @@ | |||||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); | |||||
INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); |
@@ -0,0 +1,13 @@ | |||||
DROP TABLE IF EXISTS `orm_user`; | |||||
CREATE TABLE `orm_user` ( | |||||
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', | |||||
`name` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', | |||||
`password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', | |||||
`salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', | |||||
`email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', | |||||
`phone_number` VARCHAR(15) NOT NULL UNIQUE COMMENT '手机号码', | |||||
`status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态,-1:逻辑删除,0:禁用,1:启用', | |||||
`create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', | |||||
`last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', | |||||
`last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' | |||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; |