集成 Mybatis
2019-04-24 本文已影响0人
qyfl
1.添加依赖
pom 依赖
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.添加配置
yml:
# MyBatis
mybatis:
mapper-locations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.qyfl.weichat.pojo
# MyBatis 日志
logging:
level:
com.qyfl.wechat.dao: debug
SpringBootApplication:
@SpringBootApplication
@MapperScan("com.qyfl.wechat.dao")
public class WechatApplication {
public static void main(String[] args) {
SpringApplication.run(WechatApplication.class, args);
}
}
3. 数据库中的表映射出来的对象(POJO)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductCategory {
private Integer categoryId;
private String categoryName;
private Integer categoryType;
private Date createTime;
private Date updateTime;
}
4. DAO
@Repository
public interface ProductCategoryMapper {
ProductCategory selectByPrimaryKey(Integer categoryId);
int insert(ProductCategory productCategory);
}
5. xml
然后就可以在 resources/mapper(这个路径在 yml 中的 mapper-locations 里配置) 下新建 xml 文件了。
resultMap 里的配置要与数据库对应起来。
<?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.qyfl.wechat.dao.ProductCategoryMapper">
<resultMap id="BaseResultMap" type="com.qyfl.wechat.pojo.ProductCategory">
<constructor>
<idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
<arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String"/>
<arg column="category_type" jdbcType="INTEGER" javaType="java.lang.Integer"/>
<arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date"/>
<arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date"/>
</constructor>
</resultMap>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from product_category where category_id = #{categoryId,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.qyfl.wechat.pojo.ProductCategory">
insert into product_category(category_id,category_name,category_type,create_time,update_time)
value(#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR}, #{categoryType,jdbcType=INTEGER}, now(), now())
</insert>
</mapper>
然后就可以在 service 中调用了。
下例是在 SpringBootTest 中测试用例:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryMapperTest {
@Autowired
private ProductCategoryMapper productCategoryMapper;
@Test
@Transactional
public void selectByPrimaryKeyTest() {
ProductCategory category = productCategoryMapper.selectByPrimaryKey(1);
System.out.println(category.toString());
}
@Test
@Transactional
public void insertTest() {
ProductCategory category = new ProductCategory();
category.setCategoryName("女生最爱");
category.setCategoryType(2);
Integer result = productCategoryMapper.insert(category);
System.out.println(result);
}
}