最近工作中用到了mybatis的Java API方式進行開發,順便也整理下該功能的用法,接下來會針對基本部分進行學習:
Mybatis官網給了具體的文檔,但是並沒有對以上用法具體介紹,因此在這里整理下,以便以后工作用到時,可以參考。
本章主要增、刪、改、查的用法進行學習(本章將結合Spring自動注入《Spring(二十三):Spring自動注入的實現方式》),下邊文章分為以下幾個步驟:
1)新建maven,並引入spring/mybatis/mybatis-spring/mysql/druid/junit包,新增配置spring-config.xml/mybaits-config.xml/jdbc.properties配置文件;
2)在mysql中新建mydb,並創建article,article_category兩張表;
3)新增用法;
4)修改用法;
5)查詢用法;
6)刪除用法。
1)新建maven,並引入spring/mybatis/mybatis-spring/mysql/druid/junit包,新增配置spring-config.xml/mybaits-config.xml/jdbc.properties配置文件
新建一個maven項目(Learn-Spring-01,源碼以上處至github:https://github.com/478632418/mybatis-spring-auto-annonation/tree/master/Learn-Spring-01),項目pom.xml中引入以下包:
spring相關包:
。。。。。
<org.springframework.version>5.2.0.RELEASE</org.springframework.version> <org.mybatis.version>3.4.6</org.mybatis.version> <com.alibaba.version>1.1.21</com.alibaba.version> <mysql.version>8.0.11</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> <!-- Sping Test相關依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> 。。。
mybatis、mybatis-spring包
<!--MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${org.mybatis.version}</version> </dependency> <!-- Mybatis自身實現的Spring整合依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency>
mysql和druid包
<!--MySql數據庫驅動 --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${com.alibaba.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
junit包:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
新建以下類:
實體類:
com.dx.test.model.Article.java

package com.dx.test.model; import java.util.Date; /** * 文章詳情 */ public class Article { private Long id; // 文章id private String title; // 文章標題 private String content; // 文章內容 private Integer categoryId; // 文章分類id private String createUser; // 新建用戶 private String createUserId;// 新建用戶id private Date createTime; // 新建時間 private String updateUser; // 修改用戶 private String updateUserId;// 修改用戶id private Date updateTime; // 修改時間 private Integer version; // 樂觀鎖版本號 private ArticleCategory articleCategory; // [擴展字段]文章分類實體 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getCategoryId() { return categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } public String getCreateUserId() { return createUserId; } public void setCreateUserId(String createUserId) { this.createUserId = createUserId; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getUpdateUser() { return updateUser; } public void setUpdateUser(String updateUser) { this.updateUser = updateUser; } public String getUpdateUserId() { return updateUserId; } public void setUpdateUserId(String updateUserId) { this.updateUserId = updateUserId; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } public ArticleCategory getArticleCategory() { return articleCategory; } public void setArticleCategory(ArticleCategory articleCategory) { this.articleCategory = articleCategory; } @Override public String toString() { return "Article [id=" + id + ", title=" + title + ", content=" + content + ", categoryId=" + categoryId + ", articleCategory=" + articleCategory + "]"; } }
com.dx.test.model.ArticleCategory.java

package com.dx.test.model; import java.util.Date; /** * 文章分類 * */ public class ArticleCategory { private Integer id; //文章分類id private String title; // 文章分類名稱 private String imgSrc; // 文章分類banner圖片 private String description; // 文章分類描述 private String createUser; // 新建用戶 private String createUserId;// 新建用戶id private Date createTime; // 新建時間 private String updateUser; // 修改用戶 private String updateUserId;// 修改用戶id private Date updateTime; // 修改時間 private Integer version; // 樂觀鎖版本號 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImgSrc() { return imgSrc; } public void setImgSrc(String imgSrc) { this.imgSrc = imgSrc; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } public String getCreateUserId() { return createUserId; } public void setCreateUserId(String createUserId) { this.createUserId = createUserId; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getUpdateUser() { return updateUser; } public void setUpdateUser(String updateUser) { this.updateUser = updateUser; } public String getUpdateUserId() { return updateUserId; } public void setUpdateUserId(String updateUserId) { this.updateUserId = updateUserId; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } @Override public String toString() { return "ArticleCategory [id=" + id + ", title=" + title + ", imgSrc=" + imgSrc + ", description=" + description + "]"; } }
Mybatis Java API mapper類:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleCategoryMapper.java
Mybatis Java API mapper的sql生成類:
com.dx.test.mapper.sqlprovider.ArticleSqlProvder.java
com.dx.test.mapper.sqlprovider.ArticleCategorySqlProvder.java
Repository層(類上都使用@Repository注解):
com.dx.test.repository.ArticleDao.java
com.dx.test.repository.ArticleCategoryDao.java
Service層(類上都使用@Service注解):
com.dx.test.service.ArticleService.java
com.dx.test.service.ArticleCategoryService.java
包含main入口類:
com.dx.test.App.java
在/src/main/resources下新建jdbc.properties、mybatis-config.xml、spring-config.xml配置文件:
1)jdbc.properties中配置mysql的user/pwd/url/driver/其他先關配置;
2)mybatis-config.xml配置mybatis配置;
3)spring-config.xml配置spring自動注入,以及mybatis整合。
jdbc.properties
#jdbc settings jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false jdbc.username=root jdbc.password=123456 #pool settings jdbc.pool.init=1 jdbc.pool.minIdle=3 jdbc.pool.maxActive=20 #jdbc.testSql=SELECT 'x' jdbc.testSql=SELECT 'x' FROM DUAL
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--配置全局屬性--> <settings> <!-- 打開延遲加載的開關 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 將積極加載改為消極加載(即按需加載) --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 打開全局緩存開關(二級緩存)默認值就是 true --> <setting name="cacheEnabled" value="true"/> <!--使用jdbc的getGeneratekeys獲取自增主鍵值--> <setting name="useGeneratedKeys" value="true"/> <!--使用列別名替換別名 默認true select name as title form table; --> <setting name="useColumnLabel" value="true"/> <!--開啟駝峰命名轉換--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--打印sql日志--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- 引用db.properties配置文件 --> <!-- <properties resource="db.properties"/> <typeAliases> <package name="com.dx.test.model"/> </typeAliases> --> <!-- 對事務的管理和連接池的配置 --> <!-- <environments default="mysql_jdbc"> <environment id="oracle_jdbc"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="mysql_jdbc"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> --> <!-- <mappers> <mapper resource="resources/mapper/TaskAutoExecutePlanMapper.xml"/> </mappers> --> <mappers> <mapper class="com.dx.test.mapper.ArticleCategoryMapper"></mapper> <mapper class="com.dx.test.mapper.ArticleMapper"></mapper> </mappers> </configuration>
備注:
1)因這里采用的是mybatis與spring整合,因此mysql先關數據源信息不在mybatis-config.xml中配置,而是配置spring-config.xml的dataSource的bean下;
2)mappers節點下的mapper需要指定mapper類,又因這里是采用的mybatis java api方式(只需要在ArticleCategoryMapper和ArticleMapper上添加上@Mapper注解即可),因此不再需要配置*Mapper.xml。
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="com.dx.test.repository,com.dx.test.mapper,com.dx.test.service" /> <!-- 配置整合Mybatis過程 --> <!-- 配置數據庫相關參數 properties的屬性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> --> <!--2.配置連接池屬性 --> <!-- 數據源配置, 使用 Druid 數據庫連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass --> <property name="driverClassName" value="${jdbc.driver}"/> <!-- 基本屬性 url、user、password --> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.init}"/> <property name="minIdle" value="${jdbc.pool.minIdle}"/> <property name="maxActive" value="${jdbc.pool.maxActive}"/> <!-- 配置獲取連接等待超時的時間 --> <property name="maxWait" value="60000"/> <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="${jdbc.testSql}"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打開PSCache,並且指定每個連接上PSCache的大小(Oracle使用) --> <!-- <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> --> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat"/> </bean> <!--3.配置SqlSessionFactory對象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入數據庫連接池 --> <property name="dataSource" ref="dataSource"/> <!--配置mybatis全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 因為我們這采用的是Mybatis Java API方式,因此不需要配置 --> <property name="typeAliasesPackage" value="com.dx.test.model"/> <!--掃描entity包,使用別名,多個用;隔開 --> <!--掃描sql配置文件:mapper需要的xml文件 --> <!-- <property name="mapperLocations" value="classpath:mapper/*.xml"/> --> </bean> <!-- Mapper接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dx.test.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 如果想對單個mapper類關聯上sqlSessionFactory,可以這么使用,具體參考:http://mybatis.org/spring/getting-started.html --> <!-- <bean id="articlerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.dx.test.dao.mapper.ArticleMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> --> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
備注:
上邊配置文件spring-config.xml配置包含兩大部分配置:
1)spring自動注入配置;
2)spring注入sqlSessionFatcory到mybatis:
2.1)引入jdbc.properties配置信息到配置文件中;
2.2)dataSource bean的定義,這里采用的是com.alibaba.druid.pool.DruidDataSource作為dataSource實例;
2.3)使用sqlSessionFactory bean,需要引入mybatis-config.xml配置、dataSource bean實例;
2.4)使用org.mybatis.spring.mapper.MapperScannerConfigurer試下掃描mapper包路徑下mapper,並關聯上sqlSesssionFactory;
2.5)事務管理。
2)在mysql中新建mydb,並創建article,article_category兩張表
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for article -- ---------------------------- DROP TABLE IF EXISTS `article`; CREATE TABLE `article` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增id', `category_id` int(11) NOT NULL COMMENT '文章分類id', `title` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '文章標題', `content` varchar(2048) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '文章內容', `create_user` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '新建人', `create_user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '新建人id', `create_time` date NOT NULL COMMENT '新建時間', `update_user` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '修改人', `update_user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '修改人id', `update_time` date NOT NULL COMMENT '修改時間', `version` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `idx_category_id` (`category_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for article_category -- ---------------------------- DROP TABLE IF EXISTS `article_category`; CREATE TABLE `article_category` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id', `title` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '文章分類名稱', `img_src` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '文章分類banner圖片', `description` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '文章分類描述', `create_user` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '新建人', `create_user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '新建人id', `create_time` date NOT NULL COMMENT '新建時間', `update_user` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '修改人', `update_user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '修改人id', `update_time` date NOT NULL COMMENT '修改時間', `version` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
新建數據庫mydb,並新建article(文章表)、article_category(文章分類表)
3)新增用法
Mybatis Java API mapper類:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleCategoryMapper.java
ArticleMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleMapper { /** * 入庫分類 * * @param articleCategory 待入庫實體 * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE, useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") @InsertProvider(type = ArticleSqlProvider.class, method = "insert") int insert(Article article); }
ArticleCategoryMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleCategoryMapper { /** * 入庫文章分類 * * @param articleCategory 待入庫實體 * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE, useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") @InsertProvider(type = ArticleCategorySqlProvider.class, method = "insert") int insert(ArticleCategory articleCategory); }
Mybatis Java API mapper的sql生成類:
com.dx.test.mapper.sqlprovider.ArticleSqlProvder.java
com.dx.test.mapper.sqlprovider.ArticleCategorySqlProvder.java
ArticleSqlProvider.java
package com.dx.test.mapper.sqlprovider; 。。。 public class ArticleSqlProvider { /** * 生成文章入庫SQL * * @param article 待插入文章實體 * @return 生成SQL */ public String insert(final Article article) { return new SQL() { { INSERT_INTO("article"); INTO_COLUMNS("title", "content", "category_id", "create_user", "create_user_id", "create_time","update_user", "update_user_id", "update_time", "version"); INTO_VALUES("#{title}", "#{content}", "#{categoryId}", "#{createUser}", "#{createUserId}","now()", "#{updateUser}", "#{updateUserId}", "now()", "0"); } }.toString(); } }
ArticleCategorySqlProvider.java
package com.dx.test.mapper.sqlprovider; 。。。 public class ArticleCategorySqlProvider { /** * 生成插入文章分類的SQL * @param articleCategory文章分類 * @return 返回插入文章SQL * */ public String insert(ArticleCategory articleCategory) { return new SQL() {{ INSERT_INTO("article_category"); INTO_COLUMNS("title","img_src","description", "create_user", "create_user_id", "create_time","update_user", "update_user_id", "update_time", "version"); INTO_VALUES("#{title}","#{imgSrc}","#{description}","#{createUser}", "#{createUserId}","now()", "#{updateUser}", "#{updateUserId}", "now()", "0"); }}.toString(); } }
Repository層(類上都使用@Repository注解):
com.dx.test.repository.ArticleDao.java
com.dx.test.repository.ArticleCategoryDao.java
ArticleDao.java
package com.dx.test.repository; 。。。 @Repository public class ArticleDao implements ArticleMapper { @Autowired() private ArticleMapper articleMapper; @Override public int insert(Article article) { return this.articleMapper.insert(article); } }
ArticleCategoryDao.java
package com.dx.test.repository; 。。。 @Repository public class ArticleCategoryDao implements ArticleCategoryMapper { @Autowired private ArticleCategoryMapper articleCategoryMapper; /** * 插入文章分類 * @param articleCategory 插入文章分類 * @return 相應條數 * */ @Override public int insert(ArticleCategory articleCategory) { int result= this.articleCategoryMapper.insert(articleCategory); return result; } }
Service層(類上都使用@Service注解):
com.dx.test.service.ArticleService.java
com.dx.test.service.ArticleCategoryService.java
ArticleService.java
package com.dx.test.service; ... @Service public class ArticleService implements ArticleMapper{ @Autowired private ArticleDao articleDao; @Override public int insert(Article article) { return this.articleDao.insert(article); } }
ArticleCategoryService.java
package com.dx.test.service; ... @Service public class ArticleCategoryService implements ArticleCategoryMapper { @Autowired private ArticleCategoryDao articleCategoryDao; @Override public int insert(ArticleCategory articleCategory) { return this.articleCategoryDao.insert(articleCategory); } }
4)修改用法
Mybatis Java API mapper類:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleCategoryMapper.java
ArticleMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleMapper { /** * 根據文章id,刪除文章 * * @param id 文章id * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE) @UpdateProvider(type = ArticleSqlProvider.class, method = "update") int update(Article article); }
ArticleCategoryMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleCategoryMapper { /** * 根據文章id,刪除文章 * * @param id 文章id * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE) @UpdateProvider(type = ArticleCategorySqlProvider.class, method = "update") int update(ArticleCategory articleCategory); }
Mybatis Java API mapper的sql生成類:
com.dx.test.mapper.sqlprovider.ArticleSqlProvder.java
com.dx.test.mapper.sqlprovider.ArticleCategorySqlProvder.java
ArticleSqlProvder.java
package com.dx.test.mapper.sqlprovider; 。。。 public class ArticleSqlProvider { public String update(final Article article) { StringBuilder sql = new StringBuilder(); sql.append("update `article` set `id`=#{id}"); if (article.getCategoryId() != null) { sql.append(", `category_id`=#{categoryId}"); } if (article.getTitle() != null) { sql.append(", `title`=#{title}"); } if (article.getContent() != null) { sql.append(", `content`=#{content}"); } if (article.getCreateUser() != null) { sql.append(", `create_user` = #{createUser}"); } if (article.getCreateUserId() != null) { sql.append(", `create_user_id` = #{createUserId}"); } sql.append(", `update_time` = now()"); if (article.getUpdateUser() != null) { sql.append(", `update_user` = #{updateUser}"); } if (article.getUpdateUserId() != null) { sql.append(", `update_user_id` = #{updateUserId}"); } sql.append(", `version` = `version` + 1"); sql.append(" WHERE `id` = #{id} AND `version` = #{version}"); return sql.toString(); } }
ArticleCategorySqlProvder.java
package com.dx.test.mapper.sqlprovider; 。。。 public class ArticleCategorySqlProvider { /** * 生成修改文章分類SQL * @param articleCategory 文章分類實體 * @return 返回修改文章分類SQL * */ public String update(ArticleCategory articleCategory) { StringBuilder sql=new StringBuilder(); sql.append("update `article_category` set id=#{id}"); if (articleCategory.getTitle() != null) { sql.append(", `title`=#{title}"); } if (articleCategory.getImgSrc() != null) { sql.append(", `img_src`=#{imgSrc}"); } if(articleCategory.getDescription()!=null) { sql.append(", `description`=#{description}"); } if (articleCategory.getCreateUser() != null) { sql.append(", `create_user` = #{createUser}"); } if (articleCategory.getCreateUserId() != null) { sql.append(", `create_user_id` = #{createUserId}"); } sql.append(", `update_time` = now()"); if (articleCategory.getUpdateUser() != null) { sql.append(", `update_user` = #{updateUser}"); } if (articleCategory.getUpdateUserId() != null) { sql.append(", `update_user_id` = #{updateUserId}"); } sql.append(", `version` = `version` + 1"); sql.append(" WHERE `id` = #{id} AND `version` = #{version}"); return sql.toString(); } }
5)查詢用法
Mybatis Java API mapper類:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleCategoryMapper.java
ArticleMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleMapper { /** * 根據文章id,查詢文章詳情 * * @param id 文章id * @return 返回查詢到的文章詳情 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @Results(id = "articleResultWithCategory", value = { @Result(property = "id", column = "id", id = true), @Result(property = "categoryId", column = "category_id"), @Result(property = "articleCategory", javaType = ArticleCategory.class, one = @One(select = "com.dx.test.mapper.ArticleCategoryMapper.getById"), column = "category_id"), @Result(property = "title", column = "title"), @Result(property = "content", column = "content"), @Result(property = "createUser", column = "create_user"), @Result(property = "createUserId", column = "create_user_id"), @Result(property = "createTime", column = "create_time"), @Result(property = "updateUser", column = "update_user"), @Result(property = "updateUserId", column = "update_user_id"), @Result(property = "updateTime", column = "update_time"), @Result(property = "version", column = "version") }) @Select({ "select * from article where id=#{id}" }) Article getById(@Param("id") Long id); /** * 根據文章id,查詢文章詳情 * * @param id 文章id * @return 返回查詢到的文章詳情 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @Results(id = "articleResultWithoutCategory", value = { @Result(property = "id", column = "id", id = true), @Result(property = "categoryId", column = "category_id"), @Result(property = "title", column = "title"), @Result(property = "content", column = "content"), @Result(property = "createUser", column = "create_user"), @Result(property = "createUserId", column = "create_user_id"), @Result(property = "createTime", column = "create_time"), @Result(property = "updateUser", column = "update_user"), @Result(property = "updateUserId", column = "update_user_id"), @Result(property = "updateTime", column = "update_time"), @Result(property = "version", column = "version") }) @Select({ "select * from article where id=#{id}" }) Article getByIdWithoutCategory(@Param("id") Long id); /** * 根據條件查詢文章列表 * * @param article 查詢條件 * @return 查詢到的文章列表 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @SelectProvider(type = ArticleSqlProvider.class, method = "queryList") @ResultMap(value = "articleResultWithoutCategory") List<Article> queryList(Article article); }
ArticleCategoryMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleCategoryMapper { /** * 根據文章分類id,查詢文件分類詳情 * @param id 文章分類id * @return 查詢到的文章分類詳情 * */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @Results(id="articleCategoryResult",value = { @Result(property = "id",column = "id",id = true), @Result(property = "title",column = "title"), @Result(property = "imgSrc",column = "img_src"), @Result(property = "description",column = "description"), @Result(property = "createUser",column = "create_user"), @Result(property = "createUserId",column = "create_user_id"), @Result(property = "createTime",column = "create_time"), @Result(property = "updateUser",column = "update_user"), @Result(property = "updateUserId",column = "update_user_id"), @Result(property = "updateTime",column = "update_time"), @Result(property = "version",column = "version") }) @Select("select * from article_category where id=#{id}") ArticleCategory getById(Integer id); @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @ResultMap(value ="articleCategoryResult") @SelectProvider(type = ArticleCategorySqlProvider.class, method = "queryList") List<ArticleCategory> queyrList(ArticleCategory articleCategory); }
Mybatis Java API mapper的sql生成類:
com.dx.test.mapper.sqlprovider.ArticleSqlProvder.java
com.dx.test.mapper.sqlprovider.ArticleCategorySqlProvder.java
ArticleSqlProvder.java
package com.dx.test.mapper.sqlprovider; ... public class ArticleSqlProvider { public String queryList(Article article) { StringBuilder sql=new StringBuilder(); sql.append("select * from `article_category` where 1=1 "); if(article.getId()!=null) { sql.append(" AND `id`=#{id}"); } if (article.getCategoryId() != null) { sql.append(" AND `category_id`=#{categoryId}"); } if (article.getTitle() != null) { sql.append(" AND `title` like CONCAT(CONCAT('%', #{title}), '%')"); } if (article.getCreateTime() != null) { sql.append(" AND `create_time` = #{createTime}"); } if (article.getCreateUser() != null) { sql.append(" AND `create_user` = #{createUser}"); } if (article.getCreateUserId() != null) { sql.append(" AND `create_user_id` = #{createUserId}"); } if (article.getUpdateTime() != null) { sql.append(" AND `update_time` = #{updateTime}"); } if (article.getUpdateUser() != null) { sql.append(" AND `update_user` = #{updateUser}"); } if (article.getUpdateUserId() != null) { sql.append(" AND `update_user_id` = #{updateUserId}"); } if (article.getVersion() != null) { sql.append(" AND `version` = #{version}"); } sql.append(" ORDER BY `id` DESC"); return sql.toString(); } }
ArticleCategorySqlProvder.java
package com.dx.test.mapper.sqlprovider; ... public class ArticleCategorySqlProvider { public String queryList(ArticleCategory articleCategory) { StringBuilder sql=new StringBuilder(); sql.append("select * from `article_category` where 1=1 "); if(articleCategory.getId()!=null) { sql.append(" AND `id`=#{id}"); } if (articleCategory.getTitle() != null) { sql.append(" AND `title` like CONCAT(CONCAT('%', #{title}), '%')"); } if (articleCategory.getCreateTime() != null) { sql.append(" AND `create_time` = #{createTime}"); } if (articleCategory.getCreateUser() != null) { sql.append(" AND `create_user` = #{createUser}"); } if (articleCategory.getCreateUserId() != null) { sql.append(" AND `create_user_id` = #{createUserId}"); } if (articleCategory.getUpdateTime() != null) { sql.append(" AND `update_time` = #{updateTime}"); } if (articleCategory.getUpdateUser() != null) { sql.append(" AND `update_user` = #{updateUser}"); } if (articleCategory.getUpdateUserId() != null) { sql.append(" AND `update_user_id` = #{updateUserId}"); } if (articleCategory.getVersion() != null) { sql.append(" AND `version` = #{version}"); } sql.append(" ORDER BY `id` DESC"); return sql.toString(); } }
6)刪除用法
Mybatis Java API mapper類:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleCategoryMapper.java
ArticleMapper.java
package com.dx.test.mapper; 。。。 @Mapper public interface ArticleMapper { /** * 根據文章id,刪除文章 * * @param id 文章id * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE) @Delete("delete from article where id=#{id}") int delete(Long id); }
ArticleCategoryMapper.java
package com.dx.test.mapper; ... @Mapper public interface ArticleCategoryMapper { /** * 根據文章分類id,刪除文章分類 * * @param id 文章分類id * @return 影響條數 */ @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE) @Delete("delete from article_category where id=#{id}") int delete(Integer id); }
7)入口App.java類和測試類AppTest.java
App.java
package com.dx.test; import java.util.Date; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dx.test.model.Article; import com.dx.test.model.ArticleCategory; import com.dx.test.service.ArticleCategoryService; import com.dx.test.service.ArticleService; /** * Hello SpringFramework! */ public class App { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-config.xml"); ArticleService articleService = applicationContext.getBean(ArticleService.class); ArticleCategoryService articleCategoryService = applicationContext.getBean(ArticleCategoryService.class); ArticleCategory articleCategory=new ArticleCategory(); articleCategory.setTitle("test category"); articleCategory.setImgSrc("http://im.dic/img/abd.gif"); articleCategory.setDescription("description"); articleCategory.setCreateUser("test"); articleCategory.setCreateUserId("11"); articleCategory.setCreateTime(new Date()); articleCategoryService.insert(articleCategory); Article waitingInsertArticle=new Article(); waitingInsertArticle.setCategoryId(articleCategory.getId()); waitingInsertArticle.setTitle("test"); waitingInsertArticle.setContent("test content"); waitingInsertArticle.setCreateUser("test"); waitingInsertArticle.setCreateUserId("11"); waitingInsertArticle.setCreateTime(new Date()); int result=articleService.insert(waitingInsertArticle); System.out.println(result); Article article = articleService.getById(waitingInsertArticle.getId()); System.out.println(article); } }
AppTest.java
package com.dx.test; import java.util.Date; import java.util.List; import java.util.Random; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dx.test.model.ArticleCategory; import com.dx.test.service.ArticleCategoryService; /** * Unit test for simple App. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "classpath:spring-config.xml" }) public class AppTest { @Autowired private ArticleCategoryService articleCategoryService; private ArticleCategory articleCategory; private final String title = "category test title"; private final String titleNew = "category test title new"; @Before public void testArticleCategoryCreate() { ArticleCategory articleCategory = new ArticleCategory(); articleCategory.setTitle(title); articleCategory.setDescription("category description"); articleCategory.setImgSrc("http://www.test.com/img/category/img-" + new Random().nextInt(1000) + ".gif"); articleCategory.setCreateTime(new Date()); articleCategory.setCreateUser("create user"); articleCategory.setCreateUserId("user-" + new Random().nextInt(1000)); int result = this.articleCategoryService.insert(articleCategory); this.articleCategory = articleCategory; Assert.assertEquals(result, 1); } @Test public void testSelectById() { ArticleCategory articleCategory = this.articleCategoryService.getById(this.articleCategory.getId()); Assert.assertEquals(articleCategory.getId(), this.articleCategory.getId()); Assert.assertEquals(articleCategory.getTitle(), this.title); } @Test public void testQueryList() { ArticleCategory queryArticleCategory = new ArticleCategory(); queryArticleCategory.setTitle("test"); List<ArticleCategory> queryResultList = this.articleCategoryService.queyrList(queryArticleCategory); Assert.assertFalse(queryResultList.size() == 0); } @Test public void testUpdate() { ArticleCategory articleCategory = this.articleCategoryService.getById(this.articleCategory.getId()); Assert.assertEquals(articleCategory.getId(), this.articleCategory.getId()); Assert.assertEquals(articleCategory.getTitle(), this.title); articleCategory.setTitle(this.titleNew); int result=this.articleCategoryService.update(articleCategory); Assert.assertEquals(result, 1); articleCategory = this.articleCategoryService.getById(this.articleCategory.getId()); Assert.assertEquals(articleCategory.getId(), this.articleCategory.getId()); Assert.assertEquals(articleCategory.getTitle(), this.title); } @After public void testDelete() { int result = this.articleCategoryService.delete(this.articleCategory.getId()); Assert.assertEquals(result, 1); } }