整合Spring Boot与MyBatis框架的步骤如下:
步骤1:创建Spring Boot项目
- 在IDE中创建一个新的Spring Boot项目。
步骤2:添加相关依赖
- 在项目的pom.xml文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
步骤3:配置数据库连接
- 在application.properties或application.yml文件中添加数据库连接的相关配置:
这里我就用properties
将username以及password还有db_name 换成自己的
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=db_username
spring.datasource.password=db_password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
yml形式的
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_name?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: your_name
password: your_password
此外还要进行classpath的配置:
如果你想指定MyBatis的Mapper文件所在的类路径,可以在配置文件中添加以下配置:
YAML格式:
mybatis:
mapper-locations: classpath:com/example/mapper/*.xml
Properties格式:
mybatis.mapper-locations=classpath:com/example/mapper/*.xml
上述配置表示Mapper文件位于`com/example/mapper/`目录下,并且文件后缀为`.xml`。
此外,如果你的Mapper文件位于不同的路径下,也可以使用多个`classpath`来指定多个路径:
YAML格式:
mybatis:
mapper-locations:
- classpath:com/example/mapper1/*.xml
- classpath:com/example/mapper2/*.xml
Properties格式:
mybatis.mapper-locations=classpath:com/example/mapper1/*.xml, classpath:com/example/mapper2/*.xml
步骤4:创建实体类和Mapper接口以及配置类
- 在src/main/java目录下创建实体类和Mapper接口,实体类用于映射数据库表的字段,Mapper接口用于定义CRUD操作的方法。
@Configuration
@MapperScan("com.fish.mapper")
public class MybatisConfiguration {
}
步骤5:编写Mapper.xml文件
- 在src/main/resources目录下创建Mapper.xml文件,用于编写SQL语句和映射关系。
<?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.example.mapper.UserMapper">
<resultMap id="UserResultMap" type="com.example.entity.User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="email" property="email"/>
</resultMap>
<select id="findById" parameterType="int" resultMap="UserResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="save" parameterType="com.example.entity.User">
INSERT INTO user (name, email) VALUES (#{name}, #{email})
</insert>
<update id="update" parameterType="com.example.entity.User">
UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}
</update>
<delete id="deleteById" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
当在MyBatis的Mapper.xml文件中编写SQL语句时,我们可以使用一些特殊的语法来实现动态的SQL拼接,比如:
1. If语句
<select id="getUserList" parameterType="com.example.entity.User" resultMap="UserResultMap">
SELECT * FROM user
WHERE 1=1
<if test="name != null">
AND name = #{name}
</if>
<if test="email != null">
AND email = #{email}
</if>
</select>
在上述示例中,<if>标签用于条件判断,根据传入的参数动态拼接SQL语句。如果传入的name不为null,则会拼接AND name = #{name},如果传入的email不为null,则会拼接AND email = #{email}。
2. Where语句与判断语句
<select id="getUserList" parameterType="com.example.entity.User" resultMap="UserResultMap">
SELECT * FROM user
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="email != null">
AND email = #{email}
</if>
</where>
</select>
在上述示例中,<where>标签用于条件拼接,其中的<if>标签用于判断是否拼接该条件。如果传入的name不为null,则会拼接AND name = #{name},如果传入的email不为null,则会拼接AND email = #{email}。使用<where>标签可以在没有满足条件时自动省略WHERE关键字,避免额外的空白。
3. Choose语句(类似于Java中的switch语句)
<select id="getUserList" parameterType="com.example.entity.User" resultMap="UserResultMap">
SELECT * FROM user
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<when test="email != null">
AND email = #{email}
</when>
<otherwise>
AND age >= #{age}
</otherwise>
</choose>
</where>
</select>
在上述示例中,<choose>标签类似于Java中的switch语句,用于根据不同的条件拼接不同的SQL语句。如果传入的name不为null,则条件为AND name = #{name},如果传入的email不为null,则条件为AND email = #{email},否则条件为AND age >= #{age}。
在上述示例的XML文件中,你可以看到以下SQL语句涉及到的语法:
- `<select>`:用于查询数据。
- `<insert>`:用于插入数据。
- `<update>`:用于更新数据。
- `<delete>`:用于删除数据。
- `#{}`:占位符,用于传递参数。
- `resultMap`:映射关系,将数据库字段映射到实体类属性。
- `<result>`:用于定义映射关系,指定数据库列和实体属性的对应关系。