Springboot統合mybatis crud実装、mybatisのxml構成の簡単な紹介
4470 ワード
mybatisはjpaのように自動的に表を建てることができなくて、表は早めに建てる必要があります
依存の追加
application.yml構成
mapper構成クラス
domainクラス:
アプリケーションプログラムに@MapperScan(basePackages="com.example.demo.mybatis.mapper")注記を追加してください.basePackagesの後ろにMapperのパスがあります.
xmlを使用してmybatisを使用するには:
ProductCatagoryMapper.xml:
プログラムがxmlを見つけるためにアプリケーションでymlは次の構成を追加します.
sql構築テーブル:
SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `product_category`; CREATE TABLE `product_category` ( `category_id` int(11) NOT NULL AUTO_INCREMENT, `category_name` varchar(255) DEFAULT NULL, `category_type` int(11) DEFAULT NULL, `create_time` datetime DEFAULT NULL, `update_time` datetime DEFAULT NULL, PRIMARY KEY (`category_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
依存の追加
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
application.yml構成
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xxxx?characterEncoding=utf8&useSSL=true
username: xxxx
password: xxxxxxx
mapper構成クラス
public interface Mapper {
// map
@Insert("insert into product_category(category_name,category_type) values (#{category_name,jdbcType=VARCHAR},#{category_type,jdbcType=INTEGER})")
int insertByMap(Map map);// 1, 0
//
@Insert("insert into product_category(category_name,category_type) values (#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
int insertByObject(ProductCategory productCategory);
// @Result ,column =
@Select("select * from product_category where category_type=#{categoryType}")
@Results({@Result(column = "category_type", property = "categoryType"), @Result(column = "category_name", property = "categoryName"), @Result(column = "category_id", property = "categoryId")
})
List findByCategoryType(Integer categoryType);
//
// category_type
@Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType} ")
int updateByCategoryType(@Param("categoryName") String categoryName,@Param("categoryType") Integer categoryType);
//
// categoryType
@Update("update product_category set category_name=#{categoryName} where category_type=#{categoryType} ")
int updateObject(ProductCategory productCategory);
//
@Delete("delete from product_category where category_type=#{categoryType}")
int deleteByCategoryType(Integer categoryType);
// ProductCatagoryMapper.xml crud
// application.yml , ProductCatagoryMapper.xml
// ,
ProductCategory selectByCategoryType(Integer categoryType);
}
domainクラス:
@Data
public class ProductCategory {
private Integer categoryId;
private String categoryName;
private Integer categoryType;
private Date createTime;
private Date updateTime;
public ProductCategory() {
}
public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
アプリケーションプログラムに@MapperScan(basePackages="com.example.demo.mybatis.mapper")注記を追加してください.basePackagesの後ろにMapperのパスがあります.
xmlを使用してmybatisを使用するには:
ProductCatagoryMapper.xml:
プログラムがxmlを見つけるためにアプリケーションでymlは次の構成を追加します.
mybatis:
mapper-locations: classpath:mapper/*.xml
sql構築テーブル:
SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `product_category`; CREATE TABLE `product_category` ( `category_id` int(11) NOT NULL AUTO_INCREMENT, `category_name` varchar(255) DEFAULT NULL, `category_type` int(11) DEFAULT NULL, `create_time` datetime DEFAULT NULL, `update_time` datetime DEFAULT NULL, PRIMARY KEY (`category_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;