引入MyBatis:SpringBoot+Mybatis+MySQL

0.项目概述
目的是将User类存入MySQL,实现插入与查询操作。
在这里插入图片描述
这里省略了service层。
User类不用使用任何注解,字段如下:
private int id;
private String name;
private int age;
private long phoneNumber;
数据库:

create table user(
 id int(11) PRIMARY KEY,
 name varchar(25) not null,
 age int(4) not null,
 phoneNumber int(11)
);

1.配置项目依赖:
(1)
在这里插入图片描述
(2)
在这里插入图片描述
总的来说,项目依赖比最简单的Web多了mybatis-spring-boot-starter和mysql-connector-java:

<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.3</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

2.配置文件
整个项目需要三种配置文件:
(1)mybatis-config.xml,设置mybatis的运行参数,参数很多,不设置就会采用默认值。
(2)mapper.xml,一张数据表需要一个mapper文件。
(3)application.properties文件,项目默认提供的文件,里面设置数据库连接相关参数。
2.1.mybatis-config.xml
mybatis配置文件官网地址
在resources目录下创建mybatis-config.xml文件
在这里插入图片描述
XML 文件使用的头部声明,它用来验证MyBatis 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">

在这里面可以设置类简称

<?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>
    <!--指定一个包名,MyBatis会在包名下面搜索需要的Java Bean,
    每一个在包中的Java Bean,在没有注解的情况下,会使用Bean的首字母小写的非限定类名来作为它的别名-->
    <typeAliases>
        <package name="com.linglib.mybatisdemo.pojo"/>
    </typeAliases>

</configuration>

2.2.mapper文件
mapper文件的具体编写参考官方文档
在mybatis中,可以不用使用实现类,编写接口代码和建立mapper.xml文件即可操作数据库。
接口代码:

@Mapper
public interface UserMapper {
    //增加用户
    int addUser(User user);

    //根据名字查询用户
    User findUserByName(String name);

    //查询所有用户
    List<User> findAllUser();
}

与mybatis-config.xml头声明类似,mapper.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">

2.2.1.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.linglib.mybatisdemo.mapper.UserMapper">

</mapper>

(1)映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句。
值是接口的全限定名称。
(2)
2.2.2.insert标签
插入数据

<mapper namespace="com.linglib.mybatisdemo.mapper.UserMapper">
    <!--插入数据-->
    <insert id="addUser">
        insert into user set id=#{id},name=#{name},age=#{age},phoneNumber=#{phoneNumber}
    </insert>
</mapper>

(1)id就是接口中的方法名称。
2.2.3.select标签
查询数据,select标签通常与resultMap标签结合使用。
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中。

<mapper namespace="com.linglib.mybatisdemo.mapper.UserMapper">
    <!--插入数据-->
    <insert id="addUser">
        insert into user set id=#{id},name=#{name},age=#{age},phoneNumber=#{phoneNumber}
    </insert>

    <resultMap id="BaseResultMap" type="com.linglib.mybatisdemo.pojo.User">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="phoneNumber" property="phoneNumber"/>
    </resultMap>
    <!--根据名字查询单个语句-->
    <select id="findUserByName" resultMap="BaseResultMap">
      select * from user where name=#{name}
    </select>

    <!--查询所有数据-->
    <select id="findAllUser" resultMap="BaseResultMap">
        select * from user
    </select>
</mapper>

(1)column是数据库字段,property是实体类字段。
2.3.properties

#jdbc:mysql://ip:port/数据库名称?参数选项
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=idea2018
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver

mybatis.config-location=classpath:config/mybatis-config.xml
mybatis.mapper-locations= classpath:mapper/*.xml
#我的8080端口被占用,所以采用8081
server.port=8081

3.配套的其它类
3.1.controller

import com.linglib.mybatisdemo.mapper.UserMapper;
import com.linglib.mybatisdemo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Title UserController
 * @Description TODO
 * @Author ZhangLing
 * @Date 2020/9/27 10:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/add")
    public void addUser(int id,String name,int age,long phoneNumber){
        User user=new User(id,name,age,phoneNumber);
        userMapper.addUser(user);
    }

    @GetMapping("findUser")
    public User findUser(String name){
        return userMapper.findUserByName(name);
    }

    @GetMapping("findAllUser")
    public List<User> findAllUser(){
        return userMapper.findAllUser();
    }
}

4.测试
(1)插入数据
浏览器访问:

http://localhost:8081/user/add?id=1&name=zhang&age=18&phoneNumber=123897
http://localhost:8081/user/add?id=2&name=li&age=19&phoneNumber=89076

在这里插入图片描述
(2)查询数据

http://localhost:8081/user/findUser?name=zhang

结果:
在这里插入图片描述
(3)查询所有数据

http://localhost:8081/user/findAllUser

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值