From 8b45b1a27c10039d80a77298c1a02f7681fe0c89 Mon Sep 17 00:00:00 2001 From: happyxiaofan Date: Tue, 12 Sep 2017 20:53:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Espring-boot-mybatis=E6=A1=88?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/sql/springboot/spring-boot-mybatis.sql | 9 + pom.xml | 4 +- spring-boot-mybatis/pom.xml | 68 +- .../springboot/mybatis/Application.java | 13 + .../mybatis/config/DataSourceConfig.java | 77 +++ .../constants/DataSourceConstants.java | 8 + .../springboot/mybatis/entity/User.java | 179 +++++ .../mybatis/entity/UserExample.java | 640 ++++++++++++++++++ .../springboot/mybatis/mapper/UserMapper.java | 96 +++ .../src/main/resources/application.properties | 5 + .../src/main/resources/mybatis/UserMapper.xml | 286 ++++++++ .../springboot/mybatis/ApplicationTests.java | 35 + .../src/test/resources/mybatis-generator.xml | 46 ++ spring-boot-quickstart/pom.xml | 4 - 14 files changed, 1453 insertions(+), 17 deletions(-) create mode 100644 docs/sql/springboot/spring-boot-mybatis.sql create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/Application.java create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/config/DataSourceConfig.java create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/constants/DataSourceConstants.java create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/User.java create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/UserExample.java create mode 100644 spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/mapper/UserMapper.java create mode 100644 spring-boot-mybatis/src/main/resources/application.properties create mode 100644 spring-boot-mybatis/src/main/resources/mybatis/UserMapper.xml create mode 100644 spring-boot-mybatis/src/test/java/com/rhwayfun/springboot/mybatis/ApplicationTests.java create mode 100644 spring-boot-mybatis/src/test/resources/mybatis-generator.xml diff --git a/docs/sql/springboot/spring-boot-mybatis.sql b/docs/sql/springboot/spring-boot-mybatis.sql new file mode 100644 index 0000000..843b701 --- /dev/null +++ b/docs/sql/springboot/spring-boot-mybatis.sql @@ -0,0 +1,9 @@ +CREATE TABLE `user` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增,主键', + `user_id` bigint(20) DEFAULT NULL COMMENT '用户id', + `user_name` varchar(15) DEFAULT NULL, + `age` int(11) DEFAULT NULL COMMENT '年龄', + `birth` date DEFAULT NULL COMMENT '生日', + PRIMARY KEY (`id`), + UNIQUE KEY `id_UNIQUE` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='用户信息表'; diff --git a/pom.xml b/pom.xml index 7f236a3..7dd0f7b 100644 --- a/pom.xml +++ b/pom.xml @@ -416,14 +416,14 @@ - + org.mybatis.generator mybatis-generator-maven-plugin diff --git a/spring-boot-mybatis/pom.xml b/spring-boot-mybatis/pom.xml index 8009556..7d77630 100644 --- a/spring-boot-mybatis/pom.xml +++ b/spring-boot-mybatis/pom.xml @@ -1,15 +1,61 @@ - - - spring-boot-learning-examples - com.rhwayfun - 0.0.1-SNAPSHOT - - 4.0.0 + + 4.0.0 - spring-boot-mybatis + + spring-boot-learning-examples + com.rhwayfun + 0.0.1-SNAPSHOT + + spring-boot-mybatis + 0.0.1-SNAPSHOT + jar - \ No newline at end of file + + + org.springframework.boot + spring-boot-starter + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + + mysql + mysql-connector-java + + + + com.alibaba + druid + + + + org.springframework.boot + spring-boot-configuration-processor + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.jacoco + jacoco-maven-plugin + + + + diff --git a/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/Application.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/Application.java new file mode 100644 index 0000000..18fa00e --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/Application.java @@ -0,0 +1,13 @@ +package com.rhwayfun.springboot.mybatis; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(Application.class, args); + Thread.sleep(Long.MAX_VALUE); + } +} diff --git a/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/config/DataSourceConfig.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/config/DataSourceConfig.java new file mode 100644 index 0000000..f1bc06c --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/config/DataSourceConfig.java @@ -0,0 +1,77 @@ +package com.rhwayfun.springboot.mybatis.config; + +import com.alibaba.druid.pool.DruidDataSource; +import com.rhwayfun.springboot.mybatis.constants.DataSourceConstants; +import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +import javax.sql.DataSource; + +@Configuration +@ConfigurationProperties(prefix = "mybatis.datasource") +@MapperScan(basePackages = { DataSourceConstants.MAPPER_PACKAGE }, sqlSessionFactoryRef = "mybatisSqlSessionFactory") +public class DataSourceConfig { + + private String url; + + private String username; + + private String password; + + @Bean(name = "mybatisDataSource") + public DataSource mybatisDataSource() { + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setDriverClassName("com.mysql.jdbc.Driver"); + dataSource.setUrl(url); + dataSource.setUsername(username); + dataSource.setPassword(password); + return dataSource; + } + + @Bean(name = "mybatisTransactionManager") + public DataSourceTransactionManager mybatisTransactionManager() { + return new DataSourceTransactionManager(mybatisDataSource()); + } + + @Bean(name = "mybatisSqlSessionFactory") + public SqlSessionFactory mybatisSqlSessionFactory(@Qualifier("mybatisDataSource") DataSource mybatisDataSource) + throws Exception { + final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); + sessionFactory.setDataSource(mybatisDataSource); + sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() + .getResources(DataSourceConstants.MAPPER_LOCATION)); + return sessionFactory.getObject(); + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/constants/DataSourceConstants.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/constants/DataSourceConstants.java new file mode 100644 index 0000000..dafcb15 --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/constants/DataSourceConstants.java @@ -0,0 +1,8 @@ +package com.rhwayfun.springboot.mybatis.constants; + +public abstract class DataSourceConstants { + + public static final String MAPPER_PACKAGE = "com.rhwayfun.springboot.mybatis.mapper"; + public static final String MAPPER_LOCATION = "classpath:mybatis/*Mapper*.xml"; + +} diff --git a/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/User.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/User.java new file mode 100644 index 0000000..f6e4586 --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/User.java @@ -0,0 +1,179 @@ +package com.rhwayfun.springboot.mybatis.entity; + +import java.io.Serializable; +import java.util.Date; + +public class User implements Serializable { + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column user.id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private Integer id; + + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column user.user_id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private Long userId; + + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column user.user_name + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private String userName; + + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column user.age + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private Integer age; + + /** + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column user.birth + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private Date birth; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + private static final long serialVersionUID = 1L; + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column user.id + * + * @return the value of user.id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Integer getId() { + return id; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column user.id + * + * @param id the value for user.id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column user.user_id + * + * @return the value of user.user_id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Long getUserId() { + return userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column user.user_id + * + * @param userId the value for user.user_id + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setUserId(Long userId) { + this.userId = userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column user.user_name + * + * @return the value of user.user_name + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public String getUserName() { + return userName; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column user.user_name + * + * @param userName the value for user.user_name + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setUserName(String userName) { + this.userName = userName == null ? null : userName.trim(); + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column user.age + * + * @return the value of user.age + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Integer getAge() { + return age; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column user.age + * + * @param age the value for user.age + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setAge(Integer age) { + this.age = age; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column user.birth + * + * @return the value of user.birth + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Date getBirth() { + return birth; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column user.birth + * + * @param birth the value for user.birth + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setBirth(Date birth) { + this.birth = birth; + } +} \ No newline at end of file diff --git a/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/UserExample.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/UserExample.java new file mode 100644 index 0000000..ec2980f --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/entity/UserExample.java @@ -0,0 +1,640 @@ +package com.rhwayfun.springboot.mybatis.entity; + +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +public class UserExample { + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + protected String orderByClause; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + protected boolean distinct; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + protected List oredCriteria; + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public UserExample() { + oredCriteria = new ArrayList(); + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setOrderByClause(String orderByClause) { + this.orderByClause = orderByClause; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public String getOrderByClause() { + return orderByClause; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public boolean isDistinct() { + return distinct; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public List getOredCriteria() { + return oredCriteria; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void or(Criteria criteria) { + oredCriteria.add(criteria); + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Criteria or() { + Criteria criteria = createCriteriaInternal(); + oredCriteria.add(criteria); + return criteria; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public Criteria createCriteria() { + Criteria criteria = createCriteriaInternal(); + if (oredCriteria.size() == 0) { + oredCriteria.add(criteria); + } + return criteria; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + protected Criteria createCriteriaInternal() { + Criteria criteria = new Criteria(); + return criteria; + } + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + public void clear() { + oredCriteria.clear(); + orderByClause = null; + distinct = false; + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + 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)); + } + + protected void addCriterionForJDBCDate(String condition, Date value, String property) { + if (value == null) { + throw new RuntimeException("Value for " + property + " cannot be null"); + } + addCriterion(condition, new java.sql.Date(value.getTime()), property); + } + + protected void addCriterionForJDBCDate(String condition, List values, String property) { + if (values == null || values.size() == 0) { + throw new RuntimeException("Value list for " + property + " cannot be null or empty"); + } + List dateList = new ArrayList(); + Iterator iter = values.iterator(); + while (iter.hasNext()) { + dateList.add(new java.sql.Date(iter.next().getTime())); + } + addCriterion(condition, dateList, property); + } + + protected void addCriterionForJDBCDate(String condition, Date value1, Date value2, String property) { + if (value1 == null || value2 == null) { + throw new RuntimeException("Between values for " + property + " cannot be null"); + } + addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property); + } + + 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 andUserIdIsNull() { + addCriterion("user_id is null"); + return (Criteria) this; + } + + public Criteria andUserIdIsNotNull() { + addCriterion("user_id is not null"); + return (Criteria) this; + } + + public Criteria andUserIdEqualTo(Long value) { + addCriterion("user_id =", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdNotEqualTo(Long value) { + addCriterion("user_id <>", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdGreaterThan(Long value) { + addCriterion("user_id >", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdGreaterThanOrEqualTo(Long value) { + addCriterion("user_id >=", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdLessThan(Long value) { + addCriterion("user_id <", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdLessThanOrEqualTo(Long value) { + addCriterion("user_id <=", value, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdIn(List values) { + addCriterion("user_id in", values, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdNotIn(List values) { + addCriterion("user_id not in", values, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdBetween(Long value1, Long value2) { + addCriterion("user_id between", value1, value2, "userId"); + return (Criteria) this; + } + + public Criteria andUserIdNotBetween(Long value1, Long value2) { + addCriterion("user_id not between", value1, value2, "userId"); + return (Criteria) this; + } + + public Criteria andUserNameIsNull() { + addCriterion("user_name is null"); + return (Criteria) this; + } + + public Criteria andUserNameIsNotNull() { + addCriterion("user_name is not null"); + return (Criteria) this; + } + + public Criteria andUserNameEqualTo(String value) { + addCriterion("user_name =", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameNotEqualTo(String value) { + addCriterion("user_name <>", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameGreaterThan(String value) { + addCriterion("user_name >", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameGreaterThanOrEqualTo(String value) { + addCriterion("user_name >=", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameLessThan(String value) { + addCriterion("user_name <", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameLessThanOrEqualTo(String value) { + addCriterion("user_name <=", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameLike(String value) { + addCriterion("user_name like", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameNotLike(String value) { + addCriterion("user_name not like", value, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameIn(List values) { + addCriterion("user_name in", values, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameNotIn(List values) { + addCriterion("user_name not in", values, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameBetween(String value1, String value2) { + addCriterion("user_name between", value1, value2, "userName"); + return (Criteria) this; + } + + public Criteria andUserNameNotBetween(String value1, String value2) { + addCriterion("user_name not between", value1, value2, "userName"); + return (Criteria) this; + } + + public Criteria andAgeIsNull() { + addCriterion("age is null"); + return (Criteria) this; + } + + public Criteria andAgeIsNotNull() { + addCriterion("age is not null"); + return (Criteria) this; + } + + public Criteria andAgeEqualTo(Integer value) { + addCriterion("age =", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeNotEqualTo(Integer value) { + addCriterion("age <>", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeGreaterThan(Integer value) { + addCriterion("age >", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeGreaterThanOrEqualTo(Integer value) { + addCriterion("age >=", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeLessThan(Integer value) { + addCriterion("age <", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeLessThanOrEqualTo(Integer value) { + addCriterion("age <=", value, "age"); + return (Criteria) this; + } + + public Criteria andAgeIn(List values) { + addCriterion("age in", values, "age"); + return (Criteria) this; + } + + public Criteria andAgeNotIn(List values) { + addCriterion("age not in", values, "age"); + return (Criteria) this; + } + + public Criteria andAgeBetween(Integer value1, Integer value2) { + addCriterion("age between", value1, value2, "age"); + return (Criteria) this; + } + + public Criteria andAgeNotBetween(Integer value1, Integer value2) { + addCriterion("age not between", value1, value2, "age"); + return (Criteria) this; + } + + public Criteria andBirthIsNull() { + addCriterion("birth is null"); + return (Criteria) this; + } + + public Criteria andBirthIsNotNull() { + addCriterion("birth is not null"); + return (Criteria) this; + } + + public Criteria andBirthEqualTo(Date value) { + addCriterionForJDBCDate("birth =", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthNotEqualTo(Date value) { + addCriterionForJDBCDate("birth <>", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthGreaterThan(Date value) { + addCriterionForJDBCDate("birth >", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthGreaterThanOrEqualTo(Date value) { + addCriterionForJDBCDate("birth >=", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthLessThan(Date value) { + addCriterionForJDBCDate("birth <", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthLessThanOrEqualTo(Date value) { + addCriterionForJDBCDate("birth <=", value, "birth"); + return (Criteria) this; + } + + public Criteria andBirthIn(List values) { + addCriterionForJDBCDate("birth in", values, "birth"); + return (Criteria) this; + } + + public Criteria andBirthNotIn(List values) { + addCriterionForJDBCDate("birth not in", values, "birth"); + return (Criteria) this; + } + + public Criteria andBirthBetween(Date value1, Date value2) { + addCriterionForJDBCDate("birth between", value1, value2, "birth"); + return (Criteria) this; + } + + public Criteria andBirthNotBetween(Date value1, Date value2) { + addCriterionForJDBCDate("birth not between", value1, value2, "birth"); + return (Criteria) this; + } + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table user + * + * @mbg.generated do_not_delete_during_merge Tue Sep 12 20:40:14 CST 2017 + */ + public static class Criteria extends GeneratedCriteria { + + protected Criteria() { + super(); + } + } + + /** + * This class was generated by MyBatis Generator. + * This class corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + 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/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/mapper/UserMapper.java b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/mapper/UserMapper.java new file mode 100644 index 0000000..d38c988 --- /dev/null +++ b/spring-boot-mybatis/src/main/java/com/rhwayfun/springboot/mybatis/mapper/UserMapper.java @@ -0,0 +1,96 @@ +package com.rhwayfun.springboot.mybatis.mapper; + +import com.rhwayfun.springboot.mybatis.entity.User; +import com.rhwayfun.springboot.mybatis.entity.UserExample; +import java.util.List; +import org.apache.ibatis.annotations.Param; + +public interface UserMapper { + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + long countByExample(UserExample example); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int deleteByExample(UserExample example); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int deleteByPrimaryKey(Integer id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int insert(User record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int insertSelective(User record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + List selectByExample(UserExample example); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + User selectByPrimaryKey(Integer id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int updateByExample(@Param("record") User record, @Param("example") UserExample example); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int updateByPrimaryKeySelective(User record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table user + * + * @mbg.generated Tue Sep 12 20:40:14 CST 2017 + */ + int updateByPrimaryKey(User record); +} \ No newline at end of file diff --git a/spring-boot-mybatis/src/main/resources/application.properties b/spring-boot-mybatis/src/main/resources/application.properties new file mode 100644 index 0000000..34d366e --- /dev/null +++ b/spring-boot-mybatis/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=spring-boot-mybatis + +mybatis.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull +mybatis.datasource.username=springboot +mybatis.datasource.password=springboot \ No newline at end of file diff --git a/spring-boot-mybatis/src/main/resources/mybatis/UserMapper.xml b/spring-boot-mybatis/src/main/resources/mybatis/UserMapper.xml new file mode 100644 index 0000000..b96ad0e --- /dev/null +++ b/spring-boot-mybatis/src/main/resources/mybatis/UserMapper.xml @@ -0,0 +1,286 @@ + + + + + + + + + + + + + + + + + + + + + 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, user_id, user_name, age, birth + + + + + + delete from `user` + where id = #{id,jdbcType=INTEGER} + + + + delete from `user` + + + + + + + + SELECT LAST_INSERT_ID() + + insert into `user` (user_id, user_name, age, + birth) + values (#{userId,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, + #{birth,jdbcType=DATE}) + + + + + SELECT LAST_INSERT_ID() + + insert into `user` + + + user_id, + + + user_name, + + + age, + + + birth, + + + + + #{userId,jdbcType=BIGINT}, + + + #{userName,jdbcType=VARCHAR}, + + + #{age,jdbcType=INTEGER}, + + + #{birth,jdbcType=DATE}, + + + + + + + update `user` + + + id = #{record.id,jdbcType=INTEGER}, + + + user_id = #{record.userId,jdbcType=BIGINT}, + + + user_name = #{record.userName,jdbcType=VARCHAR}, + + + age = #{record.age,jdbcType=INTEGER}, + + + birth = #{record.birth,jdbcType=DATE}, + + + + + + + + + update `user` + set id = #{record.id,jdbcType=INTEGER}, + user_id = #{record.userId,jdbcType=BIGINT}, + user_name = #{record.userName,jdbcType=VARCHAR}, + age = #{record.age,jdbcType=INTEGER}, + birth = #{record.birth,jdbcType=DATE} + + + + + + + update `user` + + + user_id = #{userId,jdbcType=BIGINT}, + + + user_name = #{userName,jdbcType=VARCHAR}, + + + age = #{age,jdbcType=INTEGER}, + + + birth = #{birth,jdbcType=DATE}, + + + where id = #{id,jdbcType=INTEGER} + + + + update `user` + set user_id = #{userId,jdbcType=BIGINT}, + user_name = #{userName,jdbcType=VARCHAR}, + age = #{age,jdbcType=INTEGER}, + birth = #{birth,jdbcType=DATE} + where id = #{id,jdbcType=INTEGER} + + \ No newline at end of file diff --git a/spring-boot-mybatis/src/test/java/com/rhwayfun/springboot/mybatis/ApplicationTests.java b/spring-boot-mybatis/src/test/java/com/rhwayfun/springboot/mybatis/ApplicationTests.java new file mode 100644 index 0000000..26657b9 --- /dev/null +++ b/spring-boot-mybatis/src/test/java/com/rhwayfun/springboot/mybatis/ApplicationTests.java @@ -0,0 +1,35 @@ +package com.rhwayfun.springboot.mybatis; + +import com.rhwayfun.springboot.mybatis.entity.User; +import com.rhwayfun.springboot.mybatis.mapper.UserMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Date; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringRunner.class) +@SpringBootTest +@DirtiesContext +public class ApplicationTests { + + @Autowired + private UserMapper userMapper; + + @Test + public void insertTest() { + User user = new User(); + user.setUserId(1L); + user.setUserName("springboot"); + user.setAge(1); + user.setBirth(new Date()); + assertTrue(userMapper.insert(user) > 0); + } + +} diff --git a/spring-boot-mybatis/src/test/resources/mybatis-generator.xml b/spring-boot-mybatis/src/test/resources/mybatis-generator.xml new file mode 100644 index 0000000..ece2167 --- /dev/null +++ b/spring-boot-mybatis/src/test/resources/mybatis-generator.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
\ No newline at end of file diff --git a/spring-boot-quickstart/pom.xml b/spring-boot-quickstart/pom.xml index 500be6b..60ed1a9 100644 --- a/spring-boot-quickstart/pom.xml +++ b/spring-boot-quickstart/pom.xml @@ -37,10 +37,6 @@ org.springframework.boot spring-boot-maven-plugin
- org.jacoco jacoco-maven-plugin