文章目录
1 mybatis启动-springboot引入启动
1.1 项目结构
新建一个maven项目,将下面提到的文件按照图片的目录放置,接口的名字和实体类的名字最好保持一致。
- 1 create.sql文件是本项目中使用的sql,在阅读该文章的时候,请读者先执行到数据库中。
- 2 然后定义对应的mybatis.xml配置文件,该文件定义了数据库连接,扫描mapper.xml等等配置
- 3 定义一个与数据库表对应的实体类ItemOrder,和用于java调用操作数据库的ItemOrderMapper接口,和一个与之对应的ItemOrderMapper.xml对象(这里推荐把实体,接口,xml文件放到同一包下,这样方便查找到相应的文件)
- 4 定义springboot的配置文件,application.properties
1.2 maven的 pom.xml引入包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tadpole.learn</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!--下面的xml文件中定义了build标签。这样编译的时候就会把会把包目录下的src/main/java的xml文件编译进去,否则这里会报错,找不到xml文件。 -->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
1.3 创建数据库和数据
CREATE SCHEMA `mybatis_learn` ;
CREATE TABLE `mybatis_learn`.`item_order` (
`id` INT NOT NULL AUTO_INCREMENT,
`order_id` VARCHAR(45) NULL,
`name` VARCHAR(45) NULL,
`price` BIGINT(20) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `order_id` (`order_id` ASC))
COMMENT = '订单表';
INSERT INTO `mybatis_learn`.`item_order` (`order_id`, `name`, `price`) VALUES ('123', '苹果', '20');
INSERT INTO `mybatis_learn`.`item_order` (`order_id`, `name`, `price`) VALUES ('456', '葡萄', '30');
1.4 创建mappe的相关配置
ItemOrder实体
- 注意这个实体映射类可以不定义get和set方法,mybatis会通过SetFieldInvoker设置属性,这里还是建议定义一下get和set方法。也可以使用lomk(挺方便的)。
package com.tadpole.itemorder;
public class ItemOrder {
private int id;
private String orderId;
private String name;
private long price;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getOrderId() {return orderId;}
public void setOrderId(String orderId) {this.orderId = orderId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public long getPrice() { return price; }
public void setPrice(long price) {this.price = price;}
}
ItemOrderMapper接口
package com.tadpole.itemorder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ItemOrderMapper {
ItemOrder selectById(@Param("id") int id);
}
ItemOrderMapper.xml
注意的点
- 1 mapper 的namespace是上面接口类的全路径,包路径加类名(com.tadpole.itemorder.ItemOrderMapper )
- 2 resultMap 定义的一个映射关系,id自己定义的这个映射关系的名字,type是映射到那个实体类,column是sql查询出来的列名,property是实体类的属性名。
- 3 定义一个selcet查询方法,使用上面定义的resultMap,这个select的id需要和 ItemOrderMapper的方法名的一样,这个id在一个mapper.xml文件里面是唯一的,Mapper的方法也是不能重载的。
- 4 在select标签内,sql可以用#{}或则${}引用接口传的参数,如上面的id。@Param(“id”)定义的id名字,就可以在xml中,#{id}拿到对应的参数值。
<?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.tadpole.itemorder.ItemOrderMapper">
<resultMap id="itemOrder" type="com.tadpole.itemorder.ItemOrder">
<id column="id" property="id"/>
<id column="name" property="name"/>
<id column="order_id" property="orderId"/>
<id column="price" property="price"/>
</resultMap>
<select id="selectById" resultMap="itemOrder">
select * from item_order where id = #{id}
</select>
</mapper>
1.5 springboot配置
定义数据源
server.port=8082
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis_learn?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
1.6 启动类
上面引入mybatis的springboot支持自动配置包,只要数据源初始化了,mybatis就会自动初始化,只要打上@Mapper注解就可以使用了,是不是很实用呢,需要查看详细实用可以看下一章哦
package com.tadpole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
LOGGER.info("http://localhost:9112");
}
}