小言_互联网的博客

SpringBoot整合Mybatis

399人阅读  评论(0)

1 SpringBoot整合MyBatis

1.1 整体结构

1.2 需要依赖

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.6</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
		<version>2.1.3</version>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
</dependencies>

 

1.3 准备数据库

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '名字',
  `age` int(10) UNSIGNED NOT NULL COMMENT '年龄',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '小明', 20);
INSERT INTO `user` VALUES (2, '小芳', 18);

SET FOREIGN_KEY_CHECKS = 1;

 

1.4 application.yml配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

1.5 User实体类

package com.buddha.pojo;

import lombok.Data;

@Data
public class User {
   
    private Integer id;
    private String name;
    private Integer age;
}

1.6 UserMapper

package com.buddha.mapper;

import com.buddha.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
   
    List<User> selectUserList();
}

1.7 UserMapper.xml

<?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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user
    </select>
</mapper>

1.8 UserService

package com.buddha.service;

import com.buddha.pojo.User;

import java.util.List;

public interface UserService {
   
    List<User> selectUserList();
}

1.9 UserServiceImpl

package com.buddha.service.impl;

import com.buddha.mapper.UserMapper;
import com.buddha.pojo.User;
import com.buddha.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
   
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> selectUserList() {
   
        return userMapper.selectUserList();
    }
}

 

1.10 UserController

package com.buddha.controller;

import com.buddha.pojo.User;
import com.buddha.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
   
    @Resource
    private UserService userService;

    @RequestMapping("list")
    public void selectUserList() {
   
        List<User> userList = userService.selectUserList();
        System.out.println(userList);
    }
}

 

1.11 检验效果

浏览器地址栏输入:http://localhost:8080/user/list

2 MyBatis查询条件大于等于小于等于常用写法

2.1 使用转义字符方式

含义 原符号 转义字符
小于 < &lt;
小于等于 <= &lt;=
大于 > &gt;
大于等于 >= &gt;=
逻辑与(和) & &amp;
单引号 &apos;
双引号 " &quot;
<?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.buddha.mapper.UserMapper">
    <!-- 查询年龄大于18的数据 -->
    <select id="selectUserList1" resultType="com.buddha.pojo.User">
        select * from user where age &gt; 18
    </select>

    <!-- 查询年龄小于20的数据 -->
    <select id="selectUserList2" resultType="com.buddha.pojo.User">
        select * from user where age &lt; 20
    </select>
</mapper>

效果展示

<!-- 查询年龄大于18的数据 -->
<select id="selectUserList" resultType="com.buddha.pojo.User">
    select * from user where age &gt; 18
</select>

2.2 采用XML语法方式

推荐采用此种方式,可读性好

含义 原符号 转义字符
小于 < <![CDATA[ < ]]>
小于等于 <= <![CDATA[ <= ]]>
大于 > <![CDATA[ > ]]>
大于等于 >= <![CDATA[ >= ]]>
逻辑与(和) & <![CDATA[ & ]]>
单引号 <![CDATA[ ’ ]]>
双引号 " <![CDATA[ " ]]>
<?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.buddha.mapper.UserMapper">
    <!-- 查询年龄大于18的数据 -->
    <select id="selectUserList1" resultType="com.buddha.pojo.User">
        select * from user where age <![CDATA[ > ]]> 18
    </select>

    <!-- 查询年龄小于20的数据 -->
    <select id="selectUserList2" resultType="com.buddha.pojo.User">
        select * from user where age <![CDATA[ < ]]> 20
    </select>
</mapper>

效果展示

<!-- 查询年龄大于18的数据 -->
<select id="selectUserList" resultType="com.buddha.pojo.User">
    select * from user where age <![CDATA[ > ]]> 18
</select>

3 MyBatis获取参数值的方式

3.1 单个参数

UserMapper如下:

public interface UserMapper {
   
    List<User> selectUserList(String name);
}

UserMapper.xml如下:

<?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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user where name = #{name}
    </select>
</mapper>

效果如下:

3.2 两个参数及多个参数

UserMapper如下:

public interface UserMapper {
   
    List<User> selectUserList(String name, Integer age);
}

UserMapper.xml如下:

<?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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user where name = #{name} and age = #{age}
    </select>
</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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user where name = #{param1} and age = #{param2}
    </select>
</mapper>

效果如下:

3.3 Map集合的方式

UserMapper如下:

public interface UserMapper {
   
    List<User> selectUserList(Map<String, Object> params);
}

UserMapper.xml如下:

<?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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user where name = #{name} and age = #{age}
    </select>
</mapper>

3.4 实体方式

UserMapper如下:

public interface UserMapper {
   
    List<User> selectUserList(User user);
}

UserMapper.xml如下:

<?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.buddha.mapper.UserMapper">
    <select id="selectUserList" resultType="com.buddha.pojo.User">
        select * from user where name = #{name} and age = #{age}
    </select>
</mapper>


转载:https://blog.csdn.net/rulaixiong/article/details/128459221
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场