├── .gitattributes
├── mybatis-generator
├── mybatis-generator.iml
├── .idea
│ ├── misc.xml
│ └── compiler.xml
├── pom.xml
└── src
│ └── main
│ └── resources
│ ├── generator.xml
│ └── generator-cache.xml
├── mybatis-mapping
├── mybatis-mapping.iml
├── target
│ └── classes
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── mybatis-config.xml
│ │ ├── sql
│ │ └── mybatis-mapping.sql
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ ├── AuthorMapper.xml
│ │ ├── CommentMapper.xml
│ │ └── PostMapper.xml
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── jdbc.properties
│ │ │ ├── log4j.properties
│ │ │ ├── mybatis-config.xml
│ │ │ └── sql
│ │ │ │ └── mybatis-mapping.sql
│ │ └── java
│ │ │ └── com
│ │ │ └── homejim
│ │ │ └── mybatis
│ │ │ ├── entity
│ │ │ ├── BlogCustom.java
│ │ │ ├── BlogPostBO.java
│ │ │ ├── BlogBO.java
│ │ │ ├── Blog.java
│ │ │ ├── Comment.java
│ │ │ ├── Post.java
│ │ │ └── Author.java
│ │ │ └── mapper
│ │ │ ├── AuthorMapper.java
│ │ │ ├── CommentMapper.java
│ │ │ ├── PostMapper.java
│ │ │ ├── BlogMapper.java
│ │ │ ├── AuthorMapper.xml
│ │ │ ├── CommentMapper.xml
│ │ │ └── PostMapper.xml
│ └── test
│ │ └── java
│ │ └── com
│ │ └── mybatis
│ │ └── homejim
│ │ └── mapper
│ │ ├── PostMapperTest.java
│ │ └── BlogMapperTest.java
├── .idea
│ ├── misc.xml
│ └── compiler.xml
└── pom.xml
├── mybatisGenericTokenParser
├── mybatisGenericTokenParser.iml
├── src
│ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── homejim
│ │ │ └── myabtis
│ │ │ ├── TokenHandler.java
│ │ │ └── GenericTokenParser.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── GenericTokenParserTest.java
├── .idea
│ ├── misc.xml
│ └── compiler.xml
└── pom.xml
├── mybatis-dynamic
├── target
│ └── classes
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── mybatis-config.xml
│ │ ├── sql.sql
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── StudentMapper.xml
├── src
│ └── main
│ │ ├── resources
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── mybatis-config.xml
│ │ └── sql.sql
│ │ └── java
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ ├── mapper
│ │ ├── StudentMapper.java
│ │ └── StudentMapper.xml
│ │ └── entity
│ │ └── Student.java
├── .idea
│ ├── encodings.xml
│ ├── misc.xml
│ └── compiler.xml
└── pom.xml
├── .idea
├── vcs.xml
└── compiler.xml
├── helloMybatis
├── .idea
│ ├── encodings.xml
│ ├── misc.xml
│ └── compiler.xml
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── jdbc.properties
│ │ │ ├── log4j.properties
│ │ │ └── mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── homejim
│ │ │ └── mybatis
│ │ │ ├── mapper
│ │ │ ├── StudentMapper.java
│ │ │ └── StudentMapper.xml
│ │ │ └── entity
│ │ │ └── Student.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── StudentMapperTest.java
└── pom.xml
├── mybatis-cache
├── target
│ └── classes
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── mybatis-config.xml
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── StudentMapper.xml
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── jdbc.properties
│ │ │ ├── log4j.properties
│ │ │ └── mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── homejim
│ │ │ └── mybatis
│ │ │ ├── mapper
│ │ │ ├── StudentMapper.java
│ │ │ └── StudentMapper.xml
│ │ │ └── entity
│ │ │ └── Student.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── MybatisCacheTest.java
├── .idea
│ ├── vcs.xml
│ ├── inspectionProfiles
│ │ └── Project_Default.xml
│ ├── misc.xml
│ └── compiler.xml
└── pom.xml
├── mybatis-plugin
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── jdbc.properties
│ │ │ ├── log4j.properties
│ │ │ └── mybatis-config.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── homejim
│ │ │ └── mybatis
│ │ │ ├── plugin
│ │ │ ├── Page.java
│ │ │ ├── BoundSqlSqlSource.java
│ │ │ ├── PageUtil.java
│ │ │ ├── ReflectUtil.java
│ │ │ └── PageInterceptor.java
│ │ │ ├── mapper
│ │ │ ├── StudentMapper.java
│ │ │ └── StudentMapper.xml
│ │ │ └── entity
│ │ │ └── Student.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── StudentMapperTest.java
├── target
│ └── classes
│ │ ├── jdbc.properties
│ │ ├── log4j.properties
│ │ ├── mybatis-config.xml
│ │ └── com
│ │ └── homejim
│ │ └── mybatis
│ │ └── mapper
│ │ └── StudentMapper.xml
├── .idea
│ ├── vcs.xml
│ ├── modules.xml
│ ├── misc.xml
│ ├── libraries
│ │ ├── Maven__junit_junit_4_12.xml
│ │ ├── Maven__log4j_log4j_1_2_17.xml
│ │ ├── Maven__org_mybatis_mybatis_3_4_5.xml
│ │ ├── Maven__org_slf4j_slf4j_api_1_7_25.xml
│ │ ├── Maven__org_projectlombok_lombok_1_18_8.xml
│ │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml
│ │ ├── Maven__org_slf4j_slf4j_log4j12_1_7_25.xml
│ │ ├── Maven__mysql_mysql_connector_java_5_1_45.xml
│ │ ├── Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml
│ │ └── Maven__org_apache_logging_log4j_log4j_core_2_10_0.xml
│ └── compiler.xml
├── .gitignore
├── mybatis-plugin.iml
└── pom.xml
├── .gitignore
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/mybatis-generator/mybatis-generator.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/mybatis-mapping/mybatis-mapping.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/mybatisGenericTokenParser.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/mybatis-dynamic/target/classes/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | url=jdbc:mysql://localhost:3306/mybatis
3 | username=root
4 | password=jim777
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | url=jdbc:mysql://localhost:3306/mybatis
3 | username=root
4 | password=jim777
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | url=jdbc:mysql://localhost:3306/mybatis
3 | username=root
4 | password=jim777
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | driver=com.mysql.jdbc.Driver
2 | url=jdbc:mysql://localhost:3306/mybatis
3 | username=root
4 | password=jim777
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/helloMybatis/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/mybatis-cache/target/classes/jdbc.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
3 | jdbc.username=root
4 | jdbc.password=jim666
--------------------------------------------------------------------------------
/helloMybatis/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://localhost:3306/cloudfa?useUnicode=true&characterEncoding=utf8
3 | jdbc.username=root
4 | jdbc.password=jim666
--------------------------------------------------------------------------------
/mybatis-cache/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
3 | jdbc.username=root
4 | jdbc.password=jim666
--------------------------------------------------------------------------------
/mybatis-dynamic/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/resources/jdbc.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://localhost:3306/cloudfa?useUnicode=true&characterEncoding=utf8
3 | jdbc.username=root
4 | jdbc.password=123456
--------------------------------------------------------------------------------
/mybatis-plugin/target/classes/jdbc.properties:
--------------------------------------------------------------------------------
1 | jdbc.driver=com.mysql.jdbc.Driver
2 | jdbc.url=jdbc:mysql://localhost:3306/cloudfa?useUnicode=true&characterEncoding=utf8
3 | jdbc.username=root
4 | jdbc.password=123456
--------------------------------------------------------------------------------
/mybatis-cache/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/src/main/java/com/homejim/myabtis/TokenHandler.java:
--------------------------------------------------------------------------------
1 | package com.homejim.myabtis;
2 |
3 | /**
4 | * @author Clinton Begin
5 | */
6 | public interface TokenHandler {
7 | String handleToken(String content);
8 | }
9 |
10 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/plugin/Page.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.plugin;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Getter;
5 |
6 | @AllArgsConstructor
7 | @Getter
8 | public class Page {
9 | private int offset;
10 | private int limit;
11 |
12 | }
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/BlogCustom.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class BlogCustom extends Blog{
4 | private Author author;
5 |
6 | public Author getAuthor() {
7 | return author;
8 | }
9 |
10 | public void setAuthor(Author author) {
11 | this.author = author;
12 | }
13 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 | /.idea/
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/mybatis-plugin/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/BlogPostBO.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | import java.util.List;
4 |
5 | public class BlogPostBO extends Blog {
6 |
7 | private List posts;
8 |
9 | public List getPosts() {
10 | return posts;
11 | }
12 |
13 | public void setPosts(List posts) {
14 | this.posts = posts;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/mybatis-cache/src/main/java/com/homejim/mybatis/mapper/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 |
5 | import java.util.List;
6 |
7 | public interface StudentMapper {
8 |
9 | /**
10 | *
11 | * @return
12 | */
13 | List selectAll();
14 |
15 | Student selectByPrimaryKey(int id);
16 |
17 | int updateByPrimaryKey(Student student);
18 | }
--------------------------------------------------------------------------------
/mybatis-cache/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/plugin/BoundSqlSqlSource.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.plugin;
2 |
3 | import org.apache.ibatis.mapping.BoundSql;
4 | import org.apache.ibatis.mapping.SqlSource;
5 |
6 | public class BoundSqlSqlSource implements SqlSource {
7 |
8 | private BoundSql boundSql;
9 |
10 | public BoundSqlSqlSource(BoundSql boundSql) {
11 | this.boundSql = boundSql;
12 | }
13 |
14 | @Override
15 | public BoundSql getBoundSql(Object parameterObject) {
16 | return boundSql;
17 | }
18 | }
--------------------------------------------------------------------------------
/mybatis-cache/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/mybatis-mapping/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern= [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-generator/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/mybatis-cache/target/classes/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-plugin/target/classes/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/helloMybatis/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/helloMybatis/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-cache/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-dynamic/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-dynamic/target/classes/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=INFO,Console
2 | log4j.additivity.org.apache=true
3 |
4 | log4j.logger.com.homejim.mybatis.mapper=TRACE
5 |
6 | #Console
7 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
8 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
9 | log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
10 | log4j.logger.java.sql.ResultSet=DEBUG
11 | log4j.logger.org.apache=DEBUG
12 | log4j.logger.java.sql.Connection=DEBUG
13 | log4j.logger.java.sql.Statement=DEBUG
14 | log4j.logger.java.sql.PreparedStatement=DEBUG
15 |
--------------------------------------------------------------------------------
/helloMybatis/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-cache/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-dynamic/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-mapping/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-generator/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__junit_junit_4_12.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/plugin/PageUtil.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.plugin;
2 |
3 | /**
4 | * @author homejim
5 | */
6 | public class PageUtil {
7 | private static final ThreadLocal LOCAL_PAGE = new ThreadLocal();
8 |
9 | public static void setPagingParam(int offset, int limit) {
10 | Page page = new Page(offset, limit);
11 | LOCAL_PAGE.set(page);
12 | }
13 |
14 | public static void removePagingParam() {
15 | LOCAL_PAGE.remove();
16 | }
17 |
18 | public static Page getPaingParam() {
19 | return LOCAL_PAGE.get();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__log4j_log4j_1_2_17.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_mybatis_mybatis_3_4_5.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_projectlombok_lombok_1_18_8.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_slf4j_slf4j_log4j12_1_7_25.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__mysql_mysql_connector_java_5_1_45.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_apache_logging_log4j_log4j_api_2_10_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-plugin/.idea/libraries/Maven__org_apache_logging_log4j_log4j_core_2_10_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/BlogBO.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class BlogBO extends Blog{
4 | private String username;
5 |
6 | private String email;
7 |
8 | private String userId;
9 |
10 | public String getUsername() {
11 | return username;
12 | }
13 |
14 | public void setUsername(String username) {
15 | this.username = username;
16 | }
17 |
18 | public String getEmail() {
19 | return email;
20 | }
21 |
22 | public void setEmail(String email) {
23 | this.email = email;
24 | }
25 |
26 | public String getUserId() {
27 | return userId;
28 | }
29 |
30 | public void setUserId(String userId) {
31 | this.userId = userId;
32 | }
33 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | ## Mybatis使用
3 |
4 | 该系列文章的目的, 是从入门到精通
5 |
6 | - [mybatis 初步使用](https://www.cnblogs.com/homejim/p/9613205.html): 基于 Maven 的入门示例, 超级详细的教程
7 | - [mybatis 代码生成器(IDEA, Maven)及配置详解](https://www.cnblogs.com/homejim/p/9782403.html):还在手写 XML 和 JavaBean 吗?快来试一下代码生成器吧
8 | - [mybatis 多个接口参数的注解使用方式(@Param)](https://www.cnblogs.com/homejim/p/9758930.html): 接口中需要传入多个参数, 试一下注解模式吧
9 | - [mybatis-高级结果映射之一对一](https://www.cnblogs.com/homejim/p/9785802.html): 来试试高级映射功能
10 | - [mybatis-高级结果映射之一对多](https://www.cnblogs.com/homejim/p/9808847.html): 一对多的高级映射
11 | - [mybatis 缓存的使用, 看这篇就够了](https://www.cnblogs.com/homejim/p/9729191.html): 一级缓存, 二级缓存, 提升性能的同时, 也注意它的限制
12 | - [MyBatis动态SQL](https://www.cnblogs.com/homejim/p/9909657.html):我再也不要再代码中拼接语句了!! 来用 mybatis 动态 SQL 爽一爽吧
13 | - [MyBatis 插件使用-自定义简单的分页插件](https://blog.csdn.net/weixin_37139197/article/details/101539995):在使用mybatis的过程中, 自定义插件时有发生, 此例子作为一个参考。
14 | - 未完待续...
15 |
--------------------------------------------------------------------------------
/helloMybatis/src/main/java/com/homejim/mybatis/mapper/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import org.apache.ibatis.annotations.Param;
5 |
6 | import java.util.Date;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | public interface StudentMapper {
11 |
12 | /**
13 | *
14 | * @return
15 | */
16 | List selectAll();
17 |
18 | Student selectByPrimaryKey(int id);
19 |
20 | int updateByPrimaryKey(Student student);
21 |
22 | /**
23 | * 获取一段时间内的用户
24 | * @param params
25 | * @return
26 | */
27 | List selectBetweenCreatedTime(Map params);
28 |
29 |
30 | /**
31 | *
32 | * @param bTime 开始时间
33 | * @param eTime 结束时间
34 | * @return
35 | */
36 | List selectBetweenCreatedTimeAnno(@Param("bTime")Date bTime, @Param("eTime")Date eTime);
37 | }
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/mapper/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import org.apache.ibatis.annotations.Param;
5 |
6 | import java.util.Date;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | public interface StudentMapper {
11 |
12 | /**
13 | *
14 | * @return
15 | */
16 | List selectAll();
17 |
18 | Student selectByPrimaryKey(int id);
19 |
20 | int updateByPrimaryKey(Student student);
21 |
22 | /**
23 | * 获取一段时间内的用户
24 | * @param params
25 | * @return
26 | */
27 | List selectBetweenCreatedTime(Map params);
28 |
29 |
30 | /**
31 | *
32 | * @param bTime 开始时间
33 | * @param eTime 结束时间
34 | * @return
35 | */
36 | List selectBetweenCreatedTimeAnno(@Param("bTime")Date bTime, @Param("eTime")Date eTime);
37 | }
--------------------------------------------------------------------------------
/helloMybatis/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/mybatis-cache/target/classes/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/mybatis-cache/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-GenericTokenParser
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | org.apache.logging.log4j
14 | log4j-core
15 | 2.10.0
16 |
17 |
18 | junit
19 | junit
20 | RELEASE
21 | test
22 |
23 |
24 |
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-compiler-plugin
29 |
30 | 6
31 | 6
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/test/java/com/mybatis/homejim/mapper/PostMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.mybatis.homejim.mapper;
2 |
3 | import com.homejim.mybatis.entity.Post;
4 | import com.homejim.mybatis.mapper.PostMapper;
5 | import org.apache.ibatis.io.Resources;
6 | import org.apache.ibatis.session.SqlSession;
7 | import org.apache.ibatis.session.SqlSessionFactory;
8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9 | import org.junit.BeforeClass;
10 | import org.junit.Test;
11 |
12 | import java.io.IOException;
13 | import java.io.Reader;
14 | import java.util.List;
15 |
16 | public class PostMapperTest {
17 | private static SqlSessionFactory sqlSessionFactory;
18 |
19 | @BeforeClass
20 | public static void init() {
21 | try {
22 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
23 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
24 | } catch (IOException e) {
25 | e.printStackTrace();
26 | }
27 | }
28 | @Test
29 | public void testSelectListByBlogId(){
30 | SqlSession sqlSession = sqlSessionFactory.openSession();
31 | PostMapper postMapper = sqlSession.getMapper(PostMapper.class);
32 | List posts = postMapper.selectPostByBlogId(1);
33 | for (int i = 0; i < posts.size(); i++) {
34 | System.out.println(posts.get(i).getContent());
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/mybatis-plugin/target/classes/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/mybatis-dynamic/target/classes/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
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 |
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
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 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/AuthorMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Author;
4 |
5 | public interface AuthorMapper {
6 | /**
7 | * This method was generated by MyBatis Generator.
8 | * This method corresponds to the database table author
9 | *
10 | * @mbg.generated
11 | */
12 | int deleteByPrimaryKey(Integer id);
13 |
14 | /**
15 | * This method was generated by MyBatis Generator.
16 | * This method corresponds to the database table author
17 | *
18 | * @mbg.generated
19 | */
20 | int insert(Author record);
21 |
22 | /**
23 | * This method was generated by MyBatis Generator.
24 | * This method corresponds to the database table author
25 | *
26 | * @mbg.generated
27 | */
28 | int insertSelective(Author record);
29 |
30 | /**
31 | * This method was generated by MyBatis Generator.
32 | * This method corresponds to the database table author
33 | *
34 | * @mbg.generated
35 | */
36 | Author selectByPrimaryKey(Integer id);
37 |
38 | /**
39 | * This method was generated by MyBatis Generator.
40 | * This method corresponds to the database table author
41 | *
42 | * @mbg.generated
43 | */
44 | int updateByPrimaryKeySelective(Author record);
45 |
46 | /**
47 | * This method was generated by MyBatis Generator.
48 | * This method corresponds to the database table author
49 | *
50 | * @mbg.generated
51 | */
52 | int updateByPrimaryKey(Author record);
53 |
54 | /**
55 | * 嵌套查询使用的方法
56 | * @param id
57 | * @return
58 | */
59 | Author selectById(Integer id);
60 | }
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
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 |
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
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 |
--------------------------------------------------------------------------------
/mybatis-generator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-generator
9 | 1.0-SNAPSHOT
10 |
11 |
12 | mybatis-generator
13 |
14 |
15 |
16 | org.mybatis.generator
17 | mybatis-generator-maven-plugin
18 | 1.3.7
19 |
20 |
21 | src/main/resources/generator.xml
22 | true
23 | true
24 |
25 |
26 |
27 | Generate MyBatis Artifacts
28 |
29 | generate
30 |
31 |
32 |
33 |
34 |
35 | org.mybatis.generator
36 | mybatis-generator-core
37 | 1.3.7
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/CommentMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Comment;
4 |
5 | public interface CommentMapper {
6 | /**
7 | * This method was generated by MyBatis Generator.
8 | * This method corresponds to the database table comment
9 | *
10 | * @mbg.generated
11 | */
12 | int deleteByPrimaryKey(Integer id);
13 |
14 | /**
15 | * This method was generated by MyBatis Generator.
16 | * This method corresponds to the database table comment
17 | *
18 | * @mbg.generated
19 | */
20 | int insert(Comment record);
21 |
22 | /**
23 | * This method was generated by MyBatis Generator.
24 | * This method corresponds to the database table comment
25 | *
26 | * @mbg.generated
27 | */
28 | int insertSelective(Comment record);
29 |
30 | /**
31 | * This method was generated by MyBatis Generator.
32 | * This method corresponds to the database table comment
33 | *
34 | * @mbg.generated
35 | */
36 | Comment selectByPrimaryKey(Integer id);
37 |
38 | /**
39 | * This method was generated by MyBatis Generator.
40 | * This method corresponds to the database table comment
41 | *
42 | * @mbg.generated
43 | */
44 | int updateByPrimaryKeySelective(Comment record);
45 |
46 | /**
47 | * This method was generated by MyBatis Generator.
48 | * This method corresponds to the database table comment
49 | *
50 | * @mbg.generated
51 | */
52 | int updateByPrimaryKeyWithBLOBs(Comment record);
53 |
54 | /**
55 | * This method was generated by MyBatis Generator.
56 | * This method corresponds to the database table comment
57 | *
58 | * @mbg.generated
59 | */
60 | int updateByPrimaryKey(Comment record);
61 | }
--------------------------------------------------------------------------------
/mybatis-cache/src/main/java/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
18 |
19 |
24 |
30 |
31 | update student
32 | set
33 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
34 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
35 | where student_id=#{studentId, jdbcType=INTEGER}
36 |
37 |
--------------------------------------------------------------------------------
/mybatis-cache/target/classes/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
18 |
19 |
24 |
30 |
31 | update student
32 | set
33 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
34 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
35 | where student_id=#{studentId, jdbcType=INTEGER}
36 |
37 |
--------------------------------------------------------------------------------
/mybatis-plugin/mybatis-plugin.iml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/PostMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Post;
4 |
5 | import java.util.List;
6 |
7 | public interface PostMapper {
8 | /**
9 | * This method was generated by MyBatis Generator.
10 | * This method corresponds to the database table post
11 | *
12 | * @mbg.generated
13 | */
14 | int deleteByPrimaryKey(Integer id);
15 |
16 | /**
17 | * This method was generated by MyBatis Generator.
18 | * This method corresponds to the database table post
19 | *
20 | * @mbg.generated
21 | */
22 | int insert(Post record);
23 |
24 | /**
25 | * This method was generated by MyBatis Generator.
26 | * This method corresponds to the database table post
27 | *
28 | * @mbg.generated
29 | */
30 | int insertSelective(Post record);
31 |
32 | /**
33 | * This method was generated by MyBatis Generator.
34 | * This method corresponds to the database table post
35 | *
36 | * @mbg.generated
37 | */
38 | Post selectByPrimaryKey(Integer id);
39 |
40 | /**
41 | * This method was generated by MyBatis Generator.
42 | * This method corresponds to the database table post
43 | *
44 | * @mbg.generated
45 | */
46 | int updateByPrimaryKeySelective(Post record);
47 |
48 | /**
49 | * This method was generated by MyBatis Generator.
50 | * This method corresponds to the database table post
51 | *
52 | * @mbg.generated
53 | */
54 | int updateByPrimaryKeyWithBLOBs(Post record);
55 |
56 | /**
57 | * This method was generated by MyBatis Generator.
58 | * This method corresponds to the database table post
59 | *
60 | * @mbg.generated
61 | */
62 | int updateByPrimaryKey(Post record);
63 |
64 | /**
65 | * 根据博客 id 获取发布的文章信息
66 | * @param blogId 博客 id
67 | * @return
68 | */
69 | List selectPostByBlogId(int blogId);
70 | }
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/sql/mybatis-mapping.sql:
--------------------------------------------------------------------------------
1 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
2 |
3 | USE `mybatis`;
4 |
5 | DROP TABLE IF EXISTS `author`;
6 |
7 | CREATE TABLE `author` (
8 | `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '作者id',
9 | `username` VARCHAR(20) DEFAULT NULL COMMENT '用户名',
10 | `password` VARCHAR(20) DEFAULT NULL COMMENT '密码',
11 | `email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
12 | PRIMARY KEY (`id`)
13 | ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='作者表';
14 |
15 | DROP TABLE IF EXISTS `blog`;
16 |
17 | CREATE TABLE `blog` (
18 | `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '博客id',
19 | `title` VARCHAR(20) DEFAULT NULL COMMENT '博客标题',
20 | `author_id` INT(10) UNSIGNED NOT NULL COMMENT '作者id',
21 | PRIMARY KEY (`id`),
22 | UNIQUE KEY `author_fk` (`author_id`),
23 | CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `author`(`id`)
24 | ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='博客表';
25 |
26 |
27 | DROP TABLE IF EXISTS `post`;
28 |
29 | CREATE TABLE `post` (
30 | `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '博客id',
31 | `blog_id` INT(10) UNSIGNED NOT NULL COMMENT '博客标题',
32 | `content` TEXT DEFAULT NULL COMMENT '博客内容',
33 | `draft` INT NOT NULL COMMENT '草稿',
34 | PRIMARY KEY (`id`),
35 | CONSTRAINT `blog_fk` FOREIGN KEY (`blog_id`) REFERENCES `blog`(`id`)
36 | ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='发布表';
37 |
38 | DROP TABLE IF EXISTS `comment`;
39 |
40 | CREATE TABLE `comment` (
41 | `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '博客id',
42 | `post_id` INT(10) UNSIGNED NOT NULL COMMENT '博客标题',
43 | `content` TEXT DEFAULT NULL COMMENT '博客内容',
44 | PRIMARY KEY (`id`),
45 | CONSTRAINT `post_fk` FOREIGN KEY (`post_id`) REFERENCES `post`(`id`)
46 | ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='评论表';
47 |
48 |
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/java/com/homejim/mybatis/mapper/StudentMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import org.apache.ibatis.annotations.Param;
5 |
6 | import java.util.Date;
7 | import java.util.List;
8 | import java.util.Map;
9 |
10 | public interface StudentMapper {
11 |
12 | /**
13 | *
14 | * @return
15 | */
16 | List selectAll();
17 |
18 | Student selectByPrimaryKey(int id);
19 |
20 | int updateByPrimaryKey(Student student);
21 |
22 | /**
23 | * 获取一段时间内的用户
24 | * @param params
25 | * @return
26 | */
27 | List selectBetweenCreatedTime(Map params);
28 |
29 |
30 | /**
31 | *
32 | * @param bTime 开始时间
33 | * @param eTime 结束时间
34 | * @return
35 | */
36 | List selectBetweenCreatedTimeAnno(@Param("bTime")Date bTime, @Param("eTime")Date eTime);
37 |
38 | /**
39 | * 根据输入的学生信息进行条件检索
40 | * 1. 当只输入用户名时, 使用用户名进行模糊检索;
41 | * 2. 当只输入邮箱时, 使用性别进行完全匹配
42 | * 3. 当用户名和性别都存在时, 用这两个条件进行查询匹配的用
43 | */
44 | List selectByStudentSelective(Student student);
45 |
46 | /**
47 | * 更新非空属性
48 | */
49 | int updateByPrimaryKeySelective(Student record);
50 |
51 | /**
52 | * 非空字段才进行插入
53 | */
54 | int insertSelective(Student record);
55 |
56 | /**
57 | * - 当 studen_id 有值时, 使用 studen_id 进行查询;
58 | * - 当 studen_id 没有值时, 使用 name 进行查询;
59 | * - 否则返回空
60 | */
61 | Student selectByIdOrName(Student record);
62 |
63 | /**
64 | * 根据输入的学生信息进行条件检索
65 | * 1. 当只输入用户名时, 使用用户名进行模糊检索;
66 | * 2. 当只输入邮箱时, 使用性别进行完全匹配
67 | * 3. 当用户名和性别都存在时, 用这两个条件进行查询匹配的用
68 | */
69 | List selectByStudentSelectiveWhereTag(Student student);
70 |
71 |
72 | /**
73 | * 获取 id 集合中的用户信息
74 | * @param ids
75 | * @return
76 | */
77 | List selectByStudentIdList(@Param("ids") List ids);
78 |
79 | /**
80 | * 批量插入学生
81 | */
82 | int insertList(List students);
83 | }
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/plugin/ReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.plugin;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | /**
6 | * 反射工具类,直接修改对象值
7 | */
8 | public class ReflectUtil {
9 | /**
10 | * 获取指定对象的指定属性
11 | *
12 | * @param obj 指定对象
13 | * @param fieldName 指定属性名称
14 | * @return 指定属性
15 | */
16 | public static Object getFieldValue(Object obj, String fieldName) {
17 | Object result = null;
18 | Field field = ReflectUtil.getField(obj, fieldName);
19 | if (field != null) {
20 | field.setAccessible(true);
21 | try {
22 | result = field.get(obj);
23 | } catch (IllegalArgumentException e) {
24 | e.printStackTrace();
25 | } catch (IllegalAccessException e) {
26 | e.printStackTrace();
27 | }
28 | }
29 | return result;
30 | }
31 |
32 | /**
33 | * 获取指定对象里面的指定属性对象
34 | *
35 | * @param obj 目标对象
36 | * @param fieldName 指定属性名称
37 | * @return 属性对象
38 | */
39 | private static Field getField(Object obj, String fieldName) {
40 | Field field = null;
41 | for (Class> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
42 | try {
43 | field = clazz.getDeclaredField(fieldName);
44 | break;
45 | } catch (NoSuchFieldException e) {
46 | // do nothing
47 | }
48 | }
49 | return field;
50 | }
51 |
52 | /**
53 | * 设置指定对象的指定属性值
54 | *
55 | * @param obj 指定对象
56 | * @param fieldName 指定属性
57 | * @param fieldValue 指定属性值
58 | */
59 | public static void setFieldValue(Object obj, String fieldName, String fieldValue) {
60 | Field field = ReflectUtil.getField(obj, fieldName);
61 | if (field != null) {
62 | try {
63 | field.setAccessible(true);
64 | field.set(obj, fieldValue);
65 | } catch (IllegalArgumentException e) {
66 | e.printStackTrace();
67 | } catch (IllegalAccessException e) {
68 | e.printStackTrace();
69 | }
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/mybatis-generator/src/main/resources/generator.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
33 |
34 |
35 |
36 |
37 |
38 |
41 |
42 |
43 |
44 |
45 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/resources/sql.sql:
--------------------------------------------------------------------------------
1 | /*
2 | SQLyog Ultimate v12.08 (64 bit)
3 | MySQL - 8.0.11 : Database - mybatis
4 | *********************************************************************
5 | */
6 |
7 |
8 | /*!40101 SET NAMES utf8 */;
9 |
10 | /*!40101 SET SQL_MODE=''*/;
11 |
12 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
13 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
14 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
15 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
16 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
17 |
18 | USE `mybatis`;
19 |
20 | /*Table structure for table `student` */
21 |
22 | DROP TABLE IF EXISTS `student`;
23 |
24 | CREATE TABLE `student` (
25 | `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
26 | `name` varchar(20) DEFAULT NULL COMMENT '姓名',
27 | `phone` varchar(20) DEFAULT NULL COMMENT '电话',
28 | `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
29 | `sex` tinyint(4) DEFAULT NULL COMMENT '性别',
30 | `locked` tinyint(4) DEFAULT NULL COMMENT '状态(0:正常,1:锁定)',
31 | `gmt_created` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '存入数据库的时间',
32 | `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的时间',
33 | `delete` int(11) DEFAULT NULL,
34 | PRIMARY KEY (`student_id`)
35 | ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='学生表';
36 |
37 | /*Data for the table `student` */
38 |
39 | insert into `student`(`student_id`,`name`,`phone`,`email`,`sex`,`locked`,`gmt_created`,`gmt_modified`,`delete`) values (1,'小明','13821378270','xiaoming@mybatis.cn',1,0,'2018-08-29 18:27:42','2018-10-08 20:54:25',NULL),(2,'大明','13821378271','xiaoli@mybatis.cn',0,0,'2018-08-30 18:27:42','2018-10-08 20:54:29',NULL),(3,'小刚','13821378272','xiaogang@mybatis.cn',1,0,'2018-08-31 18:27:42','2018-10-08 20:55:08',NULL),(4,'小花','13821378273','xiaohua@mybatis.cn',0,0,'2018-09-01 18:27:42','2018-10-08 20:55:12',NULL),(5,'小强','13821378274','xiaoqiang@mybatis.cn',1,0,'2018-09-02 18:27:42','2018-10-08 20:55:18',NULL),(6,'小红','13821378275','xiaohong@mybatis.cn',0,0,'2018-09-03 18:27:42','2018-10-08 20:55:27',NULL);
40 |
41 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
42 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
43 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
44 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
45 |
--------------------------------------------------------------------------------
/helloMybatis/src/main/java/com/homejim/mybatis/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | import java.util.Date;
4 |
5 | public class Student {
6 | private Integer studentId;
7 |
8 | private String name;
9 |
10 | private String phone;
11 |
12 | private String email;
13 |
14 | private Byte sex;
15 |
16 | private Byte locked;
17 |
18 | private Date gmtCreated;
19 |
20 | private Date gmtModified;
21 |
22 | public Integer getStudentId() {
23 | return studentId;
24 | }
25 |
26 | public void setStudentId(Integer studentId) {
27 | this.studentId = studentId;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name == null ? null : name.trim();
36 | }
37 |
38 | public String getPhone() {
39 | return phone;
40 | }
41 |
42 | public void setPhone(String phone) {
43 | this.phone = phone == null ? null : phone.trim();
44 | }
45 |
46 | public String getEmail() {
47 | return email;
48 | }
49 |
50 | public void setEmail(String email) {
51 | this.email = email == null ? null : email.trim();
52 | }
53 |
54 | public Byte getSex() {
55 | return sex;
56 | }
57 |
58 | public void setSex(Byte sex) {
59 | this.sex = sex;
60 | }
61 |
62 | public Byte getLocked() {
63 | return locked;
64 | }
65 |
66 | public void setLocked(Byte locked) {
67 | this.locked = locked;
68 | }
69 |
70 | public Date getGmtCreated() {
71 | return gmtCreated;
72 | }
73 |
74 | public void setGmtCreated(Date gmtCreated) {
75 | this.gmtCreated = gmtCreated;
76 | }
77 |
78 | public Date getGmtModified() {
79 | return gmtModified;
80 | }
81 |
82 | public void setGmtModified(Date gmtModified) {
83 | this.gmtModified = gmtModified;
84 | }
85 |
86 | @Override
87 | public String toString() {
88 | return "Student{" +
89 | "studentId=" + studentId +
90 | ", name='" + name + '\'' +
91 | ", phone='" + phone + '\'' +
92 | ", email='" + email + '\'' +
93 | ", sex=" + sex +
94 | ", locked=" + locked +
95 | ", gmtCreated=" + gmtCreated +
96 | ", gmtModified=" + gmtModified +
97 | '}';
98 | }
99 | }
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/java/com/homejim/mybatis/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | import java.util.Date;
4 |
5 | public class Student {
6 | private Integer studentId;
7 |
8 | private String name;
9 |
10 | private String phone;
11 |
12 | private String email;
13 |
14 | private Byte sex;
15 |
16 | private Byte locked;
17 |
18 | private Date gmtCreated;
19 |
20 | private Date gmtModified;
21 |
22 | public Integer getStudentId() {
23 | return studentId;
24 | }
25 |
26 | public void setStudentId(Integer studentId) {
27 | this.studentId = studentId;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name == null ? null : name.trim();
36 | }
37 |
38 | public String getPhone() {
39 | return phone;
40 | }
41 |
42 | public void setPhone(String phone) {
43 | this.phone = phone == null ? null : phone.trim();
44 | }
45 |
46 | public String getEmail() {
47 | return email;
48 | }
49 |
50 | public void setEmail(String email) {
51 | this.email = email == null ? null : email.trim();
52 | }
53 |
54 | public Byte getSex() {
55 | return sex;
56 | }
57 |
58 | public void setSex(Byte sex) {
59 | this.sex = sex;
60 | }
61 |
62 | public Byte getLocked() {
63 | return locked;
64 | }
65 |
66 | public void setLocked(Byte locked) {
67 | this.locked = locked;
68 | }
69 |
70 | public Date getGmtCreated() {
71 | return gmtCreated;
72 | }
73 |
74 | public void setGmtCreated(Date gmtCreated) {
75 | this.gmtCreated = gmtCreated;
76 | }
77 |
78 | public Date getGmtModified() {
79 | return gmtModified;
80 | }
81 |
82 | public void setGmtModified(Date gmtModified) {
83 | this.gmtModified = gmtModified;
84 | }
85 |
86 | @Override
87 | public String toString() {
88 | return "Student{" +
89 | "studentId=" + studentId +
90 | ", name='" + name + '\'' +
91 | ", phone='" + phone + '\'' +
92 | ", email='" + email + '\'' +
93 | ", sex=" + sex +
94 | ", locked=" + locked +
95 | ", gmtCreated=" + gmtCreated +
96 | ", gmtModified=" + gmtModified +
97 | '}';
98 | }
99 | }
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | import java.util.Date;
4 |
5 | public class Student {
6 | private Integer studentId;
7 |
8 | private String name;
9 |
10 | private String phone;
11 |
12 | private String email;
13 |
14 | private Byte sex;
15 |
16 | private Byte locked;
17 |
18 | private Date gmtCreated;
19 |
20 | private Date gmtModified;
21 |
22 | public Integer getStudentId() {
23 | return studentId;
24 | }
25 |
26 | public void setStudentId(Integer studentId) {
27 | this.studentId = studentId;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name == null ? null : name.trim();
36 | }
37 |
38 | public String getPhone() {
39 | return phone;
40 | }
41 |
42 | public void setPhone(String phone) {
43 | this.phone = phone == null ? null : phone.trim();
44 | }
45 |
46 | public String getEmail() {
47 | return email;
48 | }
49 |
50 | public void setEmail(String email) {
51 | this.email = email == null ? null : email.trim();
52 | }
53 |
54 | public Byte getSex() {
55 | return sex;
56 | }
57 |
58 | public void setSex(Byte sex) {
59 | this.sex = sex;
60 | }
61 |
62 | public Byte getLocked() {
63 | return locked;
64 | }
65 |
66 | public void setLocked(Byte locked) {
67 | this.locked = locked;
68 | }
69 |
70 | public Date getGmtCreated() {
71 | return gmtCreated;
72 | }
73 |
74 | public void setGmtCreated(Date gmtCreated) {
75 | this.gmtCreated = gmtCreated;
76 | }
77 |
78 | public Date getGmtModified() {
79 | return gmtModified;
80 | }
81 |
82 | public void setGmtModified(Date gmtModified) {
83 | this.gmtModified = gmtModified;
84 | }
85 |
86 | @Override
87 | public String toString() {
88 | return "Student{" +
89 | "studentId=" + studentId +
90 | ", name='" + name + '\'' +
91 | ", phone='" + phone + '\'' +
92 | ", email='" + email + '\'' +
93 | ", sex=" + sex +
94 | ", locked=" + locked +
95 | ", gmtCreated=" + gmtCreated +
96 | ", gmtModified=" + gmtModified +
97 | '}';
98 | }
99 | }
--------------------------------------------------------------------------------
/helloMybatis/src/main/java/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
17 |
18 |
23 |
29 |
35 |
41 |
42 | update student
43 | set
44 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
45 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
46 | where student_id=#{studentId, jdbcType=INTEGER}
47 |
48 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
17 |
18 |
23 |
29 |
35 |
41 |
42 | update student
43 | set
44 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
45 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
46 | where student_id=#{studentId, jdbcType=INTEGER}
47 |
48 |
--------------------------------------------------------------------------------
/mybatis-plugin/target/classes/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
17 |
18 |
23 |
29 |
35 |
41 |
42 | update student
43 | set
44 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
45 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
46 | where student_id=#{studentId, jdbcType=INTEGER}
47 |
48 |
--------------------------------------------------------------------------------
/mybatis-dynamic/target/classes/sql.sql:
--------------------------------------------------------------------------------
1 | /*
2 | SQLyog Ultimate v12.08 (64 bit)
3 | MySQL - 8.0.11 : Database - mybatis
4 | *********************************************************************
5 | */
6 |
7 | /*!40101 SET NAMES utf8 */;
8 |
9 | /*!40101 SET SQL_MODE=''*/;
10 |
11 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
12 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
13 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
14 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
15 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
16 |
17 | USE `mybatis`;
18 |
19 | /*Table structure for table `student` */
20 |
21 | DROP TABLE IF EXISTS `student`;
22 |
23 | CREATE TABLE `student` (
24 | `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
25 | `name` varchar(20) DEFAULT NULL COMMENT '姓名',
26 | `phone` varchar(20) DEFAULT NULL COMMENT '电话',
27 | `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
28 | `sex` tinyint(4) DEFAULT NULL COMMENT '性别',
29 | `locked` tinyint(4) DEFAULT NULL COMMENT '状态(0:正常,1:锁定)',
30 | `gmt_created` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '存入数据库的时间',
31 | `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的时间',
32 | `delete` int(11) DEFAULT NULL,
33 | PRIMARY KEY (`student_id`)
34 | ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='学生表';
35 |
36 | /*Data for the table `student` */
37 |
38 | insert into `student`(`student_id`,`name`,`phone`,`email`,`sex`,`locked`,`gmt_created`,`gmt_modified`,`delete`) values (1,'小明','13821378270','xiaoming@mybatis.cn',1,0,'2018-08-29 18:27:42','2018-10-08 20:54:25',NULL),(2,'小丽','13821378271','xiaoli@mybatis.cn',0,0,'2018-08-30 18:27:42','2018-10-08 20:54:29',NULL),(3,'小刚','13821378272','xiaogang@mybatis.cn',1,0,'2018-08-31 18:27:42','2018-10-08 20:55:08',NULL),(4,'小花','13821378273','xiaohua@mybatis.cn',0,0,'2018-09-01 18:27:42','2018-10-08 20:55:12',NULL),(5,'小强','13821378274','xiaoqiang@mybatis.cn',1,0,'2018-09-02 18:27:42','2018-10-08 20:55:18',NULL),(6,'小红','13821378275','xiaohong@mybatis.cn',0,0,'2018-09-03 18:27:42','2018-10-08 20:55:27',NULL);
39 |
40 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
41 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
42 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
43 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
44 |
--------------------------------------------------------------------------------
/mybatis-cache/src/main/java/com/homejim/mybatis/entity/Student.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | public class Student implements Serializable {
7 |
8 | private static final long serialVersionUID = -4852658907724408209L;
9 |
10 | private Integer studentId;
11 |
12 | private String name;
13 |
14 | private String phone;
15 |
16 | private String email;
17 |
18 | private Byte sex;
19 |
20 | private Byte locked;
21 |
22 | private Date gmtCreated;
23 |
24 | private Date gmtModified;
25 |
26 | public Integer getStudentId() {
27 | return studentId;
28 | }
29 |
30 | public void setStudentId(Integer studentId) {
31 | this.studentId = studentId;
32 | }
33 |
34 | public String getName() {
35 | return name;
36 | }
37 |
38 | public void setName(String name) {
39 | this.name = name == null ? null : name.trim();
40 | }
41 |
42 | public String getPhone() {
43 | return phone;
44 | }
45 |
46 | public void setPhone(String phone) {
47 | this.phone = phone == null ? null : phone.trim();
48 | }
49 |
50 | public String getEmail() {
51 | return email;
52 | }
53 |
54 | public void setEmail(String email) {
55 | this.email = email == null ? null : email.trim();
56 | }
57 |
58 | public Byte getSex() {
59 | return sex;
60 | }
61 |
62 | public void setSex(Byte sex) {
63 | this.sex = sex;
64 | }
65 |
66 | public Byte getLocked() {
67 | return locked;
68 | }
69 |
70 | public void setLocked(Byte locked) {
71 | this.locked = locked;
72 | }
73 |
74 | public Date getGmtCreated() {
75 | return gmtCreated;
76 | }
77 |
78 | public void setGmtCreated(Date gmtCreated) {
79 | this.gmtCreated = gmtCreated;
80 | }
81 |
82 | public Date getGmtModified() {
83 | return gmtModified;
84 | }
85 |
86 | public void setGmtModified(Date gmtModified) {
87 | this.gmtModified = gmtModified;
88 | }
89 |
90 | @Override
91 | public String toString() {
92 | return "Student{" +
93 | "studentId=" + studentId +
94 | ", name='" + name + '\'' +
95 | ", phone='" + phone + '\'' +
96 | ", email='" + email + '\'' +
97 | ", sex=" + sex +
98 | ", locked=" + locked +
99 | ", gmtCreated=" + gmtCreated +
100 | ", gmtModified=" + gmtModified +
101 | '}';
102 | }
103 | }
--------------------------------------------------------------------------------
/mybatis-generator/src/main/resources/generator-cache.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
41 |
42 |
43 |
44 |
45 |
46 |
49 |
50 |
51 |
52 |
53 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/BlogMapper.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Blog;
4 | import com.homejim.mybatis.entity.BlogBO;
5 | import com.homejim.mybatis.entity.BlogCustom;
6 | import com.homejim.mybatis.entity.BlogPostBO;
7 |
8 | import java.util.List;
9 |
10 | public interface BlogMapper {
11 | /**
12 | * This method was generated by MyBatis Generator.
13 | * This method corresponds to the database table blog
14 | *
15 | * @mbg.generated
16 | */
17 | int deleteByPrimaryKey(Integer id);
18 |
19 | /**
20 | * This method was generated by MyBatis Generator.
21 | * This method corresponds to the database table blog
22 | *
23 | * @mbg.generated
24 | */
25 | int insert(Blog record);
26 |
27 | /**
28 | * This method was generated by MyBatis Generator.
29 | * This method corresponds to the database table blog
30 | *
31 | * @mbg.generated
32 | */
33 | int insertSelective(Blog record);
34 |
35 | /**
36 | * This method was generated by MyBatis Generator.
37 | * This method corresponds to the database table blog
38 | *
39 | * @mbg.generated
40 | */
41 | Blog selectByPrimaryKey(Integer id);
42 |
43 | /**
44 | * This method was generated by MyBatis Generator.
45 | * This method corresponds to the database table blog
46 | *
47 | * @mbg.generated
48 | */
49 | int updateByPrimaryKeySelective(Blog record);
50 |
51 | /**
52 | * This method was generated by MyBatis Generator.
53 | * This method corresponds to the database table blog
54 | *
55 | * @mbg.generated
56 | */
57 | int updateByPrimaryKey(Blog record);
58 |
59 | /**
60 | * 根据博客的 id 获取博客及作者的信息
61 | * @param id
62 | * @return
63 | */
64 | BlogBO selectBoById(int id);
65 | /**
66 | * 根据博客的 id 获取博客及作者的信息
67 | * @param id
68 | * @return
69 | */
70 | BlogCustom selectCutomById(int id);
71 |
72 | /**
73 | * 根据博客的 id 获取博客及作者的信息, resultMap 方式
74 | * @param id
75 | * @return
76 | */
77 | BlogCustom selectCutomByIdMap(int id);
78 |
79 | /**
80 | * 根据博客的 id 获取博客及作者的信息, resultMap + association方式
81 | * @param id
82 | * @return
83 | */
84 | BlogCustom selectCutomByIdAMap(int id);
85 |
86 | /**
87 | * 根据博客的 id 获取博客及作者的信息, resultMap + association嵌套方式
88 | * @param id
89 | * @return
90 | */
91 | BlogCustom selectBlogAndAuthorByIdSelect(int id);
92 |
93 | /**
94 | * 获取博客及其发布的文章内容 一对多
95 | * @return
96 | */
97 | List selectBlogAndPostList();
98 |
99 | /**
100 | * 获取博客及其发布的文章内容 一对多:延迟加载
101 | * @return
102 | */
103 | List selectBlogAndPostListLazy();
104 | }
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/Blog.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class Blog {
4 | /**
5 | *
6 | * This field was generated by MyBatis Generator.
7 | * This field corresponds to the database column blog.id
8 | *
9 | * @mbg.generated
10 | */
11 | private Integer id;
12 |
13 | /**
14 | *
15 | * This field was generated by MyBatis Generator.
16 | * This field corresponds to the database column blog.title
17 | *
18 | * @mbg.generated
19 | */
20 | private String title;
21 |
22 | /**
23 | *
24 | * This field was generated by MyBatis Generator.
25 | * This field corresponds to the database column blog.author_id
26 | *
27 | * @mbg.generated
28 | */
29 | private Integer authorId;
30 |
31 | /**
32 | * This method was generated by MyBatis Generator.
33 | * This method returns the value of the database column blog.id
34 | *
35 | * @return the value of blog.id
36 | *
37 | * @mbg.generated
38 | */
39 | public Integer getId() {
40 | return id;
41 | }
42 |
43 | /**
44 | * This method was generated by MyBatis Generator.
45 | * This method sets the value of the database column blog.id
46 | *
47 | * @param id the value for blog.id
48 | *
49 | * @mbg.generated
50 | */
51 | public void setId(Integer id) {
52 | this.id = id;
53 | }
54 |
55 | /**
56 | * This method was generated by MyBatis Generator.
57 | * This method returns the value of the database column blog.title
58 | *
59 | * @return the value of blog.title
60 | *
61 | * @mbg.generated
62 | */
63 | public String getTitle() {
64 | return title;
65 | }
66 |
67 | /**
68 | * This method was generated by MyBatis Generator.
69 | * This method sets the value of the database column blog.title
70 | *
71 | * @param title the value for blog.title
72 | *
73 | * @mbg.generated
74 | */
75 | public void setTitle(String title) {
76 | this.title = title == null ? null : title.trim();
77 | }
78 |
79 | /**
80 | * This method was generated by MyBatis Generator.
81 | * This method returns the value of the database column blog.author_id
82 | *
83 | * @return the value of blog.author_id
84 | *
85 | * @mbg.generated
86 | */
87 | public Integer getAuthorId() {
88 | return authorId;
89 | }
90 |
91 | /**
92 | * This method was generated by MyBatis Generator.
93 | * This method sets the value of the database column blog.author_id
94 | *
95 | * @param authorId the value for blog.author_id
96 | *
97 | * @mbg.generated
98 | */
99 | public void setAuthorId(Integer authorId) {
100 | this.authorId = authorId;
101 | }
102 | }
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/Comment.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class Comment {
4 | /**
5 | *
6 | * This field was generated by MyBatis Generator.
7 | * This field corresponds to the database column comment.id
8 | *
9 | * @mbg.generated
10 | */
11 | private Integer id;
12 |
13 | /**
14 | *
15 | * This field was generated by MyBatis Generator.
16 | * This field corresponds to the database column comment.post_id
17 | *
18 | * @mbg.generated
19 | */
20 | private Integer postId;
21 |
22 | /**
23 | *
24 | * This field was generated by MyBatis Generator.
25 | * This field corresponds to the database column comment.content
26 | *
27 | * @mbg.generated
28 | */
29 | private String content;
30 |
31 | /**
32 | * This method was generated by MyBatis Generator.
33 | * This method returns the value of the database column comment.id
34 | *
35 | * @return the value of comment.id
36 | *
37 | * @mbg.generated
38 | */
39 | public Integer getId() {
40 | return id;
41 | }
42 |
43 | /**
44 | * This method was generated by MyBatis Generator.
45 | * This method sets the value of the database column comment.id
46 | *
47 | * @param id the value for comment.id
48 | *
49 | * @mbg.generated
50 | */
51 | public void setId(Integer id) {
52 | this.id = id;
53 | }
54 |
55 | /**
56 | * This method was generated by MyBatis Generator.
57 | * This method returns the value of the database column comment.post_id
58 | *
59 | * @return the value of comment.post_id
60 | *
61 | * @mbg.generated
62 | */
63 | public Integer getPostId() {
64 | return postId;
65 | }
66 |
67 | /**
68 | * This method was generated by MyBatis Generator.
69 | * This method sets the value of the database column comment.post_id
70 | *
71 | * @param postId the value for comment.post_id
72 | *
73 | * @mbg.generated
74 | */
75 | public void setPostId(Integer postId) {
76 | this.postId = postId;
77 | }
78 |
79 | /**
80 | * This method was generated by MyBatis Generator.
81 | * This method returns the value of the database column comment.content
82 | *
83 | * @return the value of comment.content
84 | *
85 | * @mbg.generated
86 | */
87 | public String getContent() {
88 | return content;
89 | }
90 |
91 | /**
92 | * This method was generated by MyBatis Generator.
93 | * This method sets the value of the database column comment.content
94 | *
95 | * @param content the value for comment.content
96 | *
97 | * @mbg.generated
98 | */
99 | public void setContent(String content) {
100 | this.content = content == null ? null : content.trim();
101 | }
102 | }
--------------------------------------------------------------------------------
/helloMybatis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | helloMybatis
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.12
16 | test
17 |
18 |
19 |
20 | mysql
21 | mysql-connector-java
22 | 5.1.45
23 |
24 |
25 |
26 |
27 | org.mybatis
28 | mybatis
29 | 3.4.5
30 |
31 |
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 | 1.7.25
37 |
38 |
39 | org.slf4j
40 | slf4j-log4j12
41 | 1.7.25
42 |
43 |
44 | org.apache.logging.log4j
45 | log4j-core
46 | 2.10.0
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-compiler-plugin
56 | 2.3.2
57 |
58 | 1.8
59 | 1.8
60 | UTF-8
61 |
62 |
63 |
64 |
65 |
66 | src/main/java
67 |
68 | **/*.properties
69 | **/*.xml
70 |
71 | false
72 |
73 |
74 | resources
75 |
76 | **/*.properties
77 | **/*.xml
78 |
79 | false
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/mybatis-cache/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-cache
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.12
16 | test
17 |
18 |
19 |
20 | mysql
21 | mysql-connector-java
22 | 5.1.45
23 |
24 |
25 |
26 |
27 | org.mybatis
28 | mybatis
29 | 3.4.5
30 |
31 |
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 | 1.7.25
37 |
38 |
39 | org.slf4j
40 | slf4j-log4j12
41 | 1.7.25
42 |
43 |
44 | org.apache.logging.log4j
45 | log4j-core
46 | 2.10.0
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | org.apache.maven.plugins
55 | maven-compiler-plugin
56 | 2.3.2
57 |
58 | 1.8
59 | 1.8
60 | UTF-8
61 |
62 |
63 |
64 |
65 |
66 | src/main/java
67 |
68 | **/*.properties
69 | **/*.xml
70 |
71 | false
72 |
73 |
74 | resources
75 |
76 | **/*.properties
77 | **/*.xml
78 |
79 | false
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/src/main/java/com/homejim/myabtis/GenericTokenParser.java:
--------------------------------------------------------------------------------
1 | package com.homejim.myabtis;
2 | /**
3 | * @author Clinton Begin
4 | */
5 | public class GenericTokenParser {
6 |
7 | private final String openToken;
8 | private final String closeToken;
9 | private final TokenHandler handler;
10 |
11 | public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
12 | this.openToken = openToken;
13 | this.closeToken = closeToken;
14 | this.handler = handler;
15 | }
16 |
17 | public String parse(String text) {
18 | if (text == null || text.isEmpty()) {
19 | return "";
20 | }
21 | // search open token
22 | int start = text.indexOf(openToken, 0);
23 | if (start == -1) {
24 | return text;
25 | }
26 | char[] src = text.toCharArray();
27 | int offset = 0;
28 | final StringBuilder builder = new StringBuilder();
29 | StringBuilder expression = null;
30 | while (start > -1) {
31 | if (start > 0 && src[start - 1] == '\\') {
32 | // this open token is escaped. remove the backslash and continue.
33 | builder.append(src, offset, start - offset - 1).append(openToken);
34 | offset = start + openToken.length();
35 | } else {
36 | // found open token. let's search close token.
37 | if (expression == null) {
38 | expression = new StringBuilder();
39 | } else {
40 | expression.setLength(0);
41 | }
42 | builder.append(src, offset, start - offset);
43 | offset = start + openToken.length();
44 | int end = text.indexOf(closeToken, offset);
45 | while (end > -1) {
46 | if (end > offset && src[end - 1] == '\\') {
47 | // this close token is escaped. remove the backslash and continue.
48 | expression.append(src, offset, end - offset - 1).append(closeToken);
49 | offset = end + closeToken.length();
50 | end = text.indexOf(closeToken, offset);
51 | } else {
52 | expression.append(src, offset, end - offset);
53 | offset = end + closeToken.length();
54 | break;
55 | }
56 | }
57 | if (end == -1) {
58 | // close token was not found.
59 | builder.append(src, start, src.length - start);
60 | offset = src.length;
61 | } else {
62 | builder.append(handler.handleToken(expression.toString()));
63 | offset = end + closeToken.length();
64 | }
65 | }
66 | start = text.indexOf(openToken, offset);
67 | }
68 | if (offset < src.length) {
69 | builder.append(src, offset, src.length - offset);
70 | }
71 | return builder.toString();
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/mybatis-dynamic/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-dynamic
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.12
16 | test
17 |
18 |
19 |
20 | mysql
21 | mysql-connector-java
22 | 5.1.45
23 |
24 |
25 |
26 |
27 | org.mybatis
28 | mybatis
29 | 3.4.5
30 |
31 |
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 | 1.7.25
37 |
38 |
39 | org.slf4j
40 | slf4j-log4j12
41 | 1.7.25
42 |
43 |
44 | org.apache.logging.log4j
45 | log4j-core
46 | 2.10.0
47 |
48 |
49 |
50 | org.apache.commons
51 | commons-lang3
52 | 3.7
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 | org.apache.maven.plugins
61 | maven-compiler-plugin
62 | 2.3.2
63 |
64 | 1.8
65 | 1.8
66 | UTF-8
67 |
68 |
69 |
70 |
71 |
72 | src/main/java
73 |
74 | **/*.properties
75 | **/*.xml
76 |
77 | false
78 |
79 |
80 | resources
81 |
82 | **/*.properties
83 | **/*.xml
84 |
85 | false
86 |
87 |
88 |
89 |
90 |
--------------------------------------------------------------------------------
/mybatis-mapping/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-mapping
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 |
14 | junit
15 | junit
16 | 4.12
17 | test
18 |
19 |
20 |
21 | mysql
22 | mysql-connector-java
23 | 5.1.45
24 |
25 |
26 |
27 |
28 | org.mybatis
29 | mybatis
30 | 3.4.5
31 |
32 |
33 |
34 |
35 | org.slf4j
36 | slf4j-api
37 | 1.7.25
38 |
39 |
40 | org.slf4j
41 | slf4j-log4j12
42 | 1.7.25
43 |
44 |
45 | org.apache.logging.log4j
46 | log4j-core
47 | 2.10.0
48 |
49 |
50 |
51 | org.apache.commons
52 | commons-lang3
53 | 3.8.1
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.apache.maven.plugins
62 | maven-compiler-plugin
63 | 2.3.2
64 |
65 | 1.8
66 | 1.8
67 | UTF-8
68 |
69 |
70 |
71 |
72 |
73 | src/main/java
74 |
75 | **/*.properties
76 | **/*.xml
77 |
78 | false
79 |
80 |
81 | resources
82 |
83 | **/*.properties
84 | **/*.xml
85 |
86 | false
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/mybatis-plugin/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.homejim.mybatis
8 | mybatis-plugin
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.12
16 | test
17 |
18 |
19 |
20 | mysql
21 | mysql-connector-java
22 | 5.1.45
23 |
24 |
25 |
26 |
27 | org.mybatis
28 | mybatis
29 | 3.4.5
30 |
31 |
32 |
33 |
34 | org.slf4j
35 | slf4j-api
36 | 1.7.25
37 |
38 |
39 | org.slf4j
40 | slf4j-log4j12
41 | 1.7.25
42 |
43 |
44 | org.apache.logging.log4j
45 | log4j-core
46 | 2.10.0
47 |
48 |
49 |
50 | org.projectlombok
51 | lombok
52 | 1.18.8
53 |
54 |
55 |
56 | com.google.guava
57 | guava
58 | 20.0
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | org.apache.maven.plugins
67 | maven-compiler-plugin
68 | 2.3.2
69 |
70 | 1.8
71 | 1.8
72 | UTF-8
73 |
74 |
75 |
76 |
77 |
78 | src/main/java
79 |
80 | **/*.properties
81 | **/*.xml
82 |
83 | false
84 |
85 |
86 | resources
87 |
88 | **/*.properties
89 | **/*.xml
90 |
91 | false
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/Post.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class Post {
4 | /**
5 | *
6 | * This field was generated by MyBatis Generator.
7 | * This field corresponds to the database column post.id
8 | *
9 | * @mbg.generated
10 | */
11 | private Integer id;
12 |
13 | /**
14 | *
15 | * This field was generated by MyBatis Generator.
16 | * This field corresponds to the database column post.blog_id
17 | *
18 | * @mbg.generated
19 | */
20 | private Integer blogId;
21 |
22 | /**
23 | *
24 | * This field was generated by MyBatis Generator.
25 | * This field corresponds to the database column post.draft
26 | *
27 | * @mbg.generated
28 | */
29 | private Integer draft;
30 |
31 | /**
32 | *
33 | * This field was generated by MyBatis Generator.
34 | * This field corresponds to the database column post.content
35 | *
36 | * @mbg.generated
37 | */
38 | private String content;
39 |
40 | /**
41 | * This method was generated by MyBatis Generator.
42 | * This method returns the value of the database column post.id
43 | *
44 | * @return the value of post.id
45 | *
46 | * @mbg.generated
47 | */
48 | public Integer getId() {
49 | return id;
50 | }
51 |
52 | /**
53 | * This method was generated by MyBatis Generator.
54 | * This method sets the value of the database column post.id
55 | *
56 | * @param id the value for post.id
57 | *
58 | * @mbg.generated
59 | */
60 | public void setId(Integer id) {
61 | this.id = id;
62 | }
63 |
64 | /**
65 | * This method was generated by MyBatis Generator.
66 | * This method returns the value of the database column post.blog_id
67 | *
68 | * @return the value of post.blog_id
69 | *
70 | * @mbg.generated
71 | */
72 | public Integer getBlogId() {
73 | return blogId;
74 | }
75 |
76 | /**
77 | * This method was generated by MyBatis Generator.
78 | * This method sets the value of the database column post.blog_id
79 | *
80 | * @param blogId the value for post.blog_id
81 | *
82 | * @mbg.generated
83 | */
84 | public void setBlogId(Integer blogId) {
85 | this.blogId = blogId;
86 | }
87 |
88 | /**
89 | * This method was generated by MyBatis Generator.
90 | * This method returns the value of the database column post.draft
91 | *
92 | * @return the value of post.draft
93 | *
94 | * @mbg.generated
95 | */
96 | public Integer getDraft() {
97 | return draft;
98 | }
99 |
100 | /**
101 | * This method was generated by MyBatis Generator.
102 | * This method sets the value of the database column post.draft
103 | *
104 | * @param draft the value for post.draft
105 | *
106 | * @mbg.generated
107 | */
108 | public void setDraft(Integer draft) {
109 | this.draft = draft;
110 | }
111 |
112 | /**
113 | * This method was generated by MyBatis Generator.
114 | * This method returns the value of the database column post.content
115 | *
116 | * @return the value of post.content
117 | *
118 | * @mbg.generated
119 | */
120 | public String getContent() {
121 | return content;
122 | }
123 |
124 | /**
125 | * This method was generated by MyBatis Generator.
126 | * This method sets the value of the database column post.content
127 | *
128 | * @param content the value for post.content
129 | *
130 | * @mbg.generated
131 | */
132 | public void setContent(String content) {
133 | this.content = content == null ? null : content.trim();
134 | }
135 | }
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/entity/Author.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.entity;
2 |
3 | public class Author {
4 | /**
5 | *
6 | * This field was generated by MyBatis Generator.
7 | * This field corresponds to the database column author.id
8 | *
9 | * @mbg.generated
10 | */
11 | private Integer id;
12 |
13 | /**
14 | *
15 | * This field was generated by MyBatis Generator.
16 | * This field corresponds to the database column author.username
17 | *
18 | * @mbg.generated
19 | */
20 | private String username;
21 |
22 | /**
23 | *
24 | * This field was generated by MyBatis Generator.
25 | * This field corresponds to the database column author.password
26 | *
27 | * @mbg.generated
28 | */
29 | private String password;
30 |
31 | /**
32 | *
33 | * This field was generated by MyBatis Generator.
34 | * This field corresponds to the database column author.email
35 | *
36 | * @mbg.generated
37 | */
38 | private String email;
39 |
40 | /**
41 | * This method was generated by MyBatis Generator.
42 | * This method returns the value of the database column author.id
43 | *
44 | * @return the value of author.id
45 | *
46 | * @mbg.generated
47 | */
48 | public Integer getId() {
49 | return id;
50 | }
51 |
52 | /**
53 | * This method was generated by MyBatis Generator.
54 | * This method sets the value of the database column author.id
55 | *
56 | * @param id the value for author.id
57 | *
58 | * @mbg.generated
59 | */
60 | public void setId(Integer id) {
61 | this.id = id;
62 | }
63 |
64 | /**
65 | * This method was generated by MyBatis Generator.
66 | * This method returns the value of the database column author.username
67 | *
68 | * @return the value of author.username
69 | *
70 | * @mbg.generated
71 | */
72 | public String getUsername() {
73 | return username;
74 | }
75 |
76 | /**
77 | * This method was generated by MyBatis Generator.
78 | * This method sets the value of the database column author.username
79 | *
80 | * @param username the value for author.username
81 | *
82 | * @mbg.generated
83 | */
84 | public void setUsername(String username) {
85 | this.username = username == null ? null : username.trim();
86 | }
87 |
88 | /**
89 | * This method was generated by MyBatis Generator.
90 | * This method returns the value of the database column author.password
91 | *
92 | * @return the value of author.password
93 | *
94 | * @mbg.generated
95 | */
96 | public String getPassword() {
97 | return password;
98 | }
99 |
100 | /**
101 | * This method was generated by MyBatis Generator.
102 | * This method sets the value of the database column author.password
103 | *
104 | * @param password the value for author.password
105 | *
106 | * @mbg.generated
107 | */
108 | public void setPassword(String password) {
109 | this.password = password == null ? null : password.trim();
110 | }
111 |
112 | /**
113 | * This method was generated by MyBatis Generator.
114 | * This method returns the value of the database column author.email
115 | *
116 | * @return the value of author.email
117 | *
118 | * @mbg.generated
119 | */
120 | public String getEmail() {
121 | return email;
122 | }
123 |
124 | /**
125 | * This method was generated by MyBatis Generator.
126 | * This method sets the value of the database column author.email
127 | *
128 | * @param email the value for author.email
129 | *
130 | * @mbg.generated
131 | */
132 | public void setEmail(String email) {
133 | this.email = email == null ? null : email.trim();
134 | }
135 | }
--------------------------------------------------------------------------------
/helloMybatis/src/test/java/com/homejim/mybatis/mapper/StudentMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import org.apache.ibatis.io.Resources;
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8 | import org.junit.BeforeClass;
9 | import org.junit.Test;
10 |
11 | import java.io.IOException;
12 | import java.io.Reader;
13 | import java.util.Calendar;
14 | import java.util.HashMap;
15 | import java.util.List;
16 | import java.util.Map;
17 |
18 | /**
19 | * Created by IntelliJ IDEA
20 | * User: Homejim
21 | * Date 2018-9-4
22 | * Time 20:40
23 | */
24 | public class StudentMapperTest {
25 | private static SqlSessionFactory sqlSessionFactory;
26 |
27 | @BeforeClass
28 | public static void init() {
29 | try {
30 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
31 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
32 | } catch (IOException e) {
33 | e.printStackTrace();
34 | }
35 | }
36 |
37 | @Test
38 | public void testSelectList() {
39 | SqlSession sqlSession = null;
40 | try {
41 | sqlSession = sqlSessionFactory.openSession();
42 |
43 | List students = sqlSession.selectList("selectAll");
44 | for (int i = 0; i < students.size(); i++) {
45 | System.out.println(students.get(i));
46 | }
47 | } catch (Exception e) {
48 | e.printStackTrace();
49 | } finally {
50 | if (sqlSession != null) {
51 | sqlSession.close();
52 | }
53 | }
54 | }
55 |
56 | @Test
57 | public void testSelectBtweenCreatedTimeMap() {
58 |
59 | Map params = new HashMap<>();
60 | Calendar bTime = Calendar.getInstance();
61 | // month 是从0~11, 所以9月是8
62 | bTime.set(2018, Calendar.AUGUST, 29);
63 | params.put("bTime", bTime.getTime());
64 |
65 | Calendar eTime = Calendar.getInstance();
66 | eTime.set(2018,Calendar.SEPTEMBER,2);
67 | params.put("eTime", eTime.getTime());
68 | SqlSession sqlSession = null;
69 | try {
70 | sqlSession = sqlSessionFactory.openSession();
71 |
72 | StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
73 | List students = studentMapper.selectBetweenCreatedTime(params);
74 | for (int i = 0; i < students.size(); i++) {
75 | System.out.println(students.get(i));
76 | }
77 | } catch (Exception e) {
78 | e.printStackTrace();
79 | } finally {
80 | if (sqlSession != null) {
81 | sqlSession.close();
82 | }
83 | }
84 |
85 | }
86 |
87 | @Test
88 | public void testSelectBtweenCreatedTimeAnno() {
89 |
90 | Map params = new HashMap<>();
91 | Calendar bTime = Calendar.getInstance();
92 | // month 是从0~11, 所以9月是8
93 | bTime.set(2018, Calendar.AUGUST, 29);
94 |
95 |
96 | Calendar eTime = Calendar.getInstance();
97 | eTime.set(2018,Calendar.SEPTEMBER,2);
98 |
99 | SqlSession sqlSession = null;
100 | try {
101 | sqlSession = sqlSessionFactory.openSession();
102 |
103 | StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
104 | List students = studentMapper.selectBetweenCreatedTimeAnno(bTime.getTime(), eTime.getTime());
105 | for (int i = 0; i < students.size(); i++) {
106 | System.out.println(students.get(i));
107 | }
108 | } catch (Exception e) {
109 | e.printStackTrace();
110 | } finally {
111 | if (sqlSession != null) {
112 | sqlSession.close();
113 | }
114 | }
115 |
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/test/java/com/homejim/mybatis/mapper/StudentMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import com.homejim.mybatis.plugin.PageUtil;
5 | import org.apache.ibatis.io.Resources;
6 | import org.apache.ibatis.session.SqlSession;
7 | import org.apache.ibatis.session.SqlSessionFactory;
8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9 | import org.junit.BeforeClass;
10 | import org.junit.Test;
11 |
12 | import java.io.IOException;
13 | import java.io.Reader;
14 | import java.util.Calendar;
15 | import java.util.HashMap;
16 | import java.util.List;
17 | import java.util.Map;
18 |
19 | /**
20 | * Created by IntelliJ IDEA
21 | * User: Homejim
22 | * Date 2018-9-4
23 | * Time 20:40
24 | */
25 | public class StudentMapperTest {
26 | private static SqlSessionFactory sqlSessionFactory;
27 |
28 | @BeforeClass
29 | public static void init() {
30 | try {
31 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
32 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
33 | } catch (IOException e) {
34 | e.printStackTrace();
35 | }
36 | }
37 |
38 | @Test
39 | public void testSelectList() {
40 | SqlSession sqlSession = null;
41 | try {
42 | sqlSession = sqlSessionFactory.openSession();
43 | PageUtil.setPagingParam(1, 2);
44 | List students = sqlSession.selectList("selectAll");
45 | for (int i = 0; i < students.size(); i++) {
46 | System.out.println(students.get(i));
47 | }
48 |
49 | List students2 = sqlSession.selectList("selectAll");
50 | for (int i = 0; i < students2.size(); i++) {
51 | System.out.println(students2.get(i));
52 | }
53 | } catch (Exception e) {
54 | e.printStackTrace();
55 | } finally {
56 | if (sqlSession != null) {
57 | sqlSession.close();
58 | }
59 | }
60 | }
61 |
62 | @Test
63 | public void testSelectBtweenCreatedTimeMap() {
64 |
65 | Map params = new HashMap<>();
66 | Calendar bTime = Calendar.getInstance();
67 | // month 是从0~11, 所以9月是8
68 | bTime.set(2018, Calendar.AUGUST, 29);
69 | params.put("bTime", bTime.getTime());
70 |
71 | Calendar eTime = Calendar.getInstance();
72 | eTime.set(2018,Calendar.SEPTEMBER,2);
73 | params.put("eTime", eTime.getTime());
74 | SqlSession sqlSession = null;
75 | try {
76 | sqlSession = sqlSessionFactory.openSession();
77 |
78 | StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
79 | List students = studentMapper.selectBetweenCreatedTime(params);
80 | for (int i = 0; i < students.size(); i++) {
81 | System.out.println(students.get(i));
82 | }
83 | } catch (Exception e) {
84 | e.printStackTrace();
85 | } finally {
86 | if (sqlSession != null) {
87 | sqlSession.close();
88 | }
89 | }
90 |
91 | }
92 |
93 | @Test
94 | public void testSelectBtweenCreatedTimeAnno() {
95 |
96 | Map params = new HashMap<>();
97 | Calendar bTime = Calendar.getInstance();
98 | // month 是从0~11, 所以9月是8
99 | bTime.set(2018, Calendar.AUGUST, 29);
100 |
101 |
102 | Calendar eTime = Calendar.getInstance();
103 | eTime.set(2018,Calendar.SEPTEMBER,2);
104 |
105 | SqlSession sqlSession = null;
106 | try {
107 | sqlSession = sqlSessionFactory.openSession();
108 |
109 | StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
110 | List students = studentMapper.selectBetweenCreatedTimeAnno(bTime.getTime(), eTime.getTime());
111 | for (int i = 0; i < students.size(); i++) {
112 | System.out.println(students.get(i));
113 | }
114 | } catch (Exception e) {
115 | e.printStackTrace();
116 | } finally {
117 | if (sqlSession != null) {
118 | sqlSession.close();
119 | }
120 | }
121 |
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/AuthorMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 | id, username, `password`, email
20 |
21 |
31 |
37 |
38 |
42 | delete from author
43 | where id = #{id,jdbcType=INTEGER}
44 |
45 |
46 |
50 | insert into author (id, username, `password`,
51 | email)
52 | values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
53 | #{email,jdbcType=VARCHAR})
54 |
55 |
56 |
60 | insert into author
61 |
62 |
63 | id,
64 |
65 |
66 | username,
67 |
68 |
69 | `password`,
70 |
71 |
72 | email,
73 |
74 |
75 |
76 |
77 | #{id,jdbcType=INTEGER},
78 |
79 |
80 | #{username,jdbcType=VARCHAR},
81 |
82 |
83 | #{password,jdbcType=VARCHAR},
84 |
85 |
86 | #{email,jdbcType=VARCHAR},
87 |
88 |
89 |
90 |
91 |
95 | update author
96 |
97 |
98 | username = #{username,jdbcType=VARCHAR},
99 |
100 |
101 | `password` = #{password,jdbcType=VARCHAR},
102 |
103 |
104 | email = #{email,jdbcType=VARCHAR},
105 |
106 |
107 | where id = #{id,jdbcType=INTEGER}
108 |
109 |
110 |
114 | update author
115 | set username = #{username,jdbcType=VARCHAR},
116 | `password` = #{password,jdbcType=VARCHAR},
117 | email = #{email,jdbcType=VARCHAR}
118 | where id = #{id,jdbcType=INTEGER}
119 |
120 |
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/com/homejim/mybatis/mapper/AuthorMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
19 | id, username, `password`, email
20 |
21 |
31 |
37 |
38 |
42 | delete from author
43 | where id = #{id,jdbcType=INTEGER}
44 |
45 |
46 |
50 | insert into author (id, username, `password`,
51 | email)
52 | values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
53 | #{email,jdbcType=VARCHAR})
54 |
55 |
56 |
60 | insert into author
61 |
62 |
63 | id,
64 |
65 |
66 | username,
67 |
68 |
69 | `password`,
70 |
71 |
72 | email,
73 |
74 |
75 |
76 |
77 | #{id,jdbcType=INTEGER},
78 |
79 |
80 | #{username,jdbcType=VARCHAR},
81 |
82 |
83 | #{password,jdbcType=VARCHAR},
84 |
85 |
86 | #{email,jdbcType=VARCHAR},
87 |
88 |
89 |
90 |
91 |
95 | update author
96 |
97 |
98 | username = #{username,jdbcType=VARCHAR},
99 |
100 |
101 | `password` = #{password,jdbcType=VARCHAR},
102 |
103 |
104 | email = #{email,jdbcType=VARCHAR},
105 |
106 |
107 | where id = #{id,jdbcType=INTEGER}
108 |
109 |
110 |
114 | update author
115 | set username = #{username,jdbcType=VARCHAR},
116 | `password` = #{password,jdbcType=VARCHAR},
117 | email = #{email,jdbcType=VARCHAR}
118 | where id = #{id,jdbcType=INTEGER}
119 |
120 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/CommentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
17 |
18 |
19 |
20 |
24 | id, post_id
25 |
26 |
27 |
31 | content
32 |
33 |
45 |
46 |
50 | delete from comment
51 | where id = #{id,jdbcType=INTEGER}
52 |
53 |
54 |
58 | insert into comment (id, post_id, content
59 | )
60 | values (#{id,jdbcType=INTEGER}, #{postId,jdbcType=INTEGER}, #{content,jdbcType=LONGVARCHAR}
61 | )
62 |
63 |
64 |
68 | insert into comment
69 |
70 |
71 | id,
72 |
73 |
74 | post_id,
75 |
76 |
77 | content,
78 |
79 |
80 |
81 |
82 | #{id,jdbcType=INTEGER},
83 |
84 |
85 | #{postId,jdbcType=INTEGER},
86 |
87 |
88 | #{content,jdbcType=LONGVARCHAR},
89 |
90 |
91 |
92 |
93 |
97 | update comment
98 |
99 |
100 | post_id = #{postId,jdbcType=INTEGER},
101 |
102 |
103 | content = #{content,jdbcType=LONGVARCHAR},
104 |
105 |
106 | where id = #{id,jdbcType=INTEGER}
107 |
108 |
109 |
113 | update comment
114 | set post_id = #{postId,jdbcType=INTEGER},
115 | content = #{content,jdbcType=LONGVARCHAR}
116 | where id = #{id,jdbcType=INTEGER}
117 |
118 |
119 |
123 | update comment
124 | set post_id = #{postId,jdbcType=INTEGER}
125 | where id = #{id,jdbcType=INTEGER}
126 |
127 |
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/com/homejim/mybatis/mapper/CommentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
17 |
18 |
19 |
20 |
24 | id, post_id
25 |
26 |
27 |
31 | content
32 |
33 |
45 |
46 |
50 | delete from comment
51 | where id = #{id,jdbcType=INTEGER}
52 |
53 |
54 |
58 | insert into comment (id, post_id, content
59 | )
60 | values (#{id,jdbcType=INTEGER}, #{postId,jdbcType=INTEGER}, #{content,jdbcType=LONGVARCHAR}
61 | )
62 |
63 |
64 |
68 | insert into comment
69 |
70 |
71 | id,
72 |
73 |
74 | post_id,
75 |
76 |
77 | content,
78 |
79 |
80 |
81 |
82 | #{id,jdbcType=INTEGER},
83 |
84 |
85 | #{postId,jdbcType=INTEGER},
86 |
87 |
88 | #{content,jdbcType=LONGVARCHAR},
89 |
90 |
91 |
92 |
93 |
97 | update comment
98 |
99 |
100 | post_id = #{postId,jdbcType=INTEGER},
101 |
102 |
103 | content = #{content,jdbcType=LONGVARCHAR},
104 |
105 |
106 | where id = #{id,jdbcType=INTEGER}
107 |
108 |
109 |
113 | update comment
114 | set post_id = #{postId,jdbcType=INTEGER},
115 | content = #{content,jdbcType=LONGVARCHAR}
116 | where id = #{id,jdbcType=INTEGER}
117 |
118 |
119 |
123 | update comment
124 | set post_id = #{postId,jdbcType=INTEGER}
125 | where id = #{id,jdbcType=INTEGER}
126 |
127 |
--------------------------------------------------------------------------------
/mybatis-plugin/src/main/java/com/homejim/mybatis/plugin/PageInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.plugin;
2 |
3 | import com.google.common.base.Joiner;
4 | import lombok.extern.slf4j.Slf4j;
5 | import org.apache.ibatis.executor.Executor;
6 | import org.apache.ibatis.mapping.BoundSql;
7 | import org.apache.ibatis.mapping.MappedStatement;
8 | import org.apache.ibatis.plugin.Interceptor;
9 | import org.apache.ibatis.plugin.Intercepts;
10 | import org.apache.ibatis.plugin.Invocation;
11 | import org.apache.ibatis.plugin.Plugin;
12 | import org.apache.ibatis.plugin.Signature;
13 | import org.apache.ibatis.session.ResultHandler;
14 | import org.apache.ibatis.session.RowBounds;
15 |
16 | import java.util.Properties;
17 |
18 | /**
19 | * 分页插件
20 | *
21 | * @author homejim
22 | */
23 | @Intercepts({
24 | @Signature(
25 | type = Executor.class,
26 | method = "query",
27 | args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
28 | )
29 | })
30 | @Slf4j
31 | public class PageInterceptor implements Interceptor {
32 |
33 | private static int MAPPEDSTATEMENT_INDEX = 0;
34 |
35 | private static int PARAMETER_INDEX = 1;
36 |
37 | private static int ROWBOUNDS_INDEX = 2;
38 |
39 | @Override
40 | public Object intercept(Invocation invocation) throws Throwable {
41 |
42 | // 从 Invocation 中获取参数
43 | final Object[] queryArgs = invocation.getArgs();
44 | final MappedStatement ms = (MappedStatement) queryArgs[MAPPEDSTATEMENT_INDEX];
45 | final Object parameter = queryArgs[PARAMETER_INDEX];
46 |
47 | // 获取分页参数
48 | Page paingParam = PageUtil.getPaingParam();
49 | if (paingParam != null) {
50 |
51 | // 构造新的 sql, select xxx from xxx where yyy limit offset,limit
52 | final BoundSql boundSql = ms.getBoundSql(parameter);
53 | String pagingSql = getPagingSql(boundSql.getSql(), paingParam.getOffset(), paingParam.getLimit());
54 |
55 | // 设置新的 MappedStatement
56 | BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), pagingSql,
57 | boundSql.getParameterMappings(), boundSql.getParameterObject());
58 | MappedStatement mappedStatement = newMappedStatement(ms, newBoundSql);
59 | queryArgs[MAPPEDSTATEMENT_INDEX] = mappedStatement;
60 |
61 | // 重置 RowBound
62 | queryArgs[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
63 | }
64 | Object result = invocation.proceed();
65 | PageUtil.removePagingParam();
66 | return result;
67 | }
68 |
69 | @Override
70 | public Object plugin(Object o) {
71 | // 使用 mybatis 提供的帮助类进行代理类的封装
72 | return Plugin.wrap(o, this);
73 | }
74 |
75 | @Override
76 | public void setProperties(Properties properties) {
77 |
78 | }
79 |
80 | /**
81 | * 创建 MappedStatement
82 | * @param ms
83 | * @param newBoundSql
84 | * @return
85 | */
86 | private MappedStatement newMappedStatement(MappedStatement ms, BoundSql newBoundSql) {
87 | MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(),
88 | new BoundSqlSqlSource(newBoundSql), ms.getSqlCommandType());
89 | builder.keyColumn(delimitedArrayToString(ms.getKeyColumns()));
90 | builder.keyGenerator(ms.getKeyGenerator());
91 | builder.keyProperty(delimitedArrayToString(ms.getKeyProperties()));
92 | builder.lang(ms.getLang());
93 | builder.resource(ms.getResource());
94 | builder.parameterMap(ms.getParameterMap());
95 | builder.resultMaps(ms.getResultMaps());
96 | builder.resultOrdered(ms.isResultOrdered());
97 | builder.resultSets(delimitedArrayToString(ms.getResultSets()));
98 | builder.resultSetType(ms.getResultSetType());
99 | builder.timeout(ms.getTimeout());
100 | builder.statementType(ms.getStatementType());
101 | builder.useCache(ms.isUseCache());
102 | builder.cache(ms.getCache());
103 | builder.databaseId(ms.getDatabaseId());
104 | builder.fetchSize(ms.getFetchSize());
105 | builder.flushCacheRequired(ms.isFlushCacheRequired());
106 | return builder.build();
107 | }
108 |
109 | public String getPagingSql(String sql, int offset, int limit) {
110 | StringBuilder result = new StringBuilder(sql.length() + 100);
111 | result.append(sql).append(" limit ");
112 |
113 | if (offset > 0) {
114 | result.append(offset).append(",").append(limit);
115 | }else{
116 | result.append(limit);
117 | }
118 | return result.toString();
119 | }
120 |
121 | public String delimitedArrayToString(String[] array) {
122 |
123 | if (array == null || array.length == 0) {
124 | return "";
125 | }
126 | Joiner joiner = Joiner.on(",");
127 | return joiner.join(array);
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/mybatisGenericTokenParser/src/test/java/com/homejim/mybatis/GenericTokenParserTest.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis;
2 |
3 |
4 | import com.homejim.myabtis.GenericTokenParser;
5 | import com.homejim.myabtis.TokenHandler;
6 | import org.junit.Ignore;
7 | import org.junit.Test;
8 |
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | import static org.junit.Assert.assertEquals;
13 |
14 | public class GenericTokenParserTest {
15 |
16 | public static class VariableTokenHandler implements TokenHandler {
17 | private Map variables;
18 |
19 | public VariableTokenHandler(Map variables) {
20 | this.variables = variables;
21 | }
22 |
23 | @Override
24 | public String handleToken(String content) {
25 | return variables.get(content);
26 | }
27 | }
28 |
29 | @Test
30 | public void simpleTest() {
31 | GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap() {
32 | {
33 | put("driver", "com.mysql.jdbc.Driver");
34 | put("url", "jdbc:mysql://localhost:3306/mybatis");
35 | put("username", "root");
36 | put("password", "aaabbb");
37 |
38 | }
39 | }));
40 |
41 | // 测试单个解析
42 | assertEquals("com.mysql.jdbc.Driver", parser.parse("${driver}"));
43 | // 多个一起测试
44 | assertEquals("驱动=com.mysql.jdbc.Driver,地址=jdbc:mysql://localhost:3306/mybatis,用户名=root",
45 | parser.parse("驱动=${driver},地址=${url},用户名=${username}"));
46 | }
47 | @Test
48 | public void shouldDemonstrateGenericTokenReplacement() {
49 | GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap() {
50 | {
51 | put("first_name", "James");
52 | put("initial", "T");
53 | put("last_name", "Kirk");
54 | put("var{with}brace", "Hiya");
55 | put("", "");
56 | }
57 | }));
58 |
59 | assertEquals("James T Kirk reporting.", parser.parse("${first_name} ${initial} ${last_name} reporting."));
60 | assertEquals("Hello captain James T Kirk", parser.parse("Hello captain ${first_name} ${initial} ${last_name}"));
61 | assertEquals("James T Kirk", parser.parse("${first_name} ${initial} ${last_name}"));
62 | assertEquals("JamesTKirk", parser.parse("${first_name}${initial}${last_name}"));
63 | assertEquals("{}JamesTKirk", parser.parse("{}${first_name}${initial}${last_name}"));
64 | assertEquals("}JamesTKirk", parser.parse("}${first_name}${initial}${last_name}"));
65 |
66 | assertEquals("}James{{T}}Kirk", parser.parse("}${first_name}{{${initial}}}${last_name}"));
67 | assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
68 | assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
69 | assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}"));
70 | assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}${}"));
71 |
72 | assertEquals("{$$something}JamesTKirk", parser.parse("{$$something}${first_name}${initial}${last_name}"));
73 | assertEquals("${", parser.parse("${"));
74 | assertEquals("${\\}", parser.parse("${\\}"));
75 | assertEquals("Hiya", parser.parse("${var{with\\}brace}"));
76 | assertEquals("", parser.parse("${}"));
77 | assertEquals("}", parser.parse("}"));
78 | assertEquals("Hello ${ this is a test.", parser.parse("Hello ${ this is a test."));
79 | assertEquals("Hello } this is a test.", parser.parse("Hello } this is a test."));
80 | assertEquals("Hello } ${ this is a test.", parser.parse("Hello } ${ this is a test."));
81 | }
82 | @Test
83 | public void shallNotInterpolateSkippedVaiables() {
84 | GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap()));
85 |
86 | assertEquals("${skipped} variable", parser.parse("\\${skipped} variable"));
87 | assertEquals("This is a ${skipped} variable", parser.parse("This is a \\${skipped} variable"));
88 | assertEquals("null ${skipped} variable", parser.parse("${skipped} \\${skipped} variable"));
89 | assertEquals("The null is ${skipped} variable", parser.parse("The ${skipped} is \\${skipped} variable"));
90 | }
91 |
92 | @Ignore("Because it randomly fails on Travis CI. It could be useful during development.")
93 | @Test(timeout = 1000)
94 | public void shouldParseFastOnJdk7u6() {
95 | // issue #760
96 | GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap() {
97 | {
98 | put("first_name", "James");
99 | put("initial", "T");
100 | put("last_name", "Kirk");
101 | put("", "");
102 | }
103 | }));
104 |
105 | StringBuilder input = new StringBuilder();
106 | for (int i = 0; i < 10000; i++) {
107 | input.append("${first_name} ${initial} ${last_name} reporting. ");
108 | }
109 | StringBuilder expected = new StringBuilder();
110 | for (int i = 0; i < 10000; i++) {
111 | expected.append("James T Kirk reporting. ");
112 | }
113 | assertEquals(expected.toString(), parser.parse(input.toString()));
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/resources/sql/mybatis-mapping.sql:
--------------------------------------------------------------------------------
1 | /*
2 | SQLyog Ultimate v12.08 (64 bit)
3 | MySQL - 8.0.11 : Database - mybatis
4 | *********************************************************************
5 | */
6 |
7 | /*!40101 SET NAMES utf8 */;
8 |
9 | /*!40101 SET SQL_MODE=''*/;
10 |
11 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
12 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
13 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
14 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
15 | CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;
16 |
17 | USE `mybatis`;
18 |
19 | /*Table structure for table `author` */
20 |
21 | DROP TABLE IF EXISTS `author`;
22 |
23 | CREATE TABLE `author` (
24 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '作者id',
25 | `username` varchar(20) DEFAULT NULL COMMENT '用户名',
26 | `password` varchar(20) DEFAULT NULL COMMENT '密码',
27 | `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
28 | PRIMARY KEY (`id`)
29 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='作者表';
30 |
31 | /*Data for the table `author` */
32 |
33 | insert into `author`(`id`,`username`,`password`,`email`) values (1,'小明','xiaoming123','xiaoming@163.com'),(2,'小王','xiaowang123','xiaowang@163.com'),(3,'大牛','daniu123','daniu@163.com'),(4,'黑子','heizi123','heizi@163.com');
34 |
35 | /*Table structure for table `blog` */
36 |
37 | DROP TABLE IF EXISTS `blog`;
38 |
39 | CREATE TABLE `blog` (
40 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '博客id',
41 | `title` varchar(20) DEFAULT NULL COMMENT '博客标题',
42 | `author_id` int(10) unsigned NOT NULL COMMENT '作者id',
43 | PRIMARY KEY (`id`),
44 | UNIQUE KEY `author_fk` (`author_id`),
45 | CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`)
46 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='博客表';
47 |
48 | /*Data for the table `blog` */
49 |
50 | insert into `blog`(`id`,`title`,`author_id`) values (1,'小明的博客',1),(2,'小王的博客',2),(3,'大牛的博客',3),(4,'黑子的博客',4);
51 |
52 | /*Table structure for table `comment` */
53 |
54 | DROP TABLE IF EXISTS `comment`;
55 |
56 | CREATE TABLE `comment` (
57 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '博客id',
58 | `post_id` int(10) unsigned NOT NULL COMMENT '博客标题',
59 | `content` text COMMENT '博客内容',
60 | PRIMARY KEY (`id`),
61 | KEY `post_fk` (`post_id`),
62 | CONSTRAINT `post_fk` FOREIGN KEY (`post_id`) REFERENCES `post` (`id`)
63 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='评论表';
64 |
65 | /*Data for the table `comment` */
66 |
67 | /*Table structure for table `post` */
68 |
69 | DROP TABLE IF EXISTS `post`;
70 |
71 | CREATE TABLE `post` (
72 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '博客id',
73 | `blog_id` int(10) unsigned NOT NULL COMMENT '博客标题',
74 | `content` text COMMENT '博客内容',
75 | `draft` int(11) NOT NULL COMMENT '草稿',
76 | PRIMARY KEY (`id`),
77 | KEY `blog_fk` (`blog_id`),
78 | CONSTRAINT `blog_fk` FOREIGN KEY (`blog_id`) REFERENCES `blog` (`id`)
79 | ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='发布表';
80 |
81 | /*Data for the table `post` */
82 |
83 | insert into `post`(`id`,`blog_id`,`content`,`draft`) values (1,1,'小明-第1篇测试啦啦啦',0),(2,1,'小明-第2篇测试啦啦啦',0),(3,1,'第3篇测试啦啦啦',0),(4,2,'小王-我是大王, 大家好',0),(5,2,'大牛-我是一只牛',0);
84 |
85 | /*Table structure for table `student` */
86 |
87 | DROP TABLE IF EXISTS `student`;
88 |
89 | CREATE TABLE `student` (
90 | `student_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
91 | `name` varchar(20) DEFAULT NULL COMMENT '姓名',
92 | `phone` varchar(20) DEFAULT NULL COMMENT '电话',
93 | `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
94 | `sex` tinyint(4) DEFAULT NULL COMMENT '性别',
95 | `locked` tinyint(4) DEFAULT NULL COMMENT '状态(0:正常,1:锁定)',
96 | `gmt_created` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '存入数据库的时间',
97 | `gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的时间',
98 | `delete` int(11) DEFAULT NULL,
99 | PRIMARY KEY (`student_id`)
100 | ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='学生表';
101 |
102 | /*Data for the table `student` */
103 |
104 | insert into `student`(`student_id`,`name`,`phone`,`email`,`sex`,`locked`,`gmt_created`,`gmt_modified`,`delete`) values (1,'小明','13821378270','xiaoming@mybatis.cn',1,0,'2018-08-29 18:27:42','2018-10-08 20:54:25',NULL),(2,'小丽','13821378271','xiaoli@mybatis.cn',0,0,'2018-08-30 18:27:42','2018-10-08 20:54:29',NULL),(3,'小刚','13821378272','xiaogang@mybatis.cn',1,0,'2018-08-31 18:27:42','2018-10-08 20:55:08',NULL),(4,'小花','13821378273','xiaohua@mybatis.cn',0,0,'2018-09-01 18:27:42','2018-10-08 20:55:12',NULL),(5,'小强','13821378274','xiaoqiang@mybatis.cn',1,0,'2018-09-02 18:27:42','2018-10-08 20:55:18',NULL),(6,'小红','13821378275','xiaohong@mybatis.cn',0,0,'2018-09-03 18:27:42','2018-10-08 20:55:27',NULL);
105 |
106 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
107 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
108 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
109 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
110 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/main/java/com/homejim/mybatis/mapper/PostMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
18 |
19 |
20 |
21 |
25 | id, blog_id, draft
26 |
27 |
28 |
32 | content
33 |
34 |
46 |
54 |
55 |
59 | delete from post
60 | where id = #{id,jdbcType=INTEGER}
61 |
62 |
63 |
67 | insert into post (id, blog_id, draft,
68 | content)
69 | values (#{id,jdbcType=INTEGER}, #{blogId,jdbcType=INTEGER}, #{draft,jdbcType=INTEGER},
70 | #{content,jdbcType=LONGVARCHAR})
71 |
72 |
73 |
77 | insert into post
78 |
79 |
80 | id,
81 |
82 |
83 | blog_id,
84 |
85 |
86 | draft,
87 |
88 |
89 | content,
90 |
91 |
92 |
93 |
94 | #{id,jdbcType=INTEGER},
95 |
96 |
97 | #{blogId,jdbcType=INTEGER},
98 |
99 |
100 | #{draft,jdbcType=INTEGER},
101 |
102 |
103 | #{content,jdbcType=LONGVARCHAR},
104 |
105 |
106 |
107 |
108 |
112 | update post
113 |
114 |
115 | blog_id = #{blogId,jdbcType=INTEGER},
116 |
117 |
118 | draft = #{draft,jdbcType=INTEGER},
119 |
120 |
121 | content = #{content,jdbcType=LONGVARCHAR},
122 |
123 |
124 | where id = #{id,jdbcType=INTEGER}
125 |
126 |
127 |
131 | update post
132 | set blog_id = #{blogId,jdbcType=INTEGER},
133 | draft = #{draft,jdbcType=INTEGER},
134 | content = #{content,jdbcType=LONGVARCHAR}
135 | where id = #{id,jdbcType=INTEGER}
136 |
137 |
138 |
142 | update post
143 | set blog_id = #{blogId,jdbcType=INTEGER},
144 | draft = #{draft,jdbcType=INTEGER}
145 | where id = #{id,jdbcType=INTEGER}
146 |
147 |
--------------------------------------------------------------------------------
/mybatis-mapping/target/classes/com/homejim/mybatis/mapper/PostMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
18 |
19 |
20 |
21 |
25 | id, blog_id, draft
26 |
27 |
28 |
32 | content
33 |
34 |
46 |
54 |
55 |
59 | delete from post
60 | where id = #{id,jdbcType=INTEGER}
61 |
62 |
63 |
67 | insert into post (id, blog_id, draft,
68 | content)
69 | values (#{id,jdbcType=INTEGER}, #{blogId,jdbcType=INTEGER}, #{draft,jdbcType=INTEGER},
70 | #{content,jdbcType=LONGVARCHAR})
71 |
72 |
73 |
77 | insert into post
78 |
79 |
80 | id,
81 |
82 |
83 | blog_id,
84 |
85 |
86 | draft,
87 |
88 |
89 | content,
90 |
91 |
92 |
93 |
94 | #{id,jdbcType=INTEGER},
95 |
96 |
97 | #{blogId,jdbcType=INTEGER},
98 |
99 |
100 | #{draft,jdbcType=INTEGER},
101 |
102 |
103 | #{content,jdbcType=LONGVARCHAR},
104 |
105 |
106 |
107 |
108 |
112 | update post
113 |
114 |
115 | blog_id = #{blogId,jdbcType=INTEGER},
116 |
117 |
118 | draft = #{draft,jdbcType=INTEGER},
119 |
120 |
121 | content = #{content,jdbcType=LONGVARCHAR},
122 |
123 |
124 | where id = #{id,jdbcType=INTEGER}
125 |
126 |
127 |
131 | update post
132 | set blog_id = #{blogId,jdbcType=INTEGER},
133 | draft = #{draft,jdbcType=INTEGER},
134 | content = #{content,jdbcType=LONGVARCHAR}
135 | where id = #{id,jdbcType=INTEGER}
136 |
137 |
138 |
142 | update post
143 | set blog_id = #{blogId,jdbcType=INTEGER},
144 | draft = #{draft,jdbcType=INTEGER}
145 | where id = #{id,jdbcType=INTEGER}
146 |
147 |
--------------------------------------------------------------------------------
/mybatis-cache/src/test/java/com/homejim/mybatis/mapper/MybatisCacheTest.java:
--------------------------------------------------------------------------------
1 | package com.homejim.mybatis.mapper;
2 |
3 | import com.homejim.mybatis.entity.Student;
4 | import org.apache.ibatis.io.Resources;
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
8 | import org.junit.Assert;
9 | import org.junit.BeforeClass;
10 | import org.junit.Test;
11 |
12 | import java.io.IOException;
13 | import java.io.Reader;
14 | import java.util.List;
15 |
16 | /**
17 | * Created by IntelliJ IDEA
18 | * User: Homejim
19 | * Date 2018-9-4
20 | * Time 20:40
21 | */
22 | public class MybatisCacheTest {
23 | private static SqlSessionFactory sqlSessionFactory;
24 |
25 | @BeforeClass
26 | public static void init() {
27 | try {
28 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
29 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
30 | } catch (IOException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 |
35 | @Test
36 | public void oneSqlSession() {
37 | SqlSession sqlSession = null;
38 | try {
39 | sqlSession = sqlSessionFactory.openSession();
40 |
41 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
42 | // 执行第一次查询
43 | List students = studentMapper.selectAll();
44 | for (int i = 0; i < students.size(); i++) {
45 | System.out.println(students.get(i));
46 | }
47 | studentMapper.updateByPrimaryKey(students.get(0));
48 | System.out.println("=============开始同一个 Sqlsession 的第二次查询============");
49 | // 同一个 sqlSession 进行第二次查询
50 |
51 | List stus = studentMapper.selectAll();
52 | Assert.assertEquals(students, stus);
53 | for (int i = 0; i < stus.size(); i++) {
54 | System.out.println("stus:" + stus.get(i));
55 | }
56 | } catch (Exception e) {
57 | e.printStackTrace();
58 | } finally {
59 | if (sqlSession != null) {
60 | sqlSession.close();
61 | }
62 | }
63 | }
64 |
65 | @Test
66 | public void differSqlSession() {
67 | SqlSession sqlSession = null;
68 | SqlSession sqlSession2 = null;
69 | try {
70 | sqlSession = sqlSessionFactory.openSession();
71 |
72 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
73 | // 执行第一次查询
74 | List students = studentMapper.selectAll();
75 | for (int i = 0; i < students.size(); i++) {
76 | System.out.println(students.get(i));
77 | }
78 | System.out.println("=============开始不同 Sqlsession 的第二次查询============");
79 | // 从新创建一个 sqlSession2 进行第二次查询
80 | sqlSession2 = sqlSessionFactory.openSession();
81 | StudentMapper studentMapper2 = sqlSession2.getMapper(StudentMapper.class);
82 | List stus = studentMapper2.selectAll();
83 | Assert.assertNotEquals(students, stus);
84 | for (int i = 0; i < stus.size(); i++) {
85 | System.out.println("stus:" + stus.get(i));
86 | }
87 | } catch (Exception e) {
88 | e.printStackTrace();
89 | } finally {
90 | if (sqlSession != null) {
91 | sqlSession.close();
92 | }
93 | if (sqlSession2 != null) {
94 | sqlSession2.close();
95 | }
96 | }
97 | }
98 |
99 | @Test
100 | public void sameSqlSessionNoCache() {
101 | SqlSession sqlSession = null;
102 | try {
103 | sqlSession = sqlSessionFactory.openSession();
104 |
105 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
106 | // 执行第一次查询
107 | Student student = studentMapper.selectByPrimaryKey(1);
108 | List students = studentMapper.selectAll();
109 | System.out.println("=============开始同一个 Sqlsession 的第二次查询============");
110 | // 同一个 sqlSession 进行第二次查询
111 | Student stu = studentMapper.selectByPrimaryKey(1);
112 | List stus = studentMapper.selectAll();
113 | Assert.assertNotEquals(student, stu);
114 | Assert.assertNotEquals(students, stus);
115 | } catch (Exception e) {
116 | e.printStackTrace();
117 | } finally {
118 | if (sqlSession != null) {
119 | sqlSession.close();
120 | }
121 | }
122 | }
123 |
124 | @Test
125 | public void secendLevelCacheTest() {
126 |
127 | // 获取 SqlSession 对象
128 | SqlSession sqlSession = sqlSessionFactory.openSession();
129 | // 获取 Mapper 对象
130 | StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
131 | // 使用 Mapper 接口的对应方法,查询 id=2 的对象
132 | Student student = studentMapper.selectByPrimaryKey(2);
133 | // 更新对象的名称
134 | student.setName("奶茶");
135 | // 再次使用相同的 SqlSession 查询id=2 的对象
136 | Student student1 = studentMapper.selectByPrimaryKey(2);
137 | /* Assert.assertEquals("奶茶", student1.getName());
138 | // 同一个 SqlSession 使用缓存, 则得到的对象都一样的
139 | Assert.assertEquals(student, student1);*/
140 |
141 | sqlSession.close();
142 |
143 | SqlSession sqlSession1 = sqlSessionFactory.openSession();
144 | StudentMapper studentMapper1 = sqlSession1.getMapper(StudentMapper.class);
145 | Student student2 = studentMapper1.selectByPrimaryKey(2);
146 | Student student3 = studentMapper1.selectByPrimaryKey(2);
147 | Assert.assertEquals("奶茶", student2.getName());
148 | Assert.assertNotEquals(student3, student2);
149 |
150 | sqlSession1.close();
151 | }
152 |
153 | }
154 |
--------------------------------------------------------------------------------
/mybatis-mapping/src/test/java/com/mybatis/homejim/mapper/BlogMapperTest.java:
--------------------------------------------------------------------------------
1 | package com.mybatis.homejim.mapper;
2 |
3 | import com.homejim.mybatis.entity.BlogBO;
4 | import com.homejim.mybatis.entity.BlogCustom;
5 | import com.homejim.mybatis.entity.BlogPostBO;
6 | import com.homejim.mybatis.entity.Post;
7 | import com.homejim.mybatis.mapper.BlogMapper;
8 | import org.apache.commons.lang3.builder.ToStringBuilder;
9 | import org.apache.commons.lang3.builder.ToStringStyle;
10 | import org.apache.ibatis.io.Resources;
11 | import org.apache.ibatis.session.SqlSession;
12 | import org.apache.ibatis.session.SqlSessionFactory;
13 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
14 | import org.junit.Assert;
15 | import org.junit.BeforeClass;
16 | import org.junit.Test;
17 |
18 | import java.io.IOException;
19 | import java.io.Reader;
20 | import java.util.List;
21 |
22 | public class BlogMapperTest {
23 | private static SqlSessionFactory sqlSessionFactory;
24 |
25 | @BeforeClass
26 | public static void init() {
27 | try {
28 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
29 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
30 | } catch (IOException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 |
35 | /**
36 | * resultType 方式一测试
37 | */
38 | @Test
39 | public void testSelectBoById() {
40 | SqlSession sqlSession = sqlSessionFactory.openSession();
41 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
42 | BlogBO blogBO = blogMapper.selectBoById(1);
43 | System.out.println(ToStringBuilder.reflectionToString(blogBO, ToStringStyle.MULTI_LINE_STYLE));
44 | }
45 | /**
46 | * resultType 方式二测试
47 | */
48 | @Test
49 | public void testSelectCustomById() {
50 | SqlSession sqlSession = sqlSessionFactory.openSession();
51 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
52 | BlogCustom blogCustom = blogMapper.selectCutomById(1);
53 | System.out.println(ToStringBuilder.reflectionToString(blogCustom, ToStringStyle.MULTI_LINE_STYLE));
54 | }
55 |
56 | /**
57 | * resultMap 方式测试
58 | */
59 | @Test
60 | public void testSelectCustomByIdMap() {
61 | SqlSession sqlSession = sqlSessionFactory.openSession();
62 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
63 | BlogCustom blogCustom = blogMapper.selectCutomByIdMap(1);
64 | System.out.println(ToStringBuilder.reflectionToString(blogCustom, ToStringStyle.MULTI_LINE_STYLE));
65 | }
66 |
67 | /**
68 | * resultMap + association 方式测试
69 | */
70 | @Test
71 | public void testSelectCustomByIdAMap() {
72 | SqlSession sqlSession = sqlSessionFactory.openSession();
73 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
74 | BlogCustom blogCustom = blogMapper.selectCutomByIdAMap(1);
75 | System.out.println(ToStringBuilder.reflectionToString(blogCustom, ToStringStyle.MULTI_LINE_STYLE));
76 | }
77 |
78 | /**
79 | * resultMap + association 嵌套查询方式测试
80 | */
81 | @Test
82 | public void testSelectBlogAndAuthorByIdSelect() {
83 | SqlSession sqlSession = sqlSessionFactory.openSession();
84 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
85 | BlogCustom blogCustom = blogMapper.selectBlogAndAuthorByIdSelect(1);
86 |
87 | Assert.assertNotNull(blogCustom);
88 | System.out.println("开始使用author对象");
89 | Assert.assertNotNull(blogCustom.getAuthor());
90 | System.out.println(ToStringBuilder.reflectionToString(blogCustom, ToStringStyle.MULTI_LINE_STYLE));
91 |
92 | }
93 |
94 | /**
95 | * resultMap + association 嵌套查询方式测试(延迟加载)
96 | */
97 | @Test
98 | public void testSelectBlogAndAuthorByIdSelectLazy() {
99 | SqlSession sqlSession = sqlSessionFactory.openSession();
100 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
101 | BlogCustom blogCustom = blogMapper.selectBlogAndAuthorByIdSelect(1);
102 |
103 | Assert.assertNotNull(blogCustom);
104 | System.out.println("开始使用author对象");
105 | Assert.assertNotNull(blogCustom.getAuthor());
106 |
107 | }
108 | /**
109 | * resultMap + association 嵌套查询方式测试(延迟加载不延迟lazyLoadTriggerMethods)
110 | */
111 | @Test
112 | public void testSelectBlogAndAuthorByIdSelectTrigger() {
113 | SqlSession sqlSession = sqlSessionFactory.openSession();
114 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
115 | BlogCustom blogCustom = blogMapper.selectBlogAndAuthorByIdSelect(1);
116 | blogCustom.equals(null);
117 | Assert.assertNotNull(blogCustom);
118 | sqlSession.close();
119 | System.out.println("开始使用author对象");
120 | Assert.assertNotNull(blogCustom.getAuthor());
121 |
122 | }
123 | /**
124 | * resultMap + collection 一对多映射
125 | */
126 | @Test
127 | public void testSelectBlogAndPostListByBlog() {
128 | SqlSession sqlSession = sqlSessionFactory.openSession();
129 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
130 | List blogPostBOs = blogMapper.selectBlogAndPostList();
131 |
132 | sqlSession.close();
133 |
134 | for (int i = 0; i < blogPostBOs.size(); i++) {
135 | BlogPostBO blogPostBO = blogPostBOs.get(i);
136 | System.out.println(blogPostBO.getTitle());
137 |
138 | for (int j = 0; j < blogPostBO.getPosts().size(); j++) {
139 | System.out.println(blogPostBO.getPosts().get(j).getContent());
140 | }
141 |
142 | System.out.println("=============这是对象分割线===============");
143 | }
144 | }
145 |
146 | /**
147 | * resultMap + collection 一对多映射, 嵌套查询
148 | */
149 | @Test
150 | public void testSelectBlogAndPostListByBlogLazy() {
151 | SqlSession sqlSession = sqlSessionFactory.openSession();
152 | BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
153 | List blogPostBOs = blogMapper.selectBlogAndPostListLazy();
154 |
155 | for (int i = 0; i < blogPostBOs.size(); i++) {
156 | BlogPostBO blogPostBO = blogPostBOs.get(i);
157 | System.out.println(blogPostBO.getTitle());
158 | List posts = blogPostBO.getPosts();
159 |
160 | for (int j = 0; j < posts.size(); j++) {
161 | System.out.println(blogPostBO.getPosts().get(j).getContent());
162 | }
163 |
164 | System.out.println("=============这是对象分割线===============");
165 | }
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/mybatis-dynamic/src/main/java/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
17 |
18 |
23 |
40 |
46 |
52 |
58 |
71 |
84 |
93 |
94 | insert into student
95 |
96 |
97 | student_id,
98 |
99 |
100 | `name`,
101 |
102 |
103 | phone,
104 |
105 |
106 | email,
107 |
108 |
109 | sex,
110 |
111 |
112 | locked,
113 |
114 |
115 | gmt_created,
116 |
117 |
118 | gmt_modified,
119 |
120 |
121 |
122 |
123 | #{studentId,jdbcType=INTEGER},
124 |
125 |
126 | #{name,jdbcType=VARCHAR},
127 |
128 |
129 | #{phone,jdbcType=VARCHAR},
130 |
131 |
132 | #{email,jdbcType=VARCHAR},
133 |
134 |
135 | #{sex,jdbcType=TINYINT},
136 |
137 |
138 | #{locked,jdbcType=TINYINT},
139 |
140 |
141 | #{gmtCreated,jdbcType=TIMESTAMP},
142 |
143 |
144 | #{gmtModified,jdbcType=TIMESTAMP},
145 |
146 |
147 |
148 |
149 | insert into student(name, phone, email, sex, locked)
150 | values
151 |
152 | (
153 | #{student.name}, #{student.phone},#{student.email},
154 | #{student.sex},#{student.locked}
155 | )
156 |
157 |
158 |
159 | update student
160 |
161 |
162 | `name` = #{name,jdbcType=VARCHAR},
163 |
164 |
165 | phone = #{phone,jdbcType=VARCHAR},
166 |
167 |
168 | email = #{email,jdbcType=VARCHAR},
169 |
170 |
171 | sex = #{sex,jdbcType=TINYINT},
172 |
173 |
174 | locked = #{locked,jdbcType=TINYINT},
175 |
176 |
177 | gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},
178 |
179 |
180 | gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
181 |
182 |
183 | where student_id = #{studentId,jdbcType=INTEGER}
184 |
185 |
186 | update student
187 | set
188 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
189 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
190 | where student_id=#{studentId, jdbcType=INTEGER}
191 |
192 |
--------------------------------------------------------------------------------
/mybatis-dynamic/target/classes/com/homejim/mybatis/mapper/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
17 |
18 |
23 |
40 |
46 |
52 |
58 |
71 |
84 |
93 |
94 | insert into student
95 |
96 |
97 | student_id,
98 |
99 |
100 | `name`,
101 |
102 |
103 | phone,
104 |
105 |
106 | email,
107 |
108 |
109 | sex,
110 |
111 |
112 | locked,
113 |
114 |
115 | gmt_created,
116 |
117 |
118 | gmt_modified,
119 |
120 |
121 |
122 |
123 | #{studentId,jdbcType=INTEGER},
124 |
125 |
126 | #{name,jdbcType=VARCHAR},
127 |
128 |
129 | #{phone,jdbcType=VARCHAR},
130 |
131 |
132 | #{email,jdbcType=VARCHAR},
133 |
134 |
135 | #{sex,jdbcType=TINYINT},
136 |
137 |
138 | #{locked,jdbcType=TINYINT},
139 |
140 |
141 | #{gmtCreated,jdbcType=TIMESTAMP},
142 |
143 |
144 | #{gmtModified,jdbcType=TIMESTAMP},
145 |
146 |
147 |
148 |
149 | insert into student(name, phone, email, sex, locked)
150 | values
151 |
152 | (
153 | #{student.name}, #{student.phone},#{student.email},
154 | #{student.sex},#{student.locked}
155 | )
156 |
157 |
158 |
159 | update student
160 |
161 |
162 | `name` = #{name,jdbcType=VARCHAR},
163 |
164 |
165 | phone = #{phone,jdbcType=VARCHAR},
166 |
167 |
168 | email = #{email,jdbcType=VARCHAR},
169 |
170 |
171 | sex = #{sex,jdbcType=TINYINT},
172 |
173 |
174 | locked = #{locked,jdbcType=TINYINT},
175 |
176 |
177 | gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},
178 |
179 |
180 | gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
181 |
182 |
183 | where student_id = #{studentId,jdbcType=INTEGER}
184 |
185 |
186 | update student
187 | set
188 | name=#{name, jdbcType=VARCHAR}, phone=#{phone, jdbcType=VARCHAR}, email=#{email, jdbcType=VARCHAR},
189 | sex=#{sex, jdbcType=VARCHAR}, locked=#{locked, jdbcType=VARCHAR}
190 | where student_id=#{studentId, jdbcType=INTEGER}
191 |
192 |
--------------------------------------------------------------------------------