├── mybatis-02-configuration ├── src │ └── main │ │ ├── resources │ │ ├── jdbc2.properties │ │ ├── jdbc1.properties │ │ ├── jdbc.properties │ │ ├── log4j.properties │ │ └── mapper │ │ │ └── department.xml │ │ └── java │ │ └── com │ │ └── linkedbear │ │ └── mybatis │ │ ├── mapper │ │ ├── UserMapper.java │ │ └── DepartmentMapper.java │ │ ├── factory │ │ └── ExtendsObjectFactory.java │ │ ├── app │ │ ├── DatabaseProviderApplication.java │ │ ├── MyBatisApplication6.java │ │ ├── LoadPropertiesApplication.java │ │ └── DepartmentTypeHandlerApplication.java │ │ ├── handler │ │ └── DepartmentTypeHandler.java │ │ └── entity │ │ ├── Department.java │ │ └── User.java └── pom.xml ├── mybatis-09-encapsulate └── src │ └── main │ ├── java │ └── com │ │ └── linkedbear │ │ └── mybatis │ │ ├── encapsulate │ │ ├── c_extra │ │ │ ├── annotation │ │ │ │ ├── strategy │ │ │ │ │ ├── IdGenerator.java │ │ │ │ │ └── UuidGenerator.java │ │ │ │ ├── Version.java │ │ │ │ ├── LogicDelete.java │ │ │ │ ├── Transient.java │ │ │ │ ├── query │ │ │ │ │ ├── Equals.java │ │ │ │ │ └── Like.java │ │ │ │ ├── Table.java │ │ │ │ ├── Column.java │ │ │ │ └── Id.java │ │ │ ├── DepartmentMapper.java │ │ │ ├── provider │ │ │ │ ├── FindAll.java │ │ │ │ ├── AbstractProvider.java │ │ │ │ ├── Delete.java │ │ │ │ ├── FindByExample.java │ │ │ │ ├── Insert.java │ │ │ │ └── Update.java │ │ │ ├── metadata │ │ │ │ ├── EntityMetadataManager.java │ │ │ │ ├── FieldMetadata.java │ │ │ │ └── EntityMetadata.java │ │ │ ├── BaseMapper.java │ │ │ └── app │ │ │ │ └── EncapsulateApplication.java │ │ ├── a_provider │ │ │ ├── annotation │ │ │ │ ├── strategy │ │ │ │ │ ├── IdGenerator.java │ │ │ │ │ └── UuidGenerator.java │ │ │ │ ├── Transient.java │ │ │ │ ├── Table.java │ │ │ │ ├── Column.java │ │ │ │ └── Id.java │ │ │ ├── BaseMapper.java │ │ │ ├── DepartmentMapper.java │ │ │ ├── app │ │ │ │ └── EncapsulateApplication.java │ │ │ └── entity │ │ │ │ └── Department.java │ │ ├── b_metadata │ │ │ ├── annotation │ │ │ │ ├── strategy │ │ │ │ │ ├── IdGenerator.java │ │ │ │ │ └── UuidGenerator.java │ │ │ │ ├── Transient.java │ │ │ │ ├── Table.java │ │ │ │ ├── Column.java │ │ │ │ └── Id.java │ │ │ ├── provider │ │ │ │ ├── FindAll.java │ │ │ │ ├── AbstractProvider.java │ │ │ │ └── Insert.java │ │ │ ├── DepartmentMapper.java │ │ │ ├── BaseMapper.java │ │ │ ├── app │ │ │ │ └── EncapsulateApplication.java │ │ │ ├── metadata │ │ │ │ ├── EntityMetadataManager.java │ │ │ │ ├── FieldMetadata.java │ │ │ │ └── EntityMetadata.java │ │ │ └── entity │ │ │ │ └── Department.java │ │ └── util │ │ │ ├── ClassUtils.java │ │ │ └── StringConvertUtils.java │ │ ├── mapper │ │ ├── UserMapper.java │ │ └── DepartmentMapper.java │ │ ├── app │ │ ├── MyBatisSpringApplication.java │ │ └── MyBatisApplication6.java │ │ └── entity │ │ ├── Department.java │ │ └── User.java │ └── resources │ ├── c_extra │ ├── mybatis-config.xml │ ├── log4j.properties │ └── department.xml │ ├── a_provider │ ├── mybatis-config.xml │ ├── log4j.properties │ ├── department.xml │ └── spring-mybatis.xml │ ├── b_metadata │ ├── mybatis-config.xml │ ├── log4j.properties │ └── department.xml │ ├── log4j.properties │ ├── mapper │ └── department.xml │ ├── mybatis-config.xml │ └── spring-mybatis.xml ├── mybatis-05-cache ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── linkedbear │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── DepartmentMapper.java │ │ │ ├── app │ │ │ ├── MyBatisApplication6.java │ │ │ ├── Level1Application.java │ │ │ ├── Level2Application.java │ │ │ ├── Level1ReferenceApplication.java │ │ │ ├── Level2ReadonlyApplication.java │ │ │ └── Level1InvalidApplication.java │ │ │ └── entity │ │ │ ├── Department.java │ │ │ └── User.java │ │ └── resources │ │ ├── log4j.properties │ │ ├── mapper │ │ └── department.xml │ │ ├── mybatis-config.xml │ │ └── ehcache.xml └── pom.xml ├── mybatis-07-extra ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── linkedbear │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── DepartmentMapper.java │ │ │ ├── app │ │ │ ├── MyBatisApplication6.java │ │ │ └── InterceptorApplication.java │ │ │ ├── plugin │ │ │ └── CustomInterceptor.java │ │ │ └── entity │ │ │ ├── Department.java │ │ │ └── User.java │ │ └── resources │ │ ├── log4j.properties │ │ ├── mapper │ │ └── department.xml │ │ └── mybatis-config.xml └── pom.xml ├── mybatis-08-spring ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── linkedbear │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── DepartmentMapper.java │ │ │ ├── app │ │ │ ├── MyBatisSpringApplication.java │ │ │ └── MyBatisApplication6.java │ │ │ └── entity │ │ │ ├── Department.java │ │ │ └── User.java │ │ └── resources │ │ ├── log4j.properties │ │ ├── mapper │ │ └── department.xml │ │ ├── mybatis-config.xml │ │ └── spring-mybatis.xml └── pom.xml ├── mybatis-06-transaction ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── linkedbear │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ └── DepartmentMapper.java │ │ │ ├── app │ │ │ ├── MyBatisApplication6.java │ │ │ └── OpenSessionAutoCommitApplication.java │ │ │ └── entity │ │ │ ├── Department.java │ │ │ └── User.java │ │ └── resources │ │ ├── log4j.properties │ │ ├── mapper │ │ └── department.xml │ │ └── mybatis-config.xml └── pom.xml ├── .gitignore ├── mybatis-01-basic ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── linkedbear │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ ├── UserMapper.java │ │ │ ├── DepartmentDao.java │ │ │ ├── DepartmentMapper.java │ │ │ └── DepartmentDaoImpl.java │ │ │ ├── app │ │ │ ├── MyBatisApplication1.java │ │ │ ├── MyBatisApplication2.java │ │ │ ├── MyBatisApplication4.java │ │ │ ├── MyBatisApplication5.java │ │ │ ├── MyBatisApplication3.java │ │ │ └── MyBatisApplication6.java │ │ │ └── entity │ │ │ ├── Department.java │ │ │ └── User.java │ │ └── resources │ │ ├── mapper │ │ ├── department1.xml │ │ ├── department2.xml │ │ ├── department3.xml │ │ └── user.xml │ │ ├── log4j.properties │ │ ├── mybatis-config-1.xml │ │ ├── mybatis-config-2.xml │ │ ├── mybatis-config-3.xml │ │ └── mybatis-config-4.xml └── pom.xml ├── mybatis-03-mapper ├── src │ └── main │ │ ├── resources │ │ ├── mapper │ │ │ └── test.xml │ │ ├── log4j.properties │ │ └── mybatis-config.xml │ │ └── java │ │ └── com │ │ └── linkedbear │ │ └── mybatis │ │ ├── mapper │ │ ├── DepartmentMapper.java │ │ └── UserMapper.java │ │ ├── app │ │ ├── JdbcFetchSizeApplication.java │ │ ├── GeneratedKeysApplication.java │ │ ├── FetchsizeApplication.java │ │ ├── MyBatisApplication6.java │ │ ├── JdbcResultSetTypeApplication.java │ │ ├── ResultMapApplication.java │ │ ├── DynamicSqlApplication.java │ │ ├── UpdateForeachApplication.java │ │ └── SelectUseCacheApplication.java │ │ └── entity │ │ ├── Department.java │ │ └── User.java └── pom.xml └── mybatis-04-annotation ├── src └── main │ ├── java │ └── com │ │ └── linkedbear │ │ └── mybatis │ │ ├── mapper │ │ ├── DepartmentMapper.java │ │ ├── UserMapper.java │ │ └── UserAnnotationMapper.java │ │ ├── app │ │ ├── MyBatisApplication6.java │ │ └── ProviderMapperApplication.java │ │ ├── entity │ │ ├── Department.java │ │ └── User.java │ │ └── provider │ │ └── UserMapperProvider.java │ └── resources │ ├── log4j.properties │ ├── mybatis-config.xml │ └── mapper │ └── department.xml └── pom.xml /mybatis-02-configuration/src/main/resources/jdbc2.properties: -------------------------------------------------------------------------------- 1 | jdbc.username=root 2 | jdbc.password=1599999 -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/resources/jdbc1.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8 -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/strategy/IdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation.strategy; 2 | 3 | public interface IdGenerator { 4 | 5 | T next(); 6 | } 7 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/strategy/IdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation.strategy; 2 | 3 | public interface IdGenerator { 4 | 5 | T next(); 6 | } 7 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/strategy/IdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation.strategy; 2 | 3 | public interface IdGenerator { 4 | 5 | T next(); 6 | } 7 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | } 11 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | } 11 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | } 11 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | } 11 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar 11 | .mvn/wrapper/maven-wrapper.jar 12 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | 11 | List findAllLazy(); 12 | } 13 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/mapper/DepartmentDao.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentDao { 8 | 9 | List findAll(); 10 | 11 | Department findById(String id); 12 | } 13 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | jdbc.driverClassName=com.mysql.jdbc.Driver 2 | jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8 3 | jdbc.username=root 4 | jdbc.password=123456 5 | #jdbc.driverClassName=org.postgresql.Driver 6 | #jdbc.url=jdbc:postgresql://localhost:5432/postgres 7 | #jdbc.username=postgres 8 | #jdbc.password=123456 -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mapper/department1.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/c_extra/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/strategy/UuidGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation.strategy; 2 | 3 | import java.util.UUID; 4 | 5 | public class UuidGenerator implements IdGenerator { 6 | 7 | @Override 8 | public String next() { 9 | return UUID.randomUUID().toString().replaceAll("-", ""); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/a_provider/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/b_metadata/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/strategy/UuidGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation.strategy; 2 | 3 | import java.util.UUID; 4 | 5 | public class UuidGenerator implements IdGenerator { 6 | 7 | @Override 8 | public String next() { 9 | return UUID.randomUUID().toString().replaceAll("-", ""); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/strategy/UuidGenerator.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation.strategy; 2 | 3 | import java.util.UUID; 4 | 5 | public class UuidGenerator implements IdGenerator { 6 | 7 | @Override 8 | public String next() { 9 | return UUID.randomUUID().toString().replaceAll("-", ""); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int update(Department department); 12 | 13 | Department findById(String id); 14 | 15 | int cleanCache(); 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int update(Department department); 12 | 13 | Department findById(String id); 14 | 15 | int cleanCache(); 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int update(Department department); 12 | 13 | Department findById(String id); 14 | 15 | int cleanCache(); 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int update(Department department); 12 | 13 | Department findById(String id); 14 | 15 | int cleanCache(); 16 | } 17 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/resources/mapper/test.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into tbl_dept2 (name, tel) VALUES (#{name}, #{tel}) 9 | 10 | 11 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | //@CacheNamespace 8 | public interface DepartmentMapper { 9 | 10 | List findAll(); 11 | 12 | int update(Department department); 13 | 14 | Department findById(String id); 15 | 16 | int cleanCache(); 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | 11 | List findAllLazy(); 12 | 13 | List findAllUseTypeHandler(); 14 | 15 | int saveUser(User user); 16 | 17 | List findAllByDepartmentId(String departmentId); 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/util/ClassUtils.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.util; 2 | 3 | import java.lang.reflect.ParameterizedType; 4 | 5 | public abstract class ClassUtils { 6 | 7 | public static Class getEntityClass(Class mapperClass) { 8 | ParameterizedType type = (ParameterizedType) mapperClass.getGenericInterfaces()[0]; 9 | return (Class) type.getActualTypeArguments()[0]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int insert(Department department); 12 | 13 | int update(Department department); 14 | 15 | int deleteById(String id); 16 | 17 | Department findById(String id); 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int insert(Department department); 12 | 13 | int update(Department department); 14 | 15 | int deleteById(String id); 16 | 17 | Department findById(String id); 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int insert(Department department); 12 | 13 | int update(Department department); 14 | 15 | int deleteById(String id); 16 | 17 | Department findById(String id); 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/mapper/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | 5 | import java.util.List; 6 | 7 | public interface DepartmentMapper { 8 | 9 | List findAll(); 10 | 11 | int insert(Department department); 12 | 13 | int update(Department department); 14 | 15 | int deleteById(String id); 16 | 17 | Department findById(String id); 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | 11 | List findAllLazy(); 12 | 13 | List findAllUseTypeHandler(); 14 | 15 | int saveUser(User user); 16 | 17 | List findAllByDepartmentId(String departmentId); 18 | 19 | int cleanCache(); 20 | } 21 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | 5 | import java.util.List; 6 | 7 | public interface UserMapper { 8 | 9 | List findAll(); 10 | 11 | List findAllLazy(); 12 | 13 | List findAllUseTypeHandler(); 14 | 15 | int saveUser(User user); 16 | 17 | List findAllByDepartmentId(String departmentId); 18 | 19 | int cleanCache(); 20 | } 21 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/Version.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Version { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/LogicDelete.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface LogicDelete { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/Transient.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Transient { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/Transient.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Transient { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/Transient.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Transient { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/query/Equals.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation.query; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Equals { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/Table.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.TYPE) 12 | public @interface Table { 13 | 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/Table.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.TYPE) 12 | public @interface Table { 13 | 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/Table.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.TYPE) 12 | public @interface Table { 13 | 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/factory/ExtendsObjectFactory.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.factory; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 5 | 6 | public class ExtendsObjectFactory extends DefaultObjectFactory { 7 | 8 | @Override 9 | public T create(Class type) { 10 | T t = super.create(type); 11 | if (User.class.equals(type)) { 12 | User user = (User) t; 13 | user.setAge(0); 14 | } 15 | return t; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/provider/FindAll.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.metadata.EntityMetadata; 4 | import org.apache.ibatis.jdbc.SQL; 5 | 6 | public class FindAll extends AbstractProvider { 7 | 8 | @Override 9 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 10 | SQL sql = new SQL(); 11 | sql.SELECT("*").FROM(entityMetadata.getTablename()); 12 | return sql.toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/Column.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Column { 13 | 14 | String value(); 15 | 16 | boolean updateIfNull() default false; 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/Column.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Column { 13 | 14 | String value(); 15 | 16 | boolean updateIfNull() default false; 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/Column.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Column { 13 | 14 | String value(); 15 | 16 | boolean updateIfNull() default false; 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.entity.Department; 4 | 5 | public interface DepartmentMapper extends BaseMapper { 6 | 7 | // @Select("select * from tbl_department") 8 | // List findAll(); 9 | // 10 | // @Update("update tbl_department set name = #{name}, tel = #{tel} where id = #{id}") 11 | // int update(Department department); 12 | // 13 | // @Select("select * from tbl_department where id = #{id}") 14 | // Department findById(String id); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.entity.Department; 4 | 5 | public interface DepartmentMapper extends BaseMapper { 6 | 7 | // @Select("select * from tbl_department") 8 | // List findAll(); 9 | // 10 | // @Update("update tbl_department set name = #{name}, tel = #{tel} where id = #{id}") 11 | // int update(Department department); 12 | // 13 | // @Select("select * from tbl_department where id = #{id}") 14 | // Department findById(String id); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 12 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/a_provider/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/b_metadata/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/c_extra/log4j.properties: -------------------------------------------------------------------------------- 1 | # Set root category priority to INFO and its only appender to CONSOLE. 2 | #log4j.rootCategory=INFO, CONSOLE debug info warn error fatal 3 | log4j.rootCategory=debug, CONSOLE 4 | 5 | # Set the enterprise logger category to FATAL and its only appender to CONSOLE. 6 | log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE 7 | 8 | # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 | log4j.appender.CONSOLE.Encoding=UTF-8 11 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m \n 13 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/FindAll.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import org.apache.ibatis.jdbc.SQL; 5 | 6 | public class FindAll extends AbstractProvider { 7 | 8 | @Override 9 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 10 | SQL sql = new SQL(); 11 | sql.SELECT("*").FROM(entityMetadata.getTablename()); 12 | if (entityMetadata.getLoginDeleteMetadata() != null) { 13 | sql.WHERE(entityMetadata.getLoginDeleteMetadata().getColumnName() + " = 0"); 14 | } 15 | return sql.toString(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/Id.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.annotation.strategy.IdGenerator; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.annotation.strategy.UuidGenerator; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | @Documented 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target(ElementType.FIELD) 15 | public @interface Id { 16 | 17 | Class generateStrategy() default UuidGenerator.class; 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/BaseMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider; 2 | 3 | import org.apache.ibatis.annotations.InsertProvider; 4 | import org.apache.ibatis.annotations.SelectProvider; 5 | import org.apache.ibatis.annotations.UpdateProvider; 6 | 7 | import java.util.List; 8 | 9 | public interface BaseMapper { 10 | 11 | @InsertProvider(type = BaseProvider.class, method = "insert") 12 | int insert(T entity); 13 | 14 | @UpdateProvider(type = BaseProvider.class, method = "update") 15 | int update(T entity); 16 | 17 | /* 18 | T get(String id); 19 | */ 20 | 21 | @SelectProvider(type = BaseProvider.class, method = "findAll") 22 | List findAll(); 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/annotation/Id.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.annotation; 2 | 3 | import com.linkedbear.mybatis.encapsulate.a_provider.annotation.strategy.IdGenerator; 4 | import com.linkedbear.mybatis.encapsulate.a_provider.annotation.strategy.UuidGenerator; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | @Documented 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target(ElementType.FIELD) 15 | public @interface Id { 16 | 17 | Class generateStrategy() default UuidGenerator.class; 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/annotation/Id.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.annotation; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.strategy.IdGenerator; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.strategy.UuidGenerator; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | @Documented 13 | @Retention(RetentionPolicy.RUNTIME) 14 | @Target(ElementType.FIELD) 15 | public @interface Id { 16 | 17 | Class generateStrategy() default UuidGenerator.class; 18 | } 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/DepartmentMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.a_provider.entity.Department; 4 | import org.apache.ibatis.annotations.Select; 5 | import org.apache.ibatis.annotations.Update; 6 | 7 | import java.util.List; 8 | 9 | public interface DepartmentMapper extends BaseMapper { 10 | 11 | // @Select("select * from tbl_department") 12 | // List findAll(); 13 | // 14 | // @Update("update tbl_department set name = #{name}, tel = #{tel} where id = #{id}") 15 | // int update(Department department); 16 | // 17 | // @Select("select * from tbl_department where id = #{id}") 18 | // Department findById(String id); 19 | } 20 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/AbstractProvider.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadataManager; 5 | import org.apache.ibatis.builder.annotation.ProviderContext; 6 | 7 | public abstract class AbstractProvider { 8 | 9 | public String invoke(Object params, ProviderContext context) { 10 | EntityMetadata entityMetadata = EntityMetadataManager.INSTANCE.getMetadataByMapper(context.getMapperType()); 11 | return buildSql(entityMetadata, params); 12 | } 13 | 14 | protected abstract String buildSql(EntityMetadata entityMetadata, Object params); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/provider/AbstractProvider.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.metadata.EntityMetadataManager; 5 | import org.apache.ibatis.builder.annotation.ProviderContext; 6 | 7 | public abstract class AbstractProvider { 8 | 9 | public String invoke(Object params, ProviderContext context) { 10 | EntityMetadata entityMetadata = EntityMetadataManager.INSTANCE.getMetadataByMapper(context.getMapperType()); 11 | return buildSql(entityMetadata, params); 12 | } 13 | 14 | protected abstract String buildSql(EntityMetadata entityMetadata, Object params); 15 | } 16 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/app/MyBatisSpringApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | import java.util.List; 8 | 9 | public class MyBatisSpringApplication { 10 | 11 | public static void main(String[] args) throws Exception { 12 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml"); 13 | 14 | DepartmentMapper departmentMapper = ctx.getBean(DepartmentMapper.class); 15 | List departmentList = departmentMapper.findAll(); 16 | departmentList.forEach(System.out::println); 17 | 18 | ctx.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/app/MyBatisSpringApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | import java.util.List; 8 | 9 | public class MyBatisSpringApplication { 10 | 11 | public static void main(String[] args) throws Exception { 12 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml"); 13 | 14 | DepartmentMapper departmentMapper = ctx.getBean(DepartmentMapper.class); 15 | List departmentList = departmentMapper.findAll(); 16 | departmentList.forEach(System.out::println); 17 | 18 | ctx.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/c_extra/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/a_provider/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/b_metadata/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 19 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/BaseMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.provider.FindAll; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.provider.Insert; 5 | import org.apache.ibatis.annotations.InsertProvider; 6 | import org.apache.ibatis.annotations.SelectProvider; 7 | import org.apache.ibatis.annotations.UpdateProvider; 8 | 9 | import java.util.List; 10 | 11 | public interface BaseMapper { 12 | 13 | @InsertProvider(type = Insert.class, method = "invoke") 14 | int insert(T entity); 15 | 16 | @UpdateProvider(type = BaseProvider.class, method = "update") 17 | int update(T entity); 18 | 19 | /* 20 | T get(String id); 21 | */ 22 | 23 | @SelectProvider(type = FindAll.class, method = "invoke") 24 | List findAll(); 25 | } 26 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/annotation/query/Like.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.annotation.query; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Target(ElementType.FIELD) 12 | public @interface Like { 13 | 14 | LikeType type() default LikeType.FULL; 15 | 16 | 17 | enum LikeType { 18 | FULL("'%', #, '%'"), LEFT("'%', #"), RIGHT("#, '%'"); 19 | 20 | private String pattern; 21 | 22 | LikeType(String pattern) { 23 | this.pattern = pattern; 24 | } 25 | 26 | public String getPattern() { 27 | return pattern; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication1.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 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 | 9 | import java.io.InputStream; 10 | import java.util.List; 11 | 12 | public class MyBatisApplication1 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config-1.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | List departmentList = sqlSession.selectList("departmentMapper.findAll"); 19 | departmentList.forEach(System.out::println); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 9 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 9 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/app/EncapsulateApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.app; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.DepartmentMapper; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.entity.Department; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | public class EncapsulateApplication { 8 | 9 | public static void main(String[] args) throws Exception { 10 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("b_metadata/spring-mybatis.xml"); 11 | DepartmentMapper departmentMapper = ctx.getBean(DepartmentMapper.class); 12 | departmentMapper.findAll(); 13 | 14 | Department department = new Department(); 15 | department.setName("test encapsulate"); 16 | department.setTel("12345"); 17 | departmentMapper.insert(department); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/JdbcFetchSizeApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | 8 | public class JdbcFetchSizeApplication { 9 | 10 | public static void main(String[] args) throws Exception { 11 | Class.forName("com.mysql.jdbc.Driver"); 12 | Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "123456"); 13 | PreparedStatement ps = connection.prepareStatement("select * from tbl_department"); 14 | ps.setFetchSize(10); 15 | ResultSet resultSet = ps.executeQuery(); 16 | while (resultSet.next()) { 17 | System.out.println(resultSet.getString("name")); 18 | } 19 | resultSet.close(); 20 | ps.close(); 21 | connection.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 9 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/metadata/EntityMetadataManager.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.util.ClassUtils; 4 | 5 | import java.util.concurrent.ConcurrentHashMap; 6 | import java.util.concurrent.ConcurrentMap; 7 | 8 | public enum EntityMetadataManager { 9 | INSTANCE; 10 | 11 | private ConcurrentMap, EntityMetadata> entityMetadataMap = new ConcurrentHashMap<>(); 12 | 13 | public void registerMetadata(EntityMetadata metadata) { 14 | this.entityMetadataMap.put(metadata.getEntityClass(), metadata); 15 | } 16 | 17 | public EntityMetadata getMetadata(Class entityClass) { 18 | return entityMetadataMap.get(entityClass); 19 | } 20 | 21 | public EntityMetadata getMetadataByMapper(Class mapperClass) { 22 | return getMetadata(ClassUtils.getEntityClass(mapperClass)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/metadata/EntityMetadataManager.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.util.ClassUtils; 4 | 5 | import java.util.concurrent.ConcurrentHashMap; 6 | import java.util.concurrent.ConcurrentMap; 7 | 8 | public enum EntityMetadataManager { 9 | INSTANCE; 10 | 11 | private ConcurrentMap, EntityMetadata> entityMetadataMap = new ConcurrentHashMap<>(); 12 | 13 | public void registerMetadata(EntityMetadata metadata) { 14 | this.entityMetadataMap.put(metadata.getEntityClass(), metadata); 15 | } 16 | 17 | public EntityMetadata getMetadata(Class entityClass) { 18 | return entityMetadataMap.get(entityClass); 19 | } 20 | 21 | public EntityMetadata getMetadataByMapper(Class mapperClass) { 22 | return getMetadata(ClassUtils.getEntityClass(mapperClass)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 9 | 10 | 11 | 14 | 15 | 18 | 19 | 22 | 23 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mybatis-config-1.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mybatis-config-2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication2.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentDao; 5 | import com.linkedbear.mybatis.mapper.DepartmentDaoImpl; 6 | import org.apache.ibatis.io.Resources; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class MyBatisApplication2 { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config-2.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | 19 | DepartmentDao departmentDao = new DepartmentDaoImpl(sqlSessionFactory); 20 | List departmentList = departmentDao.findAll(); 21 | departmentList.forEach(System.out::println); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication4.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import com.linkedbear.mybatis.mapper.UserMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class MyBatisApplication4 { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config-3.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | 20 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 21 | List userList = userMapper.findAll(); 22 | userList.forEach(System.out::println); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication5.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import com.linkedbear.mybatis.mapper.UserMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class MyBatisApplication5 { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config-3.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | 20 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 21 | List userList = userMapper.findAllLazy(); 22 | userList.forEach(System.out::println); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 11 | 12 | 13 | 16 | 17 | 20 | 21 | 24 | 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mybatis-config-3.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mybatis-config-4.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/GeneratedKeysApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 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 | 9 | import java.io.InputStream; 10 | 11 | public class GeneratedKeysApplication { 12 | 13 | public static void main(String[] args) throws Exception { 14 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 15 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 16 | SqlSession sqlSession = sqlSessionFactory.openSession(); 17 | 18 | Department department = new Department(); 19 | department.setName("hahaha"); 20 | department.setTel("12345"); 21 | sqlSession.insert("test.save", department); 22 | sqlSession.commit(); 23 | 24 | System.out.println(department); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication3.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication3 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config-2.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/FetchsizeApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class FetchsizeApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | 20 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 21 | List departmentList = departmentMapper.findAll(); 22 | departmentList.forEach(System.out::println); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/app/DatabaseProviderApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import com.linkedbear.mybatis.mapper.UserMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class DatabaseProviderApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | 20 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 21 | List userList = userMapper.findAllByDepartmentId("18ec781fbefd727923b0d35740b177ab"); 22 | userList.forEach(System.out::println); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/mapper/DepartmentDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | 7 | import java.util.List; 8 | 9 | public class DepartmentDaoImpl implements DepartmentDao { 10 | 11 | private SqlSessionFactory sqlSessionFactory; 12 | 13 | public DepartmentDaoImpl(SqlSessionFactory sqlSessionFactory) { 14 | this.sqlSessionFactory = sqlSessionFactory; 15 | } 16 | 17 | @Override 18 | public List findAll() { 19 | try (SqlSession sqlSession = sqlSessionFactory.openSession()) { 20 | return sqlSession.selectList("com.linkedbear.mybatis.mapper.DepartmentMapper.findAll"); 21 | } 22 | } 23 | 24 | @Override 25 | public Department findById(String id) { 26 | try (SqlSession sqlSession = sqlSessionFactory.openSession()) { 27 | return sqlSession.selectOne("com.linkedbear.mybatis.mapper.DepartmentMapper.findById", id); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | System.out.println(department.getUsers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config-4.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | System.out.println(department.getUsers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | System.out.println(department.getUsers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mapper/department2.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel}) 9 | 10 | 11 | 12 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 13 | 14 | 15 | 16 | delete from tbl_department where id = #{id} 17 | 18 | 19 | 22 | 23 | 26 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/app/MyBatisApplication6.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class MyBatisApplication6 { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | System.out.println(department.getUsers()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/mapper/UserAnnotationMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.mapper; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import com.linkedbear.mybatis.provider.UserMapperProvider; 5 | import org.apache.ibatis.annotations.DeleteProvider; 6 | import org.apache.ibatis.annotations.InsertProvider; 7 | import org.apache.ibatis.annotations.SelectProvider; 8 | import org.apache.ibatis.annotations.UpdateProvider; 9 | 10 | import java.util.List; 11 | 12 | public interface UserAnnotationMapper { 13 | 14 | @SelectProvider(type = UserMapperProvider.class, method = "findAll") 15 | List findAll(); 16 | 17 | @SelectProvider(type = UserMapperProvider.class, method = "findAllByExample") 18 | List findAllByExample(User example); 19 | 20 | @InsertProvider(type = UserMapperProvider.class, method = "save") 21 | void save(User user); 22 | 23 | @UpdateProvider(type = UserMapperProvider.class, method = "updateByExample") 24 | int updateByExample(User user); 25 | 26 | @DeleteProvider(type = UserMapperProvider.class, method = "deleteById") 27 | int deleteById(String id); 28 | } 29 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/Delete.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.FieldMetadata; 5 | import org.apache.ibatis.jdbc.SQL; 6 | 7 | public class Delete extends AbstractProvider { 8 | 9 | @Override 10 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 11 | SQL sql = new SQL(); 12 | FieldMetadata loginDeleteMetadata = entityMetadata.getLoginDeleteMetadata(); 13 | FieldMetadata idMetadata = entityMetadata.getIdMetadata(); 14 | if (loginDeleteMetadata != null) { 15 | sql.UPDATE(entityMetadata.getTablename()); 16 | sql.SET(loginDeleteMetadata.getColumnName() + " = 1"); 17 | sql.WHERE(idMetadata.getColumnName() + " = #{" + idMetadata.getFieldName() + "}"); 18 | } else { 19 | sql.DELETE_FROM(entityMetadata.getTablename()); 20 | sql.WHERE(idMetadata.getColumnName() + " = #{" + idMetadata.getFieldName() + "}"); 21 | } 22 | return sql.toString(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/app/EncapsulateApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.app; 2 | 3 | import com.linkedbear.mybatis.encapsulate.a_provider.DepartmentMapper; 4 | import com.linkedbear.mybatis.encapsulate.a_provider.entity.Department; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | import java.util.List; 8 | 9 | public class EncapsulateApplication { 10 | 11 | public static void main(String[] args) throws Exception { 12 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("a_provider/spring-mybatis.xml"); 13 | DepartmentMapper departmentMapper = ctx.getBean(DepartmentMapper.class); 14 | List departmentList = departmentMapper.findAll(); 15 | 16 | // Department department = new Department(); 17 | // department.setName("test encapsulate"); 18 | // department.setTel("12345"); 19 | // departmentMapper.insert(department); 20 | 21 | Department department = departmentList.get(departmentList.size() - 1); 22 | department.setTel("54321"); 23 | departmentMapper.update(department); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /mybatis-05-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 | -------------------------------------------------------------------------------- /mybatis-06-transaction/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 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 8 | 9 | 10 | 23 | 33 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/app/InterceptorApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class InterceptorApplication { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println(department); 22 | /* 23 | department.setName("技术开发部"); 24 | departmentMapper.update(department); 25 | 26 | sqlSession.commit(); 27 | sqlSession.close(); 28 | */ 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/provider/Insert.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.metadata.FieldMetadata; 5 | import org.apache.ibatis.jdbc.SQL; 6 | import org.apache.ibatis.reflection.SystemMetaObject; 7 | 8 | import java.util.List; 9 | 10 | public class Insert extends AbstractProvider { 11 | 12 | @Override 13 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 14 | String tablename = entityMetadata.getTablename(); 15 | List fieldMetadatas = entityMetadata.getFields(); 16 | String[] columns = fieldMetadatas.stream().map(FieldMetadata::getColumnName).toArray(String[]::new); 17 | String[] fields = fieldMetadatas.stream().map(fm -> "#{" + fm.getFieldName() + "}").toArray(String[]::new); 18 | 19 | SystemMetaObject.forObject(params).setValue(entityMetadata.getIdField(), entityMetadata.getIdGenerator().next()); 20 | 21 | SQL sql = new SQL(); 22 | sql.INSERT_INTO(tablename).INTO_COLUMNS(columns).INTO_VALUES(fields); 23 | return sql.toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/Level1Application.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 9 | import java.io.InputStream; 10 | 11 | public class Level1Application { 12 | 13 | public static void main(String[] args) throws Exception { 14 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 15 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 16 | SqlSession sqlSession = sqlSessionFactory.openSession(); 17 | 18 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 19 | System.out.println("第一次执行findAll......"); 20 | departmentMapper.findAll(); 21 | System.out.println("第二次执行findAll......"); 22 | departmentMapper.findAll(); 23 | System.out.println("清空一级缓存......"); 24 | departmentMapper.cleanCache(); 25 | System.out.println("清空缓存后再次执行findAll......"); 26 | departmentMapper.findAll(); 27 | 28 | sqlSession.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/BaseMapper.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.provider.Delete; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.provider.FindAll; 5 | import com.linkedbear.mybatis.encapsulate.c_extra.provider.FindByExample; 6 | import com.linkedbear.mybatis.encapsulate.c_extra.provider.Insert; 7 | import com.linkedbear.mybatis.encapsulate.c_extra.provider.Update; 8 | import org.apache.ibatis.annotations.DeleteProvider; 9 | import org.apache.ibatis.annotations.InsertProvider; 10 | import org.apache.ibatis.annotations.SelectProvider; 11 | import org.apache.ibatis.annotations.UpdateProvider; 12 | 13 | import java.util.List; 14 | 15 | public interface BaseMapper { 16 | 17 | @InsertProvider(type = Insert.class, method = "invoke") 18 | int insert(T entity); 19 | 20 | @UpdateProvider(type = Update.class, method = "invoke") 21 | int update(T entity); 22 | 23 | @DeleteProvider(type = Delete.class, method = "invoke") 24 | int delete(T entity); 25 | 26 | @SelectProvider(type = FindAll.class, method = "invoke") 27 | List findAll(); 28 | 29 | @SelectProvider(type = FindByExample.class, method = "invoke") 30 | List findByExample(T example); 31 | } 32 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/util/StringConvertUtils.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.util; 2 | 3 | public abstract class StringConvertUtils { 4 | 5 | public static String underscoreToCamelCase(String source) { 6 | if (!source.contains("_")) { 7 | return source; 8 | } 9 | char[] chars = source.toCharArray(); 10 | StringBuilder sb = new StringBuilder(); 11 | boolean nextUpper = false; 12 | for (char c : chars) { 13 | if (c == '_') { 14 | nextUpper = true; 15 | } else if (nextUpper) { 16 | sb.append(Character.toUpperCase(c)); 17 | nextUpper = false; 18 | } else { 19 | sb.append(c); 20 | } 21 | } 22 | return sb.toString(); 23 | } 24 | 25 | public static String camelCaseToUnderscore(String source) { 26 | char[] chars = source.toCharArray(); 27 | StringBuilder sb = new StringBuilder(); 28 | for (char c : chars) { 29 | if (Character.isUpperCase(c)) { 30 | sb.append('_').append(Character.toLowerCase(c)); 31 | } else { 32 | sb.append(c); 33 | } 34 | } 35 | return sb.toString(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/JdbcResultSetTypeApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | 8 | public class JdbcResultSetTypeApplication { 9 | 10 | public static void main(String[] args) throws Exception { 11 | Class.forName("com.mysql.jdbc.Driver"); 12 | Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "123456"); 13 | // PreparedStatement ps = connection.prepareStatement("select * from tbl_department"); 14 | PreparedStatement ps = connection.prepareStatement("select * from tbl_department", 15 | ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 16 | ResultSet resultSet = ps.executeQuery(); 17 | 18 | // 遍历游标向下迭代 19 | while (resultSet.next()) { 20 | System.out.println(resultSet.getString("name")); 21 | } 22 | 23 | // 遍历游标向上迭代 24 | while (resultSet.previous()) { 25 | System.out.println("倒序 --- " + resultSet.getString("name")); 26 | } 27 | 28 | resultSet.close(); 29 | ps.close(); 30 | connection.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/FindByExample.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.FieldMetadata; 5 | import org.apache.ibatis.jdbc.SQL; 6 | import org.apache.ibatis.reflection.MetaObject; 7 | import org.apache.ibatis.reflection.SystemMetaObject; 8 | 9 | public class FindByExample extends AbstractProvider { 10 | 11 | @Override 12 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 13 | SQL sql = new SQL(); 14 | sql.SELECT("*").FROM(entityMetadata.getTablename()); 15 | MetaObject metaObject = SystemMetaObject.forObject(params); 16 | if (entityMetadata.getLoginDeleteMetadata() != null) { 17 | FieldMetadata loginDeleteMetadata = entityMetadata.getLoginDeleteMetadata(); 18 | sql.WHERE(loginDeleteMetadata.getColumnName() + " = 0"); 19 | } 20 | entityMetadata.getFields().stream().filter(fm -> fm.getQuerySql() != null) 21 | .filter(fm -> metaObject.getValue(fm.getFieldName()) != null) 22 | .forEach(fm -> { 23 | sql.WHERE(fm.getQuerySql()); 24 | }); 25 | return sql.toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/plugin/CustomInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.plugin; 2 | 3 | import org.apache.ibatis.cache.CacheKey; 4 | import org.apache.ibatis.executor.Executor; 5 | import org.apache.ibatis.mapping.BoundSql; 6 | import org.apache.ibatis.mapping.MappedStatement; 7 | import org.apache.ibatis.plugin.Interceptor; 8 | import org.apache.ibatis.plugin.Intercepts; 9 | import org.apache.ibatis.plugin.Invocation; 10 | import org.apache.ibatis.plugin.Signature; 11 | import org.apache.ibatis.session.ResultHandler; 12 | import org.apache.ibatis.session.RowBounds; 13 | 14 | //@Intercepts(@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})) 15 | @Intercepts(@Signature(type = Executor.class, method = "query", 16 | args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})) 17 | public class CustomInterceptor implements Interceptor { 18 | 19 | @Override 20 | public Object intercept(Invocation invocation) throws Throwable { 21 | System.out.println("CustomInterceptor intercept run ......"); 22 | System.out.println(invocation.getTarget()); 23 | System.out.println(invocation.getMethod().getName()); 24 | System.out.println(invocation.getArgs()[1]); 25 | return invocation.proceed(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/metadata/FieldMetadata.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.metadata; 2 | 3 | public class FieldMetadata { 4 | 5 | private String fieldName; 6 | 7 | private String columnName; 8 | 9 | private boolean primary = false; 10 | 11 | private boolean updateIfNull = false; 12 | 13 | public FieldMetadata(String fieldName, String columnName) { 14 | this.fieldName = fieldName; 15 | this.columnName = columnName; 16 | } 17 | 18 | public String getFieldName() { 19 | return fieldName; 20 | } 21 | 22 | public void setFieldName(String fieldName) { 23 | this.fieldName = fieldName; 24 | } 25 | 26 | public String getColumnName() { 27 | return columnName; 28 | } 29 | 30 | public void setColumnName(String columnName) { 31 | this.columnName = columnName; 32 | } 33 | 34 | public boolean isPrimary() { 35 | return primary; 36 | } 37 | 38 | public void setPrimary(boolean primary) { 39 | this.primary = primary; 40 | } 41 | 42 | public boolean isUpdateIfNull() { 43 | return updateIfNull; 44 | } 45 | 46 | public void setUpdateIfNull(boolean updateIfNull) { 47 | this.updateIfNull = updateIfNull; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /mybatis-04-annotation/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 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/app/LoadPropertiesApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.Properties; 12 | 13 | public class LoadPropertiesApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | Properties properties = new Properties(); 18 | properties.load(Resources.getResourceAsStream("jdbc1.properties")); 19 | properties.load(Resources.getResourceAsStream("jdbc2.properties")); 20 | 21 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml, properties); 22 | SqlSession sqlSession = sqlSessionFactory.openSession(); 23 | 24 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 25 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 26 | System.out.println(department); 27 | System.out.println(department.getUsers()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/Level2Application.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 9 | import java.io.InputStream; 10 | 11 | public class Level2Application { 12 | 13 | public static void main(String[] args) throws Exception { 14 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 15 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 16 | SqlSession sqlSession = sqlSessionFactory.openSession(); 17 | 18 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 19 | System.out.println("第一次执行findAll......"); 20 | departmentMapper.findAll(); 21 | System.out.println("第二次执行findAll......"); 22 | departmentMapper.findAll(); 23 | 24 | sqlSession.close(); 25 | 26 | SqlSession sqlSession2 = sqlSessionFactory.openSession(); 27 | DepartmentMapper departmentMapper2 = sqlSession2.getMapper(DepartmentMapper.class); 28 | System.out.println("sqlSession2执行findAll......"); 29 | departmentMapper2.findAll(); 30 | 31 | sqlSession2.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/Level1ReferenceApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | 12 | public class Level1ReferenceApplication { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 20 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 21 | System.out.println("department: " + department); 22 | department.setName("哈哈哈哈"); 23 | System.out.println("department: " + department); 24 | 25 | Department department2 = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 26 | System.out.println("department2: " + department2); 27 | System.out.println(department == department2); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mapper/department3.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel}) 16 | 17 | 18 | 19 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 20 | 21 | 22 | 23 | delete from tbl_department where id = #{id} 24 | 25 | 26 | 29 | 30 | 33 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/handler/DepartmentTypeHandler.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.handler; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import org.apache.ibatis.type.JdbcType; 5 | import org.apache.ibatis.type.TypeHandler; 6 | 7 | import java.sql.CallableStatement; 8 | import java.sql.PreparedStatement; 9 | import java.sql.ResultSet; 10 | import java.sql.SQLException; 11 | 12 | public class DepartmentTypeHandler implements TypeHandler { 13 | 14 | @Override 15 | public void setParameter(PreparedStatement ps, int i, Department department, JdbcType jdbcType) throws SQLException { 16 | ps.setString(i, department.getId()); 17 | } 18 | 19 | @Override 20 | public Department getResult(ResultSet rs, String columnName) throws SQLException { 21 | Department department = new Department(); 22 | department.setId(rs.getString(columnName)); 23 | return department; 24 | } 25 | 26 | @Override 27 | public Department getResult(ResultSet rs, int columnIndex) throws SQLException { 28 | Department department = new Department(); 29 | department.setId(rs.getString(columnIndex)); 30 | return department; 31 | } 32 | 33 | @Override 34 | public Department getResult(CallableStatement cs, int columnIndex) throws SQLException { 35 | Department department = new Department(); 36 | department.setId(cs.getString(columnIndex)); 37 | return department; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | public class Department implements Serializable { 7 | private static final long serialVersionUID = -2062845216604443970L; 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | @Override 16 | public String toString() { 17 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) 23 | return true; 24 | if (o == null || getClass() != o.getClass()) 25 | return false; 26 | Department that = (Department) o; 27 | return Objects.equals(id, that.id); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return Objects.hash(id); 33 | } 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public String getTel() { 52 | return tel; 53 | } 54 | 55 | public void setTel(String tel) { 56 | this.tel = tel; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | public class Department implements Serializable { 7 | private static final long serialVersionUID = -2062845216604443970L; 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | @Override 16 | public String toString() { 17 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) 23 | return true; 24 | if (o == null || getClass() != o.getClass()) 25 | return false; 26 | Department that = (Department) o; 27 | return Objects.equals(id, that.id); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return Objects.hash(id); 33 | } 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public String getTel() { 52 | return tel; 53 | } 54 | 55 | public void setTel(String tel) { 56 | this.tel = tel; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | public class Department implements Serializable { 7 | private static final long serialVersionUID = -2062845216604443970L; 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | @Override 16 | public String toString() { 17 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) 23 | return true; 24 | if (o == null || getClass() != o.getClass()) 25 | return false; 26 | Department that = (Department) o; 27 | return Objects.equals(id, that.id); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return Objects.hash(id); 33 | } 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public String getTel() { 52 | return tel; 53 | } 54 | 55 | public void setTel(String tel) { 56 | this.tel = tel; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | public class Department implements Serializable { 7 | private static final long serialVersionUID = -2062845216604443970L; 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | @Override 16 | public String toString() { 17 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) 23 | return true; 24 | if (o == null || getClass() != o.getClass()) 25 | return false; 26 | Department that = (Department) o; 27 | return Objects.equals(id, that.id); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return Objects.hash(id); 33 | } 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public String getTel() { 52 | return tel; 53 | } 54 | 55 | public void setTel(String tel) { 56 | this.tel = tel; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | 6 | public class Department implements Serializable { 7 | private static final long serialVersionUID = -2062845216604443970L; 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | @Override 16 | public String toString() { 17 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object o) { 22 | if (this == o) 23 | return true; 24 | if (o == null || getClass() != o.getClass()) 25 | return false; 26 | Department that = (Department) o; 27 | return Objects.equals(id, that.id); 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | return Objects.hash(id); 33 | } 34 | 35 | public String getId() { 36 | return id; 37 | } 38 | 39 | public void setId(String id) { 40 | this.id = id; 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public String getTel() { 52 | return tel; 53 | } 54 | 55 | public void setTel(String tel) { 56 | this.tel = tel; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/app/DepartmentTypeHandlerApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import com.linkedbear.mybatis.mapper.UserMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class DepartmentTypeHandlerApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | 20 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 21 | List userList = userMapper.findAllUseTypeHandler(); 22 | userList.forEach(System.out::println); 23 | 24 | // User user = new User(); 25 | // user.setId(UUID.randomUUID().toString().replaceAll("-", "")); 26 | // user.setName("hahahaha"); 27 | // Department department = new Department(); 28 | // department.setId("18ec781fbefd727923b0d35740b177ab"); 29 | // user.setDepartment(department); 30 | // userMapper.saveUser(user); 31 | // 32 | // sqlSession.commit(); 33 | // sqlSession.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel}) 16 | 17 | 18 | 19 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 20 | 21 | 22 | 23 | delete from tbl_department where id = #{id} 24 | 25 | 26 | 29 | 30 | 33 | -------------------------------------------------------------------------------- /mybatis-07-extra/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-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/Insert.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.FieldMetadata; 5 | import org.apache.ibatis.jdbc.SQL; 6 | import org.apache.ibatis.reflection.SystemMetaObject; 7 | 8 | import java.util.List; 9 | 10 | public class Insert extends AbstractProvider { 11 | 12 | @Override 13 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 14 | String tablename = entityMetadata.getTablename(); 15 | List fieldMetadatas = entityMetadata.getFields(); 16 | String[] columns = fieldMetadatas.stream() 17 | .filter(fm -> !fm.getColumnName().equals(entityMetadata.getLoginDeleteMetadata().getColumnName())) 18 | .map(FieldMetadata::getColumnName).toArray(String[]::new); 19 | String[] fields = fieldMetadatas.stream() 20 | .filter(fm -> !fm.getColumnName().equals(entityMetadata.getLoginDeleteMetadata().getColumnName())) 21 | .map(fm -> "#{" + fm.getFieldName() + "}").toArray(String[]::new); 22 | 23 | SystemMetaObject.forObject(params).setValue(entityMetadata.getIdMetadata().getFieldName(), entityMetadata.getIdGenerator().next()); 24 | 25 | SQL sql = new SQL(); 26 | sql.INSERT_INTO(tablename).INTO_COLUMNS(columns).INTO_VALUES(fields); 27 | return sql.toString(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/app/OpenSessionAutoCommitApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | public class OpenSessionAutoCommitApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | SqlSession sqlSession = sqlSessionFactory.openSession(); 19 | SqlSession sqlSession2 = sqlSessionFactory.openSession(); 20 | 21 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 22 | DepartmentMapper departmentMapper2 = sqlSession2.getMapper(DepartmentMapper.class); 23 | 24 | Department department = departmentMapper2.findById("53e3803ebbf4f97968e0253e5ad4cc83"); 25 | department.setName("测试部部"); 26 | departmentMapper2.update(department); 27 | 28 | List departmentList = departmentMapper.findAll(); 29 | departmentList.forEach(System.out::println); 30 | 31 | sqlSession.close(); 32 | sqlSession2.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/ResultMapApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.entity.User; 5 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 6 | import com.linkedbear.mybatis.mapper.UserMapper; 7 | import org.apache.ibatis.io.Resources; 8 | import org.apache.ibatis.session.SqlSession; 9 | import org.apache.ibatis.session.SqlSessionFactory; 10 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 | 12 | import java.io.InputStream; 13 | import java.util.List; 14 | 15 | public class ResultMapApplication { 16 | 17 | public static void main(String[] args) throws Exception { 18 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 19 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 20 | SqlSession sqlSession = sqlSessionFactory.openSession(); 21 | 22 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 23 | List departmentList = departmentMapper.findAll(); 24 | departmentList.forEach(System.out::println); 25 | 26 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 27 | List userList = userMapper.findAll(); 28 | userList.forEach(System.out::println); 29 | 30 | List userList2 = sqlSession.selectList("com.linkedbear.mybatis.mapper.UserMapper.findAllUseDiscriminator"); 31 | userList2.forEach(System.out::println); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/metadata/FieldMetadata.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.metadata; 2 | 3 | public class FieldMetadata { 4 | 5 | private String fieldName; 6 | 7 | private String columnName; 8 | 9 | private boolean primary = false; 10 | 11 | private boolean updateIfNull = false; 12 | 13 | private String querySql; 14 | 15 | public FieldMetadata(String fieldName, String columnName) { 16 | this.fieldName = fieldName; 17 | this.columnName = columnName; 18 | } 19 | 20 | public String getFieldName() { 21 | return fieldName; 22 | } 23 | 24 | public void setFieldName(String fieldName) { 25 | this.fieldName = fieldName; 26 | } 27 | 28 | public String getColumnName() { 29 | return columnName; 30 | } 31 | 32 | public void setColumnName(String columnName) { 33 | this.columnName = columnName; 34 | } 35 | 36 | public boolean isPrimary() { 37 | return primary; 38 | } 39 | 40 | public void setPrimary(boolean primary) { 41 | this.primary = primary; 42 | } 43 | 44 | public boolean isUpdateIfNull() { 45 | return updateIfNull; 46 | } 47 | 48 | public void setUpdateIfNull(boolean updateIfNull) { 49 | this.updateIfNull = updateIfNull; 50 | } 51 | 52 | public String getQuerySql() { 53 | return querySql; 54 | } 55 | 56 | public void setQuerySql(String querySql) { 57 | this.querySql = querySql; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /mybatis-01-basic/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-01-basic 9 | 1.0-RELEASE 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | log4j 25 | log4j 26 | 1.2.17 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 3.8.1 36 | 37 | 1.8 38 | 1.8 39 | UTF-8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /mybatis-07-extra/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-07-extra 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | log4j 25 | log4j 26 | 1.2.17 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 3.8.1 36 | 37 | 1.8 38 | 1.8 39 | UTF-8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /mybatis-04-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-04-annotation 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | log4j 25 | log4j 26 | 1.2.17 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 3.8.1 36 | 37 | 1.8 38 | 1.8 39 | UTF-8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /mybatis-06-transaction/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-06-transaction 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | log4j 25 | log4j 26 | 1.2.17 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-compiler-plugin 35 | 3.8.1 36 | 37 | 1.8 38 | 1.8 39 | UTF-8 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/resources/mapper/department.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel}) 20 | 21 | 22 | 23 | update tbl_department set name = #{name}, tel = #{tel} where id = #{id} 24 | 25 | 26 | 27 | delete from tbl_department where id = #{id} 28 | 29 | 30 | 33 | 34 | 37 | 38 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | import java.util.Set; 6 | 7 | public class Department implements Serializable { 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | private Set users; 16 | 17 | @Override 18 | public String toString() { 19 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) 25 | return true; 26 | if (o == null || getClass() != o.getClass()) 27 | return false; 28 | Department that = (Department) o; 29 | return Objects.equals(id, that.id); 30 | } 31 | 32 | @Override 33 | public int hashCode() { 34 | return Objects.hash(id); 35 | } 36 | 37 | public String getId() { 38 | return id; 39 | } 40 | 41 | public void setId(String id) { 42 | this.id = id; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public String getTel() { 54 | return tel; 55 | } 56 | 57 | public void setTel(String tel) { 58 | this.tel = tel; 59 | } 60 | 61 | public Set getUsers() { 62 | return users; 63 | } 64 | 65 | public void setUsers(Set users) { 66 | this.users = users; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Objects; 5 | import java.util.Set; 6 | 7 | public class Department implements Serializable { 8 | 9 | private String id; 10 | 11 | private String name; 12 | 13 | private String tel; 14 | 15 | private Set users; 16 | 17 | @Override 18 | public String toString() { 19 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) 25 | return true; 26 | if (o == null || getClass() != o.getClass()) 27 | return false; 28 | Department that = (Department) o; 29 | return Objects.equals(id, that.id); 30 | } 31 | 32 | @Override 33 | public int hashCode() { 34 | return Objects.hash(id); 35 | } 36 | 37 | public String getId() { 38 | return id; 39 | } 40 | 41 | public void setId(String id) { 42 | this.id = id; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public String getTel() { 54 | return tel; 55 | } 56 | 57 | public void setTel(String tel) { 58 | this.tel = tel; 59 | } 60 | 61 | public Set getUsers() { 62 | return users; 63 | } 64 | 65 | public void setUsers(Set users) { 66 | this.users = users; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/DynamicSqlApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import org.apache.ibatis.io.Resources; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 | 8 | import java.io.InputStream; 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | public class DynamicSqlApplication { 13 | 14 | public static void main(String[] args) throws Exception { 15 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 16 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 17 | SqlSession sqlSession = sqlSessionFactory.openSession(); 18 | 19 | // Department department = new Department(); 20 | // department.setName("产品"); 21 | // List departmentList = sqlSession.selectList("dynamic.findAllDepartmentUseBind", department); 22 | // departmentList.forEach(System.out::println); 23 | 24 | // department.setName("测试部"); 25 | // department.setTel("12345679"); 26 | // sqlSession.update("dynamic.updateDepartment", department); 27 | 28 | // BeanMap beanMap = BeanMap.create(department); 29 | // Map departmentMap = new HashMap<>(2); 30 | // departmentMap.put("id", "53e3803ebbf4f97968e0253e5ad4cc83"); 31 | // departmentMap.put("beanMap", beanMap); 32 | // sqlSession.update("dynamic.updateDepartmentByMap", departmentMap); 33 | 34 | List list = sqlSession.selectList("dynamic.findAllUseSql", Collections.emptyMap()); 35 | list.forEach(System.out::println); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/app/EncapsulateApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.app; 2 | 3 | import com.github.pagehelper.PageHelper; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.DepartmentMapper; 5 | import com.linkedbear.mybatis.encapsulate.c_extra.entity.Department; 6 | import org.springframework.context.support.ClassPathXmlApplicationContext; 7 | 8 | import java.util.List; 9 | 10 | public class EncapsulateApplication { 11 | 12 | public static void main(String[] args) throws Exception { 13 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("c_extra/spring-mybatis.xml"); 14 | DepartmentMapper departmentMapper = ctx.getBean(DepartmentMapper.class); 15 | // List departmentList = departmentMapper.findAll(); 16 | 17 | // Department department = new Department(); 18 | // department.setName("test encapsulate"); 19 | // department.setTel("12345"); 20 | // departmentMapper.insert(department); 21 | 22 | // Department department = departmentList.get(departmentList.size() - 1); 23 | // departmentMapper.delete(department); 24 | 25 | // Department department = departmentList.get(departmentList.size() - 1); 26 | // department.setTel("54321111"); 27 | // departmentMapper.update(department); 28 | 29 | // Department example = new Department(); 30 | // example.setId("18ec781fbefd727923b0d35740b177ab"); 31 | // example.setName("部"); 32 | // departmentMapper.findByExample(example); 33 | 34 | PageHelper.startPage(1, 2); 35 | List departmentList = departmentMapper.findAll(); 36 | System.out.println(departmentList); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /mybatis-02-configuration/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-02-configuration 9 | 1.0-RELEASE 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | org.postgresql 24 | postgresql 25 | 42.2.18 26 | 27 | 28 | 29 | log4j 30 | log4j 31 | 1.2.17 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-compiler-plugin 40 | 3.8.1 41 | 42 | 1.8 43 | 1.8 44 | UTF-8 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /mybatis-05-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-05-cache 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | org.mybatis.caches 25 | mybatis-ehcache 26 | 1.2.1 27 | 28 | 29 | 30 | log4j 31 | log4j 32 | 1.2.17 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.apache.maven.plugins 40 | maven-compiler-plugin 41 | 3.8.1 42 | 43 | 1.8 44 | 1.8 45 | UTF-8 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/provider/Update.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.provider; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.EntityMetadata; 4 | import com.linkedbear.mybatis.encapsulate.c_extra.metadata.FieldMetadata; 5 | import org.apache.ibatis.jdbc.SQL; 6 | import org.apache.ibatis.reflection.MetaObject; 7 | import org.apache.ibatis.reflection.SystemMetaObject; 8 | 9 | public class Update extends AbstractProvider { 10 | 11 | @Override 12 | protected String buildSql(EntityMetadata entityMetadata, Object params) { 13 | FieldMetadata idMetadata = entityMetadata.getIdMetadata(); 14 | FieldMetadata versionMetadata = entityMetadata.getVersionMetadata(); 15 | MetaObject metaObject = SystemMetaObject.forObject(params); 16 | int currentVersion = (Integer) metaObject.getValue(versionMetadata.getFieldName()); 17 | if (versionMetadata != null) { 18 | // 创建下一个version 19 | int nextVersion = currentVersion + 1; 20 | metaObject.setValue(versionMetadata.getFieldName(), nextVersion); 21 | } 22 | 23 | SQL sql = new SQL(); 24 | sql.UPDATE(entityMetadata.getTablename()); 25 | entityMetadata.getFields().stream().filter(fm -> !fm.isPrimary()) 26 | // 过滤掉null的属性 27 | .filter(fm -> !fm.isUpdateIfNull() && metaObject.getValue(fm.getFieldName()) != null) 28 | .forEach(fm -> { 29 | sql.SET(fm.getColumnName() + " = #{" + fm.getFieldName() + "}"); 30 | }); 31 | sql.WHERE(idMetadata.getColumnName() + " = #{" + idMetadata.getFieldName() + "}"); 32 | if (versionMetadata != null) { 33 | sql.WHERE(versionMetadata.getColumnName() + " = " + currentVersion); 34 | } 35 | return sql.toString(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/resources/mapper/user.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 31 | 32 | 35 | 36 | 39 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/Level2ReadonlyApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.entity.User; 5 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 6 | import com.linkedbear.mybatis.mapper.UserMapper; 7 | import org.apache.ibatis.io.Resources; 8 | import org.apache.ibatis.session.SqlSession; 9 | import org.apache.ibatis.session.SqlSessionFactory; 10 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 | 12 | import java.io.InputStream; 13 | import java.util.List; 14 | 15 | public class Level2ReadonlyApplication { 16 | 17 | public static void main(String[] args) throws Exception { 18 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 19 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 20 | SqlSession sqlSession = sqlSessionFactory.openSession(); 21 | 22 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 23 | System.out.println("sqlSession执行findAll......"); 24 | List departmentList = departmentMapper.findAll(); 25 | 26 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 27 | List userList = userMapper.findAll(); 28 | 29 | sqlSession.close(); 30 | 31 | SqlSession sqlSession2 = sqlSessionFactory.openSession(); 32 | DepartmentMapper departmentMapper2 = sqlSession2.getMapper(DepartmentMapper.class); 33 | System.out.println("sqlSession2执行findAll......"); 34 | List departmentList2 = departmentMapper2.findAll(); 35 | 36 | UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class); 37 | List userList2 = userMapper2.findAll(); 38 | 39 | sqlSession2.close(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /mybatis-01-basic/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.util.Date; 4 | import java.util.Objects; 5 | 6 | public class User { 7 | 8 | private String id; 9 | 10 | private String name; 11 | 12 | private Integer age; 13 | 14 | private Date birthday; 15 | 16 | private Department department; 17 | 18 | @Override 19 | public String toString() { 20 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 21 | } 22 | 23 | @Override 24 | public boolean equals(Object o) { 25 | if (this == o) 26 | return true; 27 | if (o == null || getClass() != o.getClass()) 28 | return false; 29 | User user = (User) o; 30 | return Objects.equals(id, user.id); 31 | } 32 | 33 | @Override 34 | public int hashCode() { 35 | return Objects.hash(id); 36 | } 37 | 38 | public String getId() { 39 | return id; 40 | } 41 | 42 | public void setId(String id) { 43 | this.id = id; 44 | } 45 | 46 | public String getName() { 47 | return name; 48 | } 49 | 50 | public void setName(String name) { 51 | this.name = name; 52 | } 53 | 54 | public Integer getAge() { 55 | return age; 56 | } 57 | 58 | public void setAge(Integer age) { 59 | this.age = age; 60 | } 61 | 62 | public Date getBirthday() { 63 | return birthday; 64 | } 65 | 66 | public void setBirthday(Date birthday) { 67 | this.birthday = birthday; 68 | } 69 | 70 | public Department getDepartment() { 71 | return department; 72 | } 73 | 74 | public void setDepartment(Department department) { 75 | this.department = department; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/UpdateForeachApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import net.sf.cglib.beans.BeanMap; 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 | 10 | import java.io.InputStream; 11 | import java.util.HashMap; 12 | import java.util.Iterator; 13 | import java.util.Map; 14 | 15 | public class UpdateForeachApplication { 16 | 17 | public static void main(String[] args) throws Exception { 18 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 19 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 20 | SqlSession sqlSession = sqlSessionFactory.openSession(); 21 | 22 | Department department = new Department(); 23 | department.setId("53e3803ebbf4f97968e0253e5ad4cc83"); 24 | department.setName("测试部"); 25 | department.setTel(""); 26 | 27 | BeanMap beanMap = BeanMap.create(department); 28 | System.out.println(beanMap); 29 | Map departmentMap = new HashMap<>(beanMap); 30 | Iterator> entryIterator = departmentMap.entrySet().iterator(); 31 | while (entryIterator.hasNext()) { 32 | Map.Entry entry = entryIterator.next(); 33 | if (entry.getValue() == null) { 34 | entryIterator.remove(); 35 | } else if (entry.getValue() instanceof String && entry.getValue().toString().trim().isEmpty()) { 36 | entryIterator.remove(); 37 | } 38 | } 39 | System.out.println(departmentMap); 40 | // sqlSession.update("dynamic.updateDepartmentByMap", departmentMap); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/app/ProviderMapperApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.mapper.UserAnnotationMapper; 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 | 9 | import java.io.InputStream; 10 | 11 | public class ProviderMapperApplication { 12 | 13 | public static void main(String[] args) throws Exception { 14 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 15 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 16 | SqlSession sqlSession = sqlSessionFactory.openSession(); 17 | 18 | UserAnnotationMapper userMapper = sqlSession.getMapper(UserAnnotationMapper.class); 19 | 20 | /* 21 | List userList = userMapper.findAll(); 22 | userList.forEach(System.out::println); 23 | 24 | User example = new User(); 25 | example.setName("狗"); 26 | List userByExampleList = userMapper.findAllByExample(example); 27 | userByExampleList.forEach(System.out::println); 28 | */ 29 | 30 | /* 31 | User user = new User(); 32 | user.setName("阿熊哇哇"); 33 | user.setAge(3); 34 | Department department = new Department(); 35 | department.setId("18ec781fbefd727923b0d35740b177ab"); 36 | user.setDepartment(department); 37 | userMapper.save(user); 38 | 39 | User user = new User(); 40 | user.setId("fa3fdb1bfd84407c9df5eeedbec65952"); 41 | user.setName("阿熊哈哈"); 42 | userMapper.updateByExample(user); 43 | */ 44 | 45 | userMapper.deleteById("fa3fdb1bfd84407c9df5eeedbec65952"); 46 | 47 | sqlSession.commit(); 48 | sqlSession.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/metadata/EntityMetadata.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.strategy.IdGenerator; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class EntityMetadata { 9 | 10 | private Class entityClass; 11 | 12 | private String tablename; 13 | 14 | private String idField; 15 | 16 | private String idColumn; 17 | 18 | private IdGenerator idGenerator; 19 | 20 | private List fields = new ArrayList<>(); 21 | 22 | public EntityMetadata(Class entityClass) { 23 | this.entityClass = entityClass; 24 | } 25 | 26 | public Class getEntityClass() { 27 | return entityClass; 28 | } 29 | 30 | public void setEntityClass(Class entityClass) { 31 | this.entityClass = entityClass; 32 | } 33 | 34 | public String getTablename() { 35 | return tablename; 36 | } 37 | 38 | public void setTablename(String tablename) { 39 | this.tablename = tablename; 40 | } 41 | 42 | public String getIdField() { 43 | return idField; 44 | } 45 | 46 | public void setIdField(String idField) { 47 | this.idField = idField; 48 | } 49 | 50 | public String getIdColumn() { 51 | return idColumn; 52 | } 53 | 54 | public void setIdColumn(String idColumn) { 55 | this.idColumn = idColumn; 56 | } 57 | 58 | public IdGenerator getIdGenerator() { 59 | return idGenerator; 60 | } 61 | 62 | public void setIdGenerator(IdGenerator idGenerator) { 63 | this.idGenerator = idGenerator; 64 | } 65 | 66 | public List getFields() { 67 | return fields; 68 | } 69 | 70 | public void setFields(List fields) { 71 | this.fields = fields; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mybatis-07-extra/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mybatis-06-transaction/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | 5 | import java.io.Serializable; 6 | import java.util.Objects; 7 | import java.util.Set; 8 | 9 | public class Department implements Serializable { 10 | private static final long serialVersionUID = -2062845216604443970L; 11 | 12 | private String id; 13 | 14 | private String name; 15 | 16 | private String tel; 17 | 18 | private Set users; 19 | 20 | public Department() { 21 | } 22 | 23 | public Department(@Param("idd") String id) { 24 | this.id = id; 25 | } 26 | 27 | // public Department(String name) { 28 | // this.name = name; 29 | // } 30 | 31 | @Override 32 | public String toString() { 33 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 34 | } 35 | 36 | @Override 37 | public boolean equals(Object o) { 38 | if (this == o) 39 | return true; 40 | if (o == null || getClass() != o.getClass()) 41 | return false; 42 | Department that = (Department) o; 43 | return Objects.equals(id, that.id); 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return Objects.hash(id); 49 | } 50 | 51 | public String getId() { 52 | return id; 53 | } 54 | 55 | public void setId(String id) { 56 | this.id = id; 57 | } 58 | 59 | public String getName() { 60 | return name; 61 | } 62 | 63 | public void setName(String name) { 64 | this.name = name; 65 | } 66 | 67 | public String getTel() { 68 | return tel; 69 | } 70 | 71 | public void setTel(String tel) { 72 | this.tel = tel; 73 | } 74 | 75 | public Set getUsers() { 76 | return users; 77 | } 78 | 79 | public void setUsers(Set users) { 80 | this.users = users; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | 5 | import java.io.Serializable; 6 | import java.util.Objects; 7 | import java.util.Set; 8 | 9 | public class Department implements Serializable { 10 | private static final long serialVersionUID = -2062845216604443970L; 11 | 12 | private String id; 13 | 14 | private String name; 15 | 16 | private String tel; 17 | 18 | private Set users; 19 | 20 | public Department() { 21 | } 22 | 23 | public Department(@Param("idd") String id) { 24 | this.id = id; 25 | } 26 | 27 | // public Department(String name) { 28 | // this.name = name; 29 | // } 30 | 31 | @Override 32 | public String toString() { 33 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 34 | } 35 | 36 | @Override 37 | public boolean equals(Object o) { 38 | if (this == o) 39 | return true; 40 | if (o == null || getClass() != o.getClass()) 41 | return false; 42 | Department that = (Department) o; 43 | return Objects.equals(id, that.id); 44 | } 45 | 46 | @Override 47 | public int hashCode() { 48 | return Objects.hash(id); 49 | } 50 | 51 | public String getId() { 52 | return id; 53 | } 54 | 55 | public void setId(String id) { 56 | this.id = id; 57 | } 58 | 59 | public String getName() { 60 | return name; 61 | } 62 | 63 | public void setName(String name) { 64 | this.name = name; 65 | } 66 | 67 | public String getTel() { 68 | return tel; 69 | } 70 | 71 | public void setTel(String tel) { 72 | this.tel = tel; 73 | } 74 | 75 | public Set getUsers() { 76 | return users; 77 | } 78 | 79 | public void setUsers(Set users) { 80 | this.users = users; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/a_provider/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.a_provider.entity; 2 | 3 | import com.linkedbear.mybatis.encapsulate.a_provider.annotation.Id; 4 | import com.linkedbear.mybatis.encapsulate.a_provider.annotation.Table; 5 | import com.linkedbear.mybatis.encapsulate.a_provider.annotation.Transient; 6 | 7 | import java.io.Serializable; 8 | import java.util.Objects; 9 | 10 | @Table("tbl_department") 11 | public class Department implements Serializable { 12 | private static final long serialVersionUID = -2062845216604443970L; 13 | 14 | @Id 15 | private String id; 16 | 17 | private String name; 18 | 19 | private String tel; 20 | 21 | @Transient 22 | private String redundant; 23 | 24 | @Override 25 | public String toString() { 26 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 27 | } 28 | 29 | @Override 30 | public boolean equals(Object o) { 31 | if (this == o) 32 | return true; 33 | if (o == null || getClass() != o.getClass()) 34 | return false; 35 | Department that = (Department) o; 36 | return Objects.equals(id, that.id); 37 | } 38 | 39 | @Override 40 | public int hashCode() { 41 | return Objects.hash(id); 42 | } 43 | 44 | public String getId() { 45 | return id; 46 | } 47 | 48 | public void setId(String id) { 49 | this.id = id; 50 | } 51 | 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | public void setName(String name) { 57 | this.name = name; 58 | } 59 | 60 | public String getTel() { 61 | return tel; 62 | } 63 | 64 | public void setTel(String tel) { 65 | this.tel = tel; 66 | } 67 | 68 | public String getRedundant() { 69 | return redundant; 70 | } 71 | 72 | public void setRedundant(String redundant) { 73 | this.redundant = redundant; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/b_metadata/entity/Department.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.b_metadata.entity; 2 | 3 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.Id; 4 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.Table; 5 | import com.linkedbear.mybatis.encapsulate.b_metadata.annotation.Transient; 6 | 7 | import java.io.Serializable; 8 | import java.util.Objects; 9 | 10 | @Table("tbl_department") 11 | public class Department implements Serializable { 12 | private static final long serialVersionUID = -2062845216604443970L; 13 | 14 | @Id 15 | private String id; 16 | 17 | private String name; 18 | 19 | private String tel; 20 | 21 | @Transient 22 | private String redundant; 23 | 24 | @Override 25 | public String toString() { 26 | return "Department{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", tel='" + tel + '\'' + '}'; 27 | } 28 | 29 | @Override 30 | public boolean equals(Object o) { 31 | if (this == o) 32 | return true; 33 | if (o == null || getClass() != o.getClass()) 34 | return false; 35 | Department that = (Department) o; 36 | return Objects.equals(id, that.id); 37 | } 38 | 39 | @Override 40 | public int hashCode() { 41 | return Objects.hash(id); 42 | } 43 | 44 | public String getId() { 45 | return id; 46 | } 47 | 48 | public void setId(String id) { 49 | this.id = id; 50 | } 51 | 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | public void setName(String name) { 57 | this.name = name; 58 | } 59 | 60 | public String getTel() { 61 | return tel; 62 | } 63 | 64 | public void setTel(String tel) { 65 | this.tel = tel; 66 | } 67 | 68 | public String getRedundant() { 69 | return redundant; 70 | } 71 | 72 | public void setRedundant(String redundant) { 73 | this.redundant = redundant; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/app/SelectUseCacheApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.entity.Department; 4 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 5 | import com.linkedbear.mybatis.mapper.UserMapper; 6 | import org.apache.ibatis.io.Resources; 7 | import org.apache.ibatis.session.SqlSession; 8 | import org.apache.ibatis.session.SqlSessionFactory; 9 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 | 11 | import java.io.InputStream; 12 | 13 | public class SelectUseCacheApplication { 14 | 15 | public static void main(String[] args) throws Exception { 16 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 17 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 18 | 19 | SqlSession sqlSession = sqlSessionFactory.openSession(); 20 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 21 | Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 22 | System.out.println(department); 23 | Department department2 = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"); 24 | System.out.println("department == department2 : " + (department == department2)); 25 | sqlSession.close(); 26 | 27 | SqlSession sqlSession2 = sqlSessionFactory.openSession(); 28 | DepartmentMapper departmentMapper2 = sqlSession2.getMapper(DepartmentMapper.class); 29 | Department department3 = departmentMapper2.findById("18ec781fbefd727923b0d35740b177ab"); 30 | departmentMapper2.findAll(); 31 | 32 | UserMapper userMapper = sqlSession2.getMapper(UserMapper.class); 33 | userMapper.cleanCache(); 34 | System.out.println("==================cleanCache===================="); 35 | 36 | Department department4 = departmentMapper2.findById("18ec781fbefd727923b0d35740b177ab"); 37 | System.out.println("department3 == department4 : " + (department3 == department4)); 38 | departmentMapper2.findAll(); 39 | 40 | sqlSession2.close(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /mybatis-02-configuration/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.util.Date; 4 | import java.util.Objects; 5 | 6 | public class User { 7 | 8 | private String id; 9 | 10 | private String name; 11 | 12 | private Integer age; 13 | 14 | private Date birthday; 15 | 16 | private Department department; 17 | 18 | private Department department_id; 19 | 20 | @Override 21 | public String toString() { 22 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object o) { 27 | if (this == o) 28 | return true; 29 | if (o == null || getClass() != o.getClass()) 30 | return false; 31 | User user = (User) o; 32 | return Objects.equals(id, user.id); 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return Objects.hash(id); 38 | } 39 | 40 | public String getId() { 41 | return id; 42 | } 43 | 44 | public void setId(String id) { 45 | this.id = id; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public void setName(String name) { 53 | this.name = name; 54 | } 55 | 56 | public Integer getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(Integer age) { 61 | this.age = age; 62 | } 63 | 64 | public Date getBirthday() { 65 | return birthday; 66 | } 67 | 68 | public void setBirthday(Date birthday) { 69 | this.birthday = birthday; 70 | } 71 | 72 | public Department getDepartment() { 73 | return department; 74 | } 75 | 76 | public void setDepartment(Department department) { 77 | this.department = department; 78 | } 79 | 80 | public Department getDepartment_id() { 81 | return department_id; 82 | } 83 | 84 | public void setDepartment_id(Department department_id) { 85 | this.department_id = department_id; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /mybatis-05-cache/src/main/java/com/linkedbear/mybatis/app/Level1InvalidApplication.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.app; 2 | 3 | import com.linkedbear.mybatis.mapper.DepartmentMapper; 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 | 9 | import java.io.InputStream; 10 | 11 | public class Level1InvalidApplication { 12 | 13 | public static void main(String[] args) throws Exception { 14 | InputStream xml = Resources.getResourceAsStream("mybatis-config.xml"); 15 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(xml); 16 | SqlSession sqlSession = sqlSessionFactory.openSession(); 17 | SqlSession sqlSession2 = sqlSessionFactory.openSession(); 18 | 19 | // 跨SqlSession的一级缓存不共享 20 | DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class); 21 | DepartmentMapper departmentMapper2 = sqlSession2.getMapper(DepartmentMapper.class); 22 | 23 | // 此种情况会开启两个Connection连接,打印两次相同的SQL 24 | departmentMapper.findAll(); 25 | departmentMapper2.findAll(); 26 | 27 | System.out.println("重复调用findAll方法......"); 28 | departmentMapper.findAll(); 29 | System.out.println("手动清空SqlSession的缓存......"); 30 | sqlSession.clearCache(); 31 | System.out.println("清空缓存后重新调用findAll方法......"); 32 | departmentMapper.findAll(); 33 | System.out.println("--------------------------------"); 34 | 35 | sqlSession.close(); 36 | sqlSession2.close(); 37 | 38 | System.out.println("============================================="); 39 | 40 | SqlSession sqlSessionWithoutTransaction = sqlSessionFactory.openSession(true); 41 | DepartmentMapper departmentMapperWithoutTransaction = sqlSessionWithoutTransaction.getMapper(DepartmentMapper.class); 42 | 43 | departmentMapperWithoutTransaction.findAll(); 44 | departmentMapperWithoutTransaction.findAll(); 45 | 46 | sqlSessionWithoutTransaction.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /mybatis-08-spring/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-08-spring 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 23 | 24 | org.mybatis 25 | mybatis-spring 26 | 2.0.5 27 | 28 | 29 | org.springframework 30 | spring-context 31 | 5.2.6.RELEASE 32 | 33 | 34 | org.springframework 35 | spring-jdbc 36 | 5.2.6.RELEASE 37 | 38 | 39 | 40 | log4j 41 | log4j 42 | 1.2.17 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-compiler-plugin 51 | 3.8.1 52 | 53 | 1.8 54 | 1.8 55 | UTF-8 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /mybatis-08-spring/src/main/resources/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 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 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/provider/UserMapperProvider.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.provider; 2 | 3 | import com.linkedbear.mybatis.entity.User; 4 | import org.apache.ibatis.jdbc.SQL; 5 | 6 | import java.util.UUID; 7 | 8 | public class UserMapperProvider { 9 | 10 | public String findAll() { 11 | SQL sql = new SQL(); 12 | sql.SELECT("*").FROM("tbl_user"); 13 | return sql.toString(); 14 | } 15 | 16 | public String findAllByExample(User example) { 17 | SQL sql = new SQL(); 18 | sql.SELECT("*").FROM("tbl_user"); 19 | if (example.getId() != null) { 20 | sql.AND().WHERE("id = #{id}"); 21 | } 22 | if (example.getName() != null && example.getName().trim().length() > 0) { 23 | sql.AND().WHERE("name like concat('%', #{name}, '%')"); 24 | } 25 | sql.ORDER_BY("id asc"); 26 | return sql.toString(); 27 | } 28 | 29 | public String save(User user) { 30 | SQL sql = new SQL(); 31 | sql.INSERT_INTO("tbl_user"); 32 | sql.VALUES("id", "'" + UUID.randomUUID().toString().replaceAll("-", "") + "'"); 33 | sql.VALUES("name", "#{name}"); 34 | sql.VALUES("age", "#{age}"); 35 | sql.VALUES("department_id", "#{department.id}"); 36 | sql.INTO_COLUMNS("id", "name", "age", "department_id") 37 | .INTO_VALUES("'" + UUID.randomUUID().toString().replaceAll("-", "") + "'", 38 | "#{name}", "#{age}", "#{department.id}"); 39 | return sql.toString(); 40 | } 41 | 42 | public String updateByExample(User user) { 43 | SQL sql = new SQL(); 44 | sql.UPDATE("tbl_user"); 45 | if (user.getName() != null && user.getName().trim().length() > 0) { 46 | sql.SET("name = #{name}"); 47 | } 48 | if (user.getAge() != null) { 49 | sql.SET("age = #{age}"); 50 | } 51 | sql.WHERE("id = #{id}"); 52 | return sql.toString(); 53 | } 54 | 55 | public String deleteById(String id) { 56 | SQL sql = new SQL(); 57 | sql.DELETE_FROM("tbl_user"); 58 | sql.WHERE("id = #{id}"); 59 | return sql.toString(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 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 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/resources/a_provider/spring-mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 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 | -------------------------------------------------------------------------------- /mybatis-03-mapper/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | private Department department_id; 21 | 22 | @Override 23 | public String toString() { 24 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 25 | } 26 | 27 | @Override 28 | public boolean equals(Object o) { 29 | if (this == o) 30 | return true; 31 | if (o == null || getClass() != o.getClass()) 32 | return false; 33 | User user = (User) o; 34 | return Objects.equals(id, user.id); 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Objects.hash(id); 40 | } 41 | 42 | public String getId() { 43 | return id; 44 | } 45 | 46 | public void setId(String id) { 47 | this.id = id; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | public Integer getAge() { 59 | return age; 60 | } 61 | 62 | public void setAge(Integer age) { 63 | this.age = age; 64 | } 65 | 66 | public Date getBirthday() { 67 | return birthday; 68 | } 69 | 70 | public void setBirthday(Date birthday) { 71 | this.birthday = birthday; 72 | } 73 | 74 | public Department getDepartment() { 75 | return department; 76 | } 77 | 78 | public void setDepartment(Department department) { 79 | this.department = department; 80 | } 81 | 82 | public Department getDepartment_id() { 83 | return department_id; 84 | } 85 | 86 | public void setDepartment_id(Department department_id) { 87 | this.department_id = department_id; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /mybatis-04-annotation/src/main/java/com/linkedbear/mybatis/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | import java.util.Objects; 6 | 7 | public class User implements Serializable { 8 | private static final long serialVersionUID = -4232699868525443280L; 9 | 10 | private String id; 11 | 12 | private String name; 13 | 14 | private Integer age; 15 | 16 | private Date birthday; 17 | 18 | private Department department; 19 | 20 | private Department department_id; 21 | 22 | @Override 23 | public String toString() { 24 | return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", department=" + department + '}'; 25 | } 26 | 27 | @Override 28 | public boolean equals(Object o) { 29 | if (this == o) 30 | return true; 31 | if (o == null || getClass() != o.getClass()) 32 | return false; 33 | User user = (User) o; 34 | return Objects.equals(id, user.id); 35 | } 36 | 37 | @Override 38 | public int hashCode() { 39 | return Objects.hash(id); 40 | } 41 | 42 | public String getId() { 43 | return id; 44 | } 45 | 46 | public void setId(String id) { 47 | this.id = id; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | public Integer getAge() { 59 | return age; 60 | } 61 | 62 | public void setAge(Integer age) { 63 | this.age = age; 64 | } 65 | 66 | public Date getBirthday() { 67 | return birthday; 68 | } 69 | 70 | public void setBirthday(Date birthday) { 71 | this.birthday = birthday; 72 | } 73 | 74 | public Department getDepartment() { 75 | return department; 76 | } 77 | 78 | public void setDepartment(Department department) { 79 | this.department = department; 80 | } 81 | 82 | public Department getDepartment_id() { 83 | return department_id; 84 | } 85 | 86 | public void setDepartment_id(Department department_id) { 87 | this.department_id = department_id; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /mybatis-03-mapper/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 | 36 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /mybatis-09-encapsulate/src/main/java/com/linkedbear/mybatis/encapsulate/c_extra/metadata/EntityMetadata.java: -------------------------------------------------------------------------------- 1 | package com.linkedbear.mybatis.encapsulate.c_extra.metadata; 2 | 3 | import com.linkedbear.mybatis.encapsulate.c_extra.annotation.strategy.IdGenerator; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class EntityMetadata { 9 | 10 | private Class entityClass; 11 | 12 | private String tablename; 13 | 14 | private FieldMetadata idMetadata; 15 | 16 | private FieldMetadata loginDeleteMetadata; 17 | 18 | private FieldMetadata versionMetadata; 19 | 20 | private IdGenerator idGenerator; 21 | 22 | private List fields = new ArrayList<>(); 23 | 24 | public EntityMetadata(Class entityClass) { 25 | this.entityClass = entityClass; 26 | } 27 | 28 | public Class getEntityClass() { 29 | return entityClass; 30 | } 31 | 32 | public void setEntityClass(Class entityClass) { 33 | this.entityClass = entityClass; 34 | } 35 | 36 | public String getTablename() { 37 | return tablename; 38 | } 39 | 40 | public void setTablename(String tablename) { 41 | this.tablename = tablename; 42 | } 43 | 44 | public FieldMetadata getIdMetadata() { 45 | return idMetadata; 46 | } 47 | 48 | public void setIdMetadata(FieldMetadata idMetadata) { 49 | this.idMetadata = idMetadata; 50 | } 51 | 52 | public FieldMetadata getLoginDeleteMetadata() { 53 | return loginDeleteMetadata; 54 | } 55 | 56 | public void setLoginDeleteMetadata(FieldMetadata loginDeleteMetadata) { 57 | this.loginDeleteMetadata = loginDeleteMetadata; 58 | } 59 | 60 | public FieldMetadata getVersionMetadata() { 61 | return versionMetadata; 62 | } 63 | 64 | public void setVersionMetadata(FieldMetadata versionMetadata) { 65 | this.versionMetadata = versionMetadata; 66 | } 67 | 68 | public IdGenerator getIdGenerator() { 69 | return idGenerator; 70 | } 71 | 72 | public void setIdGenerator(IdGenerator idGenerator) { 73 | this.idGenerator = idGenerator; 74 | } 75 | 76 | public List getFields() { 77 | return fields; 78 | } 79 | 80 | public void setFields(List fields) { 81 | this.fields = fields; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /mybatis-03-mapper/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.linkedbear.mybatis 8 | mybatis-03-mapper 9 | 1.0-RELEASE 10 | 11 | 12 | 13 | org.mybatis 14 | mybatis 15 | 3.5.5 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 5.1.47 21 | 22 | 34 | 35 | 36 | org.postgresql 37 | postgresql 38 | 42.2.18 39 | 40 | 41 | 42 | cglib 43 | cglib 44 | 3.1 45 | 46 | 47 | 48 | log4j 49 | log4j 50 | 1.2.17 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 3.8.1 60 | 61 | 1.8 62 | 1.8 63 | UTF-8 64 | 65 | 66 | 67 | 68 | --------------------------------------------------------------------------------