diff --git a/demo-orm-mybatis-generator/README.md b/demo-orm-mybatis-generator/README.md new file mode 100644 index 000000000..d47df5e26 --- /dev/null +++ b/demo-orm-mybatis-generator/README.md @@ -0,0 +1,318 @@ +# spring-boot-demo-orm-mybatis-generator + +> 此 demo 演示了 Spring Boot 如何与原生的 mybatis 整合,通过mybatis-generator插件,将DB表映射为Java实体类,并自动生成对应Mapper文件。使用生成的Example类和 +> Criteria 通过Java代码组合查询条件或更新条件,免去编写SQl过程。 + +1. IDEA下点击右👉边栏 Maven +2. demo-orm-mybatis-generator +3. Plugins +4. mybatis-generator +5. mybatis-generator:generate +6. 在entity、mapper包下面就会生成对应的代码 + +## pom.xml + +```xml + + + 4.0.0 + + demo-orm-mybatis-generator + 1.0.0-SNAPSHOT + jar + + demo-orm-mybatis-generator + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 2.2.0 + 1.4.0 + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.version} + + + + mysql + mysql-connector-java + + + + cn.hutool + hutool-all + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + demo-orm-mybatis-generator + + + org.springframework.boot + spring-boot-maven-plugin + + + org.mybatis.generator + mybatis-generator-maven-plugin + ${mybatis.generator.version} + + ${basedir}/src/main/resources/generator/generatorConfig.xml + true + true + + + + + + +``` + +## generatorConfig.xml + +**需要修改对应的数据库驱动路径、连接信息和表信息** + +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+``` + +## SpringBootDemoOrmMybatisGeneratorApplication.java + +```java +/** + *

+ * 启动类 + *

+ * + * @author qilihui + * @date 2021/10/11 9:50 下午 + */ +@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.generator.mapper"}) +@SpringBootApplication +public class SpringBootDemoOrmMybatisGeneratorApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoOrmMybatisGeneratorApplication.class, args); + } +} +``` + +## application.yml + +```yaml +spring: + datasource: + url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver + type: com.zaxxer.hikari.HikariDataSource + initialization-mode: always + continue-on-error: true + schema: + - "classpath:db/schema.sql" + data: + - "classpath:db/data.sql" + hikari: + minimum-idle: 5 + connection-test-query: SELECT 1 FROM DUAL + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 30000 + pool-name: SpringBootDemoHikariCP + max-lifetime: 60000 + connection-timeout: 30000 +logging: + level: + com.xkcoding: debug + com.xkcoding.orm.mybatis.mapper: trace +mybatis: + configuration: + # 下划线转驼峰 + map-underscore-to-camel-case: true + mapper-locations: classpath:mappers/*.xml + type-aliases-package: com.xkcoding.orm.mybatis.entity +``` + +## UserRepo.java + +```java +/** + * @author qilihui + * @date 2021/10/11 9:50 下午 + */ +@Repository +@AllArgsConstructor(onConstructor_ = @Autowired) +public class UserRepo { + private final UserMapper userMapper; + + public User selectByName(String name) { + UserExample example = new UserExample(); + example.createCriteria().andNameEqualTo(name); + List userList = userMapper.selectByExample(example); + if (CollectionUtil.isNotEmpty(userList)) { + return userList.get(0); + } + return null; + } + + public int insert(User user) { + return userMapper.insertSelective(user); + } + + public int updateByNameSelective(User user) { + UserExample example = new UserExample(); + example.createCriteria().andNameEqualTo(user.getName()); + return userMapper.updateByExampleSelective(user, example); + } +} +``` + +## UserRepoTest.java + +```java +/** + * @author qilihui + * @date 2021/10/12 11:09 上午 + */ +@Slf4j +public class UserRepoTest extends SpringBootDemoOrmMybatisGeneratorApplicationTests { + @Autowired + private UserRepo userRepo; + + /** + * 根据name查询测试 + */ + @Test + public void selectByName() { + User user = userRepo.selectByName("user_1"); + Assert.assertTrue(Objects.nonNull(user)); + log.debug("【user】= {}", user); + } + + /** + * insert 测试 + */ + @Test + public void insert() { + User user = new User(); + user.setName("user_3"); + user.setPassword("6c6bf02c8d5d3d128f34b1700cb1e32c"); + user.setSalt("fcbdd0e8a9404a5585ea4e01d0e4d7a0"); + user.setEmail("user3@xkcoding.com"); + user.setPhoneNumber("17300000003"); + user.setStatus(1); + userRepo.insert(user); + + User select = userRepo.selectByName("user_3"); + Assert.assertTrue(Objects.nonNull(select)); + log.debug("【user】= {}", select); + } + + /** + * update 测试 + */ + @Test + public void updateByNameSelective() { + User user = new User(); + user.setName("user_2"); + user.setStatus(2); + int update = userRepo.updateByNameSelective(user); + Assert.assertEquals(update, 1); + + User select = userRepo.selectByName("user_2"); + Assert.assertTrue(Objects.nonNull(select)); + log.debug("【user】= {}", select); + } +} +``` + +## 参考 + +- Mybatis官方文档:http://www.mybatis.org/mybatis-3/zh/index.html + +- Mybatis官方脚手架文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ + +- Mybatis整合Spring Boot官方demo:https://github.com/mybatis/spring-boot-starter/tree/master/mybatis-spring-boot-samples diff --git a/demo-orm-mybatis-generator/pom.xml b/demo-orm-mybatis-generator/pom.xml new file mode 100644 index 000000000..a2bf7f153 --- /dev/null +++ b/demo-orm-mybatis-generator/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + demo-orm-mybatis-generator + 1.0.0-SNAPSHOT + jar + + demo-orm-mybatis-generator + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 2.2.0 + 1.4.0 + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.version} + + + + mysql + mysql-connector-java + + + + cn.hutool + hutool-all + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + demo-orm-mybatis-generator + + + org.springframework.boot + spring-boot-maven-plugin + + + org.mybatis.generator + mybatis-generator-maven-plugin + ${mybatis.generator.version} + + ${basedir}/src/main/resources/generator/generatorConfig.xml + true + true + + + + + + diff --git a/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplication.java b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplication.java new file mode 100644 index 000000000..261f724eb --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplication.java @@ -0,0 +1,22 @@ +package com.xkcoding.orm.mybatis.generator; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + *

+ * 启动类 + *

+ * + * @author qilihui + * @date 2021/10/11 9:50 下午 + */ +@MapperScan(basePackages = {"com.xkcoding.orm.mybatis.generator.mapper"}) +@SpringBootApplication +public class SpringBootDemoOrmMybatisGeneratorApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoOrmMybatisGeneratorApplication.class, args); + } +} diff --git a/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/User.java b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/User.java new file mode 100644 index 000000000..db7031d2e --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/User.java @@ -0,0 +1,166 @@ +package com.xkcoding.orm.mybatis.generator.entity; + +import java.util.Date; + +public class User { + private Integer id; + + private String name; + + private String password; + + private String salt; + + private String email; + + private String phoneNumber; + + private Integer status; + + private Date createTime; + + private Date lastLoginTime; + + private Date lastUpdateTime; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name == null ? null : name.trim(); + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password == null ? null : password.trim(); + } + + public String getSalt() { + return salt; + } + + public void setSalt(String salt) { + this.salt = salt == null ? null : salt.trim(); + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email == null ? null : email.trim(); + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber == null ? null : phoneNumber.trim(); + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public Date getCreateTime() { + return createTime; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + public Date getLastLoginTime() { + return lastLoginTime; + } + + public void setLastLoginTime(Date lastLoginTime) { + this.lastLoginTime = lastLoginTime; + } + + public Date getLastUpdateTime() { + return lastUpdateTime; + } + + public void setLastUpdateTime(Date lastUpdateTime) { + this.lastUpdateTime = lastUpdateTime; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", name=").append(name); + sb.append(", password=").append(password); + sb.append(", salt=").append(salt); + sb.append(", email=").append(email); + sb.append(", phoneNumber=").append(phoneNumber); + sb.append(", status=").append(status); + sb.append(", createTime=").append(createTime); + sb.append(", lastLoginTime=").append(lastLoginTime); + sb.append(", lastUpdateTime=").append(lastUpdateTime); + sb.append("]"); + return sb.toString(); + } + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + User other = (User) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) + && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword())) + && (this.getSalt() == null ? other.getSalt() == null : this.getSalt().equals(other.getSalt())) + && (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail())) + && (this.getPhoneNumber() == null ? other.getPhoneNumber() == null : this.getPhoneNumber().equals(other.getPhoneNumber())) + && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus())) + && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) + && (this.getLastLoginTime() == null ? other.getLastLoginTime() == null : this.getLastLoginTime().equals(other.getLastLoginTime())) + && (this.getLastUpdateTime() == null ? other.getLastUpdateTime() == null : this.getLastUpdateTime().equals(other.getLastUpdateTime())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); + result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode()); + result = prime * result + ((getSalt() == null) ? 0 : getSalt().hashCode()); + result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode()); + result = prime * result + ((getPhoneNumber() == null) ? 0 : getPhoneNumber().hashCode()); + result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode()); + result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); + result = prime * result + ((getLastLoginTime() == null) ? 0 : getLastLoginTime().hashCode()); + result = prime * result + ((getLastUpdateTime() == null) ? 0 : getLastUpdateTime().hashCode()); + return result; + } +} \ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/UserExample.java b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/UserExample.java new file mode 100644 index 000000000..398b72c20 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/entity/UserExample.java @@ -0,0 +1,850 @@ +package com.xkcoding.orm.mybatis.generator.entity; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class UserExample { + protected String orderByClause; + + protected boolean distinct; + + protected List oredCriteria; + + public UserExample() { + oredCriteria = new ArrayList<>(); + } + + public void setOrderByClause(String orderByClause) { + this.orderByClause = orderByClause; + } + + public String getOrderByClause() { + return orderByClause; + } + + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + + public boolean isDistinct() { + return distinct; + } + + public List getOredCriteria() { + return oredCriteria; + } + + public void or(Criteria criteria) { + oredCriteria.add(criteria); + } + + public Criteria or() { + Criteria criteria = createCriteriaInternal(); + oredCriteria.add(criteria); + return criteria; + } + + public Criteria createCriteria() { + Criteria criteria = createCriteriaInternal(); + if (oredCriteria.size() == 0) { + oredCriteria.add(criteria); + } + return criteria; + } + + protected Criteria createCriteriaInternal() { + Criteria criteria = new Criteria(); + return criteria; + } + + public void clear() { + oredCriteria.clear(); + orderByClause = null; + distinct = false; + } + + protected abstract static class GeneratedCriteria { + protected List criteria; + + protected GeneratedCriteria() { + super(); + criteria = new ArrayList<>(); + } + + public boolean isValid() { + return criteria.size() > 0; + } + + public List getAllCriteria() { + return criteria; + } + + public List getCriteria() { + return criteria; + } + + protected void addCriterion(String condition) { + if (condition == null) { + throw new RuntimeException("Value for condition cannot be null"); + } + criteria.add(new Criterion(condition)); + } + + protected void addCriterion(String condition, Object value, String property) { + if (value == null) { + throw new RuntimeException("Value for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value)); + } + + protected void addCriterion(String condition, Object value1, Object value2, String property) { + if (value1 == null || value2 == null) { + throw new RuntimeException("Between values for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value1, value2)); + } + + public Criteria andIdIsNull() { + addCriterion("id is null"); + return (Criteria) this; + } + + public Criteria andIdIsNotNull() { + addCriterion("id is not null"); + return (Criteria) this; + } + + public Criteria andIdEqualTo(Integer value) { + addCriterion("id =", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotEqualTo(Integer value) { + addCriterion("id <>", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThan(Integer value) { + addCriterion("id >", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThanOrEqualTo(Integer value) { + addCriterion("id >=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThan(Integer value) { + addCriterion("id <", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThanOrEqualTo(Integer value) { + addCriterion("id <=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdIn(List values) { + addCriterion("id in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdNotIn(List values) { + addCriterion("id not in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdBetween(Integer value1, Integer value2) { + addCriterion("id between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andIdNotBetween(Integer value1, Integer value2) { + addCriterion("id not between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andNameIsNull() { + addCriterion("name is null"); + return (Criteria) this; + } + + public Criteria andNameIsNotNull() { + addCriterion("name is not null"); + return (Criteria) this; + } + + public Criteria andNameEqualTo(String value) { + addCriterion("name =", value, "name"); + return (Criteria) this; + } + + public Criteria andNameNotEqualTo(String value) { + addCriterion("name <>", value, "name"); + return (Criteria) this; + } + + public Criteria andNameGreaterThan(String value) { + addCriterion("name >", value, "name"); + return (Criteria) this; + } + + public Criteria andNameGreaterThanOrEqualTo(String value) { + addCriterion("name >=", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLessThan(String value) { + addCriterion("name <", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLessThanOrEqualTo(String value) { + addCriterion("name <=", value, "name"); + return (Criteria) this; + } + + public Criteria andNameLike(String value) { + addCriterion("name like", value, "name"); + return (Criteria) this; + } + + public Criteria andNameNotLike(String value) { + addCriterion("name not like", value, "name"); + return (Criteria) this; + } + + public Criteria andNameIn(List values) { + addCriterion("name in", values, "name"); + return (Criteria) this; + } + + public Criteria andNameNotIn(List values) { + addCriterion("name not in", values, "name"); + return (Criteria) this; + } + + public Criteria andNameBetween(String value1, String value2) { + addCriterion("name between", value1, value2, "name"); + return (Criteria) this; + } + + public Criteria andNameNotBetween(String value1, String value2) { + addCriterion("name not between", value1, value2, "name"); + return (Criteria) this; + } + + public Criteria andPasswordIsNull() { + addCriterion("password is null"); + return (Criteria) this; + } + + public Criteria andPasswordIsNotNull() { + addCriterion("password is not null"); + return (Criteria) this; + } + + public Criteria andPasswordEqualTo(String value) { + addCriterion("password =", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordNotEqualTo(String value) { + addCriterion("password <>", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordGreaterThan(String value) { + addCriterion("password >", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordGreaterThanOrEqualTo(String value) { + addCriterion("password >=", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordLessThan(String value) { + addCriterion("password <", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordLessThanOrEqualTo(String value) { + addCriterion("password <=", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordLike(String value) { + addCriterion("password like", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordNotLike(String value) { + addCriterion("password not like", value, "password"); + return (Criteria) this; + } + + public Criteria andPasswordIn(List values) { + addCriterion("password in", values, "password"); + return (Criteria) this; + } + + public Criteria andPasswordNotIn(List values) { + addCriterion("password not in", values, "password"); + return (Criteria) this; + } + + public Criteria andPasswordBetween(String value1, String value2) { + addCriterion("password between", value1, value2, "password"); + return (Criteria) this; + } + + public Criteria andPasswordNotBetween(String value1, String value2) { + addCriterion("password not between", value1, value2, "password"); + return (Criteria) this; + } + + public Criteria andSaltIsNull() { + addCriterion("salt is null"); + return (Criteria) this; + } + + public Criteria andSaltIsNotNull() { + addCriterion("salt is not null"); + return (Criteria) this; + } + + public Criteria andSaltEqualTo(String value) { + addCriterion("salt =", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltNotEqualTo(String value) { + addCriterion("salt <>", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltGreaterThan(String value) { + addCriterion("salt >", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltGreaterThanOrEqualTo(String value) { + addCriterion("salt >=", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltLessThan(String value) { + addCriterion("salt <", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltLessThanOrEqualTo(String value) { + addCriterion("salt <=", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltLike(String value) { + addCriterion("salt like", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltNotLike(String value) { + addCriterion("salt not like", value, "salt"); + return (Criteria) this; + } + + public Criteria andSaltIn(List values) { + addCriterion("salt in", values, "salt"); + return (Criteria) this; + } + + public Criteria andSaltNotIn(List values) { + addCriterion("salt not in", values, "salt"); + return (Criteria) this; + } + + public Criteria andSaltBetween(String value1, String value2) { + addCriterion("salt between", value1, value2, "salt"); + return (Criteria) this; + } + + public Criteria andSaltNotBetween(String value1, String value2) { + addCriterion("salt not between", value1, value2, "salt"); + return (Criteria) this; + } + + public Criteria andEmailIsNull() { + addCriterion("email is null"); + return (Criteria) this; + } + + public Criteria andEmailIsNotNull() { + addCriterion("email is not null"); + return (Criteria) this; + } + + public Criteria andEmailEqualTo(String value) { + addCriterion("email =", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotEqualTo(String value) { + addCriterion("email <>", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailGreaterThan(String value) { + addCriterion("email >", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailGreaterThanOrEqualTo(String value) { + addCriterion("email >=", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLessThan(String value) { + addCriterion("email <", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLessThanOrEqualTo(String value) { + addCriterion("email <=", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailLike(String value) { + addCriterion("email like", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotLike(String value) { + addCriterion("email not like", value, "email"); + return (Criteria) this; + } + + public Criteria andEmailIn(List values) { + addCriterion("email in", values, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotIn(List values) { + addCriterion("email not in", values, "email"); + return (Criteria) this; + } + + public Criteria andEmailBetween(String value1, String value2) { + addCriterion("email between", value1, value2, "email"); + return (Criteria) this; + } + + public Criteria andEmailNotBetween(String value1, String value2) { + addCriterion("email not between", value1, value2, "email"); + return (Criteria) this; + } + + public Criteria andPhoneNumberIsNull() { + addCriterion("phone_number is null"); + return (Criteria) this; + } + + public Criteria andPhoneNumberIsNotNull() { + addCriterion("phone_number is not null"); + return (Criteria) this; + } + + public Criteria andPhoneNumberEqualTo(String value) { + addCriterion("phone_number =", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberNotEqualTo(String value) { + addCriterion("phone_number <>", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberGreaterThan(String value) { + addCriterion("phone_number >", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberGreaterThanOrEqualTo(String value) { + addCriterion("phone_number >=", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberLessThan(String value) { + addCriterion("phone_number <", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberLessThanOrEqualTo(String value) { + addCriterion("phone_number <=", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberLike(String value) { + addCriterion("phone_number like", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberNotLike(String value) { + addCriterion("phone_number not like", value, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberIn(List values) { + addCriterion("phone_number in", values, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberNotIn(List values) { + addCriterion("phone_number not in", values, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberBetween(String value1, String value2) { + addCriterion("phone_number between", value1, value2, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andPhoneNumberNotBetween(String value1, String value2) { + addCriterion("phone_number not between", value1, value2, "phoneNumber"); + return (Criteria) this; + } + + public Criteria andStatusIsNull() { + addCriterion("status is null"); + return (Criteria) this; + } + + public Criteria andStatusIsNotNull() { + addCriterion("status is not null"); + return (Criteria) this; + } + + public Criteria andStatusEqualTo(Integer value) { + addCriterion("status =", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotEqualTo(Integer value) { + addCriterion("status <>", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThan(Integer value) { + addCriterion("status >", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusGreaterThanOrEqualTo(Integer value) { + addCriterion("status >=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThan(Integer value) { + addCriterion("status <", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusLessThanOrEqualTo(Integer value) { + addCriterion("status <=", value, "status"); + return (Criteria) this; + } + + public Criteria andStatusIn(List values) { + addCriterion("status in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotIn(List values) { + addCriterion("status not in", values, "status"); + return (Criteria) this; + } + + public Criteria andStatusBetween(Integer value1, Integer value2) { + addCriterion("status between", value1, value2, "status"); + return (Criteria) this; + } + + public Criteria andStatusNotBetween(Integer value1, Integer value2) { + addCriterion("status not between", value1, value2, "status"); + return (Criteria) this; + } + + public Criteria andCreateTimeIsNull() { + addCriterion("create_time is null"); + return (Criteria) this; + } + + public Criteria andCreateTimeIsNotNull() { + addCriterion("create_time is not null"); + return (Criteria) this; + } + + public Criteria andCreateTimeEqualTo(Date value) { + addCriterion("create_time =", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotEqualTo(Date value) { + addCriterion("create_time <>", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeGreaterThan(Date value) { + addCriterion("create_time >", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeGreaterThanOrEqualTo(Date value) { + addCriterion("create_time >=", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeLessThan(Date value) { + addCriterion("create_time <", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeLessThanOrEqualTo(Date value) { + addCriterion("create_time <=", value, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeIn(List values) { + addCriterion("create_time in", values, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotIn(List values) { + addCriterion("create_time not in", values, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeBetween(Date value1, Date value2) { + addCriterion("create_time between", value1, value2, "createTime"); + return (Criteria) this; + } + + public Criteria andCreateTimeNotBetween(Date value1, Date value2) { + addCriterion("create_time not between", value1, value2, "createTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeIsNull() { + addCriterion("last_login_time is null"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeIsNotNull() { + addCriterion("last_login_time is not null"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeEqualTo(Date value) { + addCriterion("last_login_time =", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeNotEqualTo(Date value) { + addCriterion("last_login_time <>", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeGreaterThan(Date value) { + addCriterion("last_login_time >", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeGreaterThanOrEqualTo(Date value) { + addCriterion("last_login_time >=", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeLessThan(Date value) { + addCriterion("last_login_time <", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeLessThanOrEqualTo(Date value) { + addCriterion("last_login_time <=", value, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeIn(List values) { + addCriterion("last_login_time in", values, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeNotIn(List values) { + addCriterion("last_login_time not in", values, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeBetween(Date value1, Date value2) { + addCriterion("last_login_time between", value1, value2, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastLoginTimeNotBetween(Date value1, Date value2) { + addCriterion("last_login_time not between", value1, value2, "lastLoginTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeIsNull() { + addCriterion("last_update_time is null"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeIsNotNull() { + addCriterion("last_update_time is not null"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeEqualTo(Date value) { + addCriterion("last_update_time =", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeNotEqualTo(Date value) { + addCriterion("last_update_time <>", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeGreaterThan(Date value) { + addCriterion("last_update_time >", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeGreaterThanOrEqualTo(Date value) { + addCriterion("last_update_time >=", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeLessThan(Date value) { + addCriterion("last_update_time <", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeLessThanOrEqualTo(Date value) { + addCriterion("last_update_time <=", value, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeIn(List values) { + addCriterion("last_update_time in", values, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeNotIn(List values) { + addCriterion("last_update_time not in", values, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeBetween(Date value1, Date value2) { + addCriterion("last_update_time between", value1, value2, "lastUpdateTime"); + return (Criteria) this; + } + + public Criteria andLastUpdateTimeNotBetween(Date value1, Date value2) { + addCriterion("last_update_time not between", value1, value2, "lastUpdateTime"); + return (Criteria) this; + } + } + + public static class Criteria extends GeneratedCriteria { + protected Criteria() { + super(); + } + } + + public static class Criterion { + private String condition; + + private Object value; + + private Object secondValue; + + private boolean noValue; + + private boolean singleValue; + + private boolean betweenValue; + + private boolean listValue; + + private String typeHandler; + + public String getCondition() { + return condition; + } + + public Object getValue() { + return value; + } + + public Object getSecondValue() { + return secondValue; + } + + public boolean isNoValue() { + return noValue; + } + + public boolean isSingleValue() { + return singleValue; + } + + public boolean isBetweenValue() { + return betweenValue; + } + + public boolean isListValue() { + return listValue; + } + + public String getTypeHandler() { + return typeHandler; + } + + protected Criterion(String condition) { + super(); + this.condition = condition; + this.typeHandler = null; + this.noValue = true; + } + + protected Criterion(String condition, Object value, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.typeHandler = typeHandler; + if (value instanceof List) { + this.listValue = true; + } else { + this.singleValue = true; + } + } + + protected Criterion(String condition, Object value) { + this(condition, value, null); + } + + protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.secondValue = secondValue; + this.typeHandler = typeHandler; + this.betweenValue = true; + } + + protected Criterion(String condition, Object value, Object secondValue) { + this(condition, value, secondValue, null); + } + } +} \ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/mapper/UserMapper.java b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/mapper/UserMapper.java new file mode 100644 index 000000000..1cccfd9df --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/mapper/UserMapper.java @@ -0,0 +1,30 @@ +package com.xkcoding.orm.mybatis.generator.mapper; + +import com.xkcoding.orm.mybatis.generator.entity.User; +import com.xkcoding.orm.mybatis.generator.entity.UserExample; +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface UserMapper { + long countByExample(UserExample example); + + int deleteByExample(UserExample example); + + int deleteByPrimaryKey(Integer id); + + int insert(User record); + + int insertSelective(User record); + + List selectByExample(UserExample example); + + User selectByPrimaryKey(Integer id); + + int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); + + int updateByExample(@Param("record") User record, @Param("example") UserExample example); + + int updateByPrimaryKeySelective(User record); + + int updateByPrimaryKey(User record); +} \ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/repo/UserRepo.java b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/repo/UserRepo.java new file mode 100644 index 000000000..497c85a80 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/java/com/xkcoding/orm/mybatis/generator/repo/UserRepo.java @@ -0,0 +1,41 @@ +package com.xkcoding.orm.mybatis.generator.repo; + +import cn.hutool.core.collection.CollectionUtil; +import com.xkcoding.orm.mybatis.generator.entity.User; +import com.xkcoding.orm.mybatis.generator.entity.UserExample; +import com.xkcoding.orm.mybatis.generator.mapper.UserMapper; +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + * @author qilihui + * @date 2021/10/11 9:50 下午 + */ +@Repository +@AllArgsConstructor(onConstructor_ = @Autowired) +public class UserRepo { + private final UserMapper userMapper; + + public User selectByName(String name) { + UserExample example = new UserExample(); + example.createCriteria().andNameEqualTo(name); + List userList = userMapper.selectByExample(example); + if (CollectionUtil.isNotEmpty(userList)) { + return userList.get(0); + } + return null; + } + + public int insert(User user) { + return userMapper.insertSelective(user); + } + + public int updateByNameSelective(User user) { + UserExample example = new UserExample(); + example.createCriteria().andNameEqualTo(user.getName()); + return userMapper.updateByExampleSelective(user, example); + } +} diff --git a/demo-orm-mybatis-generator/src/main/resources/application.yml b/demo-orm-mybatis-generator/src/main/resources/application.yml new file mode 100644 index 000000000..d5ea9864f --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/resources/application.yml @@ -0,0 +1,32 @@ +spring: + datasource: + url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver + type: com.zaxxer.hikari.HikariDataSource + initialization-mode: always + continue-on-error: true + schema: + - "classpath:db/schema.sql" + data: + - "classpath:db/data.sql" + hikari: + minimum-idle: 5 + connection-test-query: SELECT 1 FROM DUAL + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 30000 + pool-name: SpringBootDemoHikariCP + max-lifetime: 60000 + connection-timeout: 30000 +logging: + level: + com.xkcoding: debug + com.xkcoding.orm.mybatis.mapper: trace +mybatis: + configuration: + # 下划线转驼峰 + map-underscore-to-camel-case: true + mapper-locations: classpath:mappers/*.xml + type-aliases-package: com.xkcoding.orm.mybatis.entity diff --git a/demo-orm-mybatis-generator/src/main/resources/db/data.sql b/demo-orm-mybatis-generator/src/main/resources/db/data.sql new file mode 100644 index 000000000..3ab2b5985 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/resources/db/data.sql @@ -0,0 +1,2 @@ +INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); +INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone_number`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); \ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/main/resources/db/schema.sql b/demo-orm-mybatis-generator/src/main/resources/db/schema.sql new file mode 100644 index 000000000..22804e575 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/resources/db/schema.sql @@ -0,0 +1,13 @@ +DROP TABLE IF EXISTS `orm_user`; +CREATE TABLE `orm_user` ( + `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', + `password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', + `salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', + `email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', + `phone_number` VARCHAR(15) NOT NULL UNIQUE COMMENT '手机号码', + `status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态,-1:逻辑删除,0:禁用,1:启用', + `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', + `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', + `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; diff --git a/demo-orm-mybatis-generator/src/main/resources/generator/generatorConfig.xml b/demo-orm-mybatis-generator/src/main/resources/generator/generatorConfig.xml new file mode 100644 index 000000000..3ac2d7b32 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/resources/generator/generatorConfig.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/main/resources/mappers/UserMapper.xml b/demo-orm-mybatis-generator/src/main/resources/mappers/UserMapper.xml new file mode 100644 index 000000000..e9dbbb211 --- /dev/null +++ b/demo-orm-mybatis-generator/src/main/resources/mappers/UserMapper.xml @@ -0,0 +1,292 @@ + + + + + + + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + id, name, password, salt, email, phone_number, status, create_time, last_login_time, + last_update_time + + + + + delete from orm_user + where id = #{id,jdbcType=INTEGER} + + + delete from orm_user + + + + + + insert into orm_user (id, name, password, + salt, email, phone_number, + status, create_time, last_login_time, + last_update_time) + values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, + #{salt,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phoneNumber,jdbcType=VARCHAR}, + #{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{lastLoginTime,jdbcType=TIMESTAMP}, + #{lastUpdateTime,jdbcType=TIMESTAMP}) + + + insert into orm_user + + + id, + + + name, + + + password, + + + salt, + + + email, + + + phone_number, + + + status, + + + create_time, + + + last_login_time, + + + last_update_time, + + + + + #{id,jdbcType=INTEGER}, + + + #{name,jdbcType=VARCHAR}, + + + #{password,jdbcType=VARCHAR}, + + + #{salt,jdbcType=VARCHAR}, + + + #{email,jdbcType=VARCHAR}, + + + #{phoneNumber,jdbcType=VARCHAR}, + + + #{status,jdbcType=INTEGER}, + + + #{createTime,jdbcType=TIMESTAMP}, + + + #{lastLoginTime,jdbcType=TIMESTAMP}, + + + #{lastUpdateTime,jdbcType=TIMESTAMP}, + + + + + + update orm_user + + + id = #{record.id,jdbcType=INTEGER}, + + + name = #{record.name,jdbcType=VARCHAR}, + + + password = #{record.password,jdbcType=VARCHAR}, + + + salt = #{record.salt,jdbcType=VARCHAR}, + + + email = #{record.email,jdbcType=VARCHAR}, + + + phone_number = #{record.phoneNumber,jdbcType=VARCHAR}, + + + status = #{record.status,jdbcType=INTEGER}, + + + create_time = #{record.createTime,jdbcType=TIMESTAMP}, + + + last_login_time = #{record.lastLoginTime,jdbcType=TIMESTAMP}, + + + last_update_time = #{record.lastUpdateTime,jdbcType=TIMESTAMP}, + + + + + + + + update orm_user + set id = #{record.id,jdbcType=INTEGER}, + name = #{record.name,jdbcType=VARCHAR}, + password = #{record.password,jdbcType=VARCHAR}, + salt = #{record.salt,jdbcType=VARCHAR}, + email = #{record.email,jdbcType=VARCHAR}, + phone_number = #{record.phoneNumber,jdbcType=VARCHAR}, + status = #{record.status,jdbcType=INTEGER}, + create_time = #{record.createTime,jdbcType=TIMESTAMP}, + last_login_time = #{record.lastLoginTime,jdbcType=TIMESTAMP}, + last_update_time = #{record.lastUpdateTime,jdbcType=TIMESTAMP} + + + + + + update orm_user + + + name = #{name,jdbcType=VARCHAR}, + + + password = #{password,jdbcType=VARCHAR}, + + + salt = #{salt,jdbcType=VARCHAR}, + + + email = #{email,jdbcType=VARCHAR}, + + + phone_number = #{phoneNumber,jdbcType=VARCHAR}, + + + status = #{status,jdbcType=INTEGER}, + + + create_time = #{createTime,jdbcType=TIMESTAMP}, + + + last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP}, + + + last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}, + + + where id = #{id,jdbcType=INTEGER} + + + update orm_user + set name = #{name,jdbcType=VARCHAR}, + password = #{password,jdbcType=VARCHAR}, + salt = #{salt,jdbcType=VARCHAR}, + email = #{email,jdbcType=VARCHAR}, + phone_number = #{phoneNumber,jdbcType=VARCHAR}, + status = #{status,jdbcType=INTEGER}, + create_time = #{createTime,jdbcType=TIMESTAMP}, + last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP}, + last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP} + where id = #{id,jdbcType=INTEGER} + + \ No newline at end of file diff --git a/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplicationTests.java b/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplicationTests.java new file mode 100644 index 000000000..f56cedf09 --- /dev/null +++ b/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/SpringBootDemoOrmMybatisGeneratorApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.orm.mybatis.generator; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootDemoOrmMybatisGeneratorApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/repo/UserRepoTest.java b/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/repo/UserRepoTest.java new file mode 100644 index 000000000..5dff19a7e --- /dev/null +++ b/demo-orm-mybatis-generator/src/test/java/com/xkcoding/orm/mybatis/generator/repo/UserRepoTest.java @@ -0,0 +1,65 @@ +package com.xkcoding.orm.mybatis.generator.repo; + +import com.xkcoding.orm.mybatis.generator.SpringBootDemoOrmMybatisGeneratorApplicationTests; +import com.xkcoding.orm.mybatis.generator.entity.User; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Objects; + +/** + * @author qilihui + * @date 2021/10/12 11:09 上午 + */ +@Slf4j +public class UserRepoTest extends SpringBootDemoOrmMybatisGeneratorApplicationTests { + @Autowired + private UserRepo userRepo; + + /** + * 根据name查询测试 + */ + @Test + public void selectByName() { + User user = userRepo.selectByName("user_1"); + Assert.assertTrue(Objects.nonNull(user)); + log.debug("【user】= {}", user); + } + + /** + * insert 测试 + */ + @Test + public void insert() { + User user = new User(); + user.setName("user_3"); + user.setPassword("6c6bf02c8d5d3d128f34b1700cb1e32c"); + user.setSalt("fcbdd0e8a9404a5585ea4e01d0e4d7a0"); + user.setEmail("user3@xkcoding.com"); + user.setPhoneNumber("17300000003"); + user.setStatus(1); + userRepo.insert(user); + + User select = userRepo.selectByName("user_3"); + Assert.assertTrue(Objects.nonNull(select)); + log.debug("【user】= {}", select); + } + + /** + * update 测试 + */ + @Test + public void updateByNameSelective() { + User user = new User(); + user.setName("user_2"); + user.setStatus(2); + int update = userRepo.updateByNameSelective(user); + Assert.assertEquals(update, 1); + + User select = userRepo.selectByName("user_2"); + Assert.assertTrue(Objects.nonNull(select)); + log.debug("【user】= {}", select); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 702799f5b..e05001f0c 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ demo-orm-jdbctemplate demo-orm-jpa demo-orm-mybatis + demo-orm-mybatis-generator demo-orm-mybatis-mapper-page demo-orm-mybatis-plus demo-orm-beetlsql