MyBatis --- 配置步骤
本文并非具体的细节,而是主要的配置步骤
概述
MyBatis 是半自动的ORM 框架,在MyBatis 整合 Spring Boot 的时候步骤比较繁琐,所以写下此篇纪录一下步骤。
使用 MyBatis 框架需要了解 Entity, Dao, Mapper ,Service 这几个包的作用。例如一下几个类 :
Entity 包下的某个bean
public class GoodCategory implements Serializable {
private Integer id; private String name; private static final long serialVersionUID = 1L; 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();
} @Override
public String toString() {
return "GoodCategory{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Dao
public interface GoodCategoryMapper {
long countByExample(GoodCategoryExample example); int deleteByExample(GoodCategoryExample example); int deleteByPrimaryKey(Integer id); int insert(GoodCategory record); int insertSelective(GoodCategory record); List<GoodCategory> selectByExample(GoodCategoryExample example); GoodCategory selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") GoodCategory record, @Param("example") GoodCategoryExample example); int updateByExample(@Param("record") GoodCategory record, @Param("example") GoodCategoryExample example); int updateByPrimaryKeySelective(GoodCategory record); int updateByPrimaryKey(GoodCategory record);
}
Mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vb.seckillserver.dao.good.GoodCategoryMapper">
<resultMap id="BaseResultMap" type="GoodCategory">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="CHAR" property="name" />
</resultMap>
...
...
</mapper>
Service
public interface GoodService {
List<GoodCategory> getAllGoodCatory();
List<ProductBrank> getAllGoodBrand();
List<GoodStore> getAllGoodStore();
List<GoodType> getAllGoodType();
List<GoodBean> getGoodProSkuByCateId(int categoryId);
List<GoodBean> getGoodProSkuByBrandId(int brandId);
boolean createProduct(GoodProduct product);
}
生成 Dao,Entity 和 Map 文件可以使用MyBatis Generator 工具生成,步骤参见该篇文章
步骤
依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
属性配置
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>
<!--使用jdbc的getGeneratekeys获取自增主键值-->
<setting name="useGeneratedKeys" value="true"/> <!--使用列别名替换列名 默认值为true
select name as title(实体中的属性名是title) form table;
开启后mybatis会自动帮我们把表中name的值赋到对应实体的title属性中
-->
<setting name="useColumnLabel" value="true"/> <!--开启驼峰命名转换 例如: Table field:create_time 到 Entity(createTime)-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--懒加载,为了解决多表连接问题,当一个类(bean)嵌套一个类的情况时,解决N+1 问题-->
<!--<setting name="lazyLoadingEnabled" value="true"/>-->
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
</settings> </configuration>
application.properties 里面配置,指定map文件位置和其他属性.
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=12345678 mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:/mapper/**/*.xml
mybatis.type-aliases-package=com.vb.seckillserver.entity
Application 文件上注解
@SpringBootApplication
@MapperScan("com.vb.seckillserver.dao")
public class SeckillServerApplication { public static void main(String[] args) {
SpringApplication.run(SeckillServerApplication.class, args);
} }
编写测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class GoodServiceImplTest { @Autowired
private GoodService mGoodService; @Test
public void getAllGoodCatory() {
List<GoodCategory> allGoodCatory = mGoodService.getAllGoodCatory();
for (GoodCategory category : allGoodCatory) {
System.out.println("xyz ;"+category.getName());
}
}
}
补充
- map文件中,namespace 需要指定全限定文件名,而要是有指定Entity 缩写简称,resultMap 中 type 名字只需要类名即可
参考资料
- 无
MyBatis --- 配置步骤的更多相关文章
- spring MVC、mybatis配置读写分离
spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数 ...
- Mybatis配置
首先导入mybatis-3.2.3.jar包 还有连接数据库的驱动包 工程中必须导入的三个包(对应的包附件中可以下载): mybatis-3.2.3.jar sqljdbc.jar log ...
- log4j.properties 详解与配置步骤
一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...
- log4j.properties 详解与配置步骤(转)
找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...
- MySQL数据库集群进行正确配置步骤
MySQL数据库集群进行正确配置步骤 2010-06-09 10:47 arrowcat 博客园 字号:T | T 我们今天是要和大家一起分享的是对MySQL数据库集群进行正确配置,我前两天在相关网站 ...
- Apache安装配置步骤
注释:这里以Linux 红帽商业版为例~~~~~~~纯手打啊 Apache安装配置步骤 准备:关闭其他虚拟设备 #/etc/init.d/libvirtd stop #/etc/init.d/xend ...
- Windows Live Writer配置步骤
推荐文档: [超详细教程]使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 Live Writer 使用小贴示:发博客时始终使用图片原始 ...
- Oracle 11g客户端在Linux系统上的配置步骤详解
Oracle 11g客户端在Linux系统上的配置步骤详解 2011-07-26 10:47 newhappy2008 CSDN博客 字号:T | T 本文我们主要介绍了Oracle 11g客户端在L ...
- jenkins 邮件配置步骤
一.进行系统管理中的邮件配置步骤: 1.设置Extended E-mail Notification 二.对构建的job 添加邮件发送的步骤: 3.成功截图:
随机推荐
- ASP.NET Core 2 学习笔记(五)静态文件
之前的ASP.NET网站,只要把*.html.*.css.*.jpg.*.png.*.js等静态文件放在项目根目录,默认都可以直接被浏览:但ASP.NET Core 小改了浏览静态文件的方式,默认根目 ...
- NetCore偶尔有用篇:NetCore项目WebApi返回Json属性大小写
一.概述 1.前面文章介绍Controller的大小写问题时,目的只是介绍它的差异性,有同学回复了,这里把它作为一个点写一下吧. 二.默认定义的转换结果 1.写一个返回对象的方法. 2.运行查看结果. ...
- .NET Core 玩一玩 Ocelot API网关
.net 这几年国内确实不好过. 很多都选择转行.不过.net Core跨平台 开源之后 .社区的生态在慢慢建立.往好的趋势发展. 对于坚守在.NET战线的开发者来说 是个挺不错的消息. 特别是微软 ...
- Word发表blog格式模板
一级标题(黑体,二号,加粗) 二级标题(黑体,三号,加粗) 正文(宋体+Times New Roman,小四) 注意事项: 序号列表"不连续"时,不得使用自动序号 连续(word连 ...
- 【文文殿下】CF1098C Construct a tree 题解
题解 挺水的一道题. Rating $ \color{orange} {2300}$ 以下送命题. 首先我们知道,所有子树大小之和就是节点个数加上从根到所有节点的路径长度之和. 他要求度数尽可能小,所 ...
- django模型中, 外键字段使用to_filed属性 指定到所关联主表的某个字段
在django项目的开发过程中,在设计模型时一开始将主键设置成了一个自定义的字段,但是在创建搜索索引时却发现必须要存在一个id的字段,并且为主键(不知道是否是项目一开始就这样配置的原因), 但此时表结 ...
- Sort-242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- 三,PHP缓存机制实现页面静态化
页面静态化思路: 因为新闻这种信息对实时性要求不高,并且比较稳定,所以可以这样做:当地一个用户访问某条新闻后,我们使用ob缓存机制,将内容缓存到html页面.当下一次访问时候,直接访问html页面.这 ...
- CPU的寄存器结构
计算机的硬件有三个基本要素,CPU.内存和I/O.CPU负责解释.执行程序,从内存或I/O输入数据,在内部进行运算,再把运算结果输出到内存或I/O.内存中存放着程序,程序是指令和数据的集合.I/O中临 ...
- jvm高级特性(4)(内存分配回收策略)
JVM高级特性与实践(四):内存分配 与 回收策略 一. 内存分配 和 回收策略 1,对象内存分配的概念: 往大方向讲,它就是在堆上分配(但也可能经过JIT编译后被拆散为标量类型并间接地栈上分配), ...