本文并非具体的细节,而是主要的配置步骤
概述
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 名字只需要类名即可
参考资料
- 无