2021-04-14

mybatis使用mapper配置并使用查询数据

mapper概述
使用mapper主要是作为java方法和sql数据的桥梁,帮助我们更好的使用sql

准备数据源

删除mybatis_demo数据库

drop database if exists mybatis_demo;

创建mybatis_demo数据库

create database mybatis_demo;

使用mybatis_demo数据库

use mybatis_demo;

创建account表

create table user (
id int auto_increment primary key,
username varchar(20),
age int,
score int
);

新增数据

insert into user (id, username, age, score) values
(1,‘peter’, 18, 100), (2,‘pedro’, 24, 200),
(3,‘jerry’, 28, 500), (4,‘mike’, 12, 300),
(5,‘tom’, 27, 1000);
在navicat中运行,即可导入数据库;

注解方式实现java方法和sql语句的连接
首先,先导入驱动mysql需要的架包、mybatis依赖的基础包和创建mybatis配置文件

在pom.xml中导入mysql驱动包:



mysql
mysql-connector-java
8.0.23


导入mybatis基础包和日志依赖包

org.mybatis mybatis 3.5.6 ch.qos.logback logback-classic 1.3.0-alpha5 test 在resources目录下创建mybatis-config.xml配置文件,配置内容如下: <?xml version="1.0" encoding="UTF-8" ?>
        <!-- 数据源 -->
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo?useSSL=false"/>
            <property name="username" value="root"/>
            <property name="password" value="1958410771"></property>
            <!-- mapper配置 -->

        </dataSource>
    </environment>
</environments>
<!-- mapper配置 -->
<mappers>
    <mapper class="mapper.UserMapper"/>
</mappers>
关于mybatis配置文件中各个属性的层次结构请见https://mp.csdn.net/editor/html/115447503;

代码实现
新建mapper 包下 创建UserMapper.java

package mapper;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
//通过select注解为该方法加上对应的sql语句
@Select(“select * from user where id = #{id}”)
//通过用户id查询用户名称
String selectUsernameById(Integer id);
}

创建UserTest.java测试类

public class UserTest {
public static void main(String[] args) throws IOException, SQLException {

    // 读取配置文件mybatis-config.xml
    InputStream configuration = Resources.getResourceAsStream("mybatis-config.xml");

    // 得到 SqlSessionFactory 核心类
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

    // 开始一个 sql 会话
    SqlSession session = sqlSessionFactory.openSession();

    // 得到 mapper
    UserMapper mapper = session.getMapper(UserMapper.class);

    // 调用注解的SQL
    String username = mapper.selectUsernameById(1);
    System.out.println("username: " + username);
    // 关闭会话
    session.close();
}

}
如上代码,通过注解的方式连接了java方法与sql语句操作数据库。

接下来

通过xml文件实现mapper
在resources目录下新建包mapper然后新建UserMapper.xml文件。

<?xml version="1.0" encoding="UTF-8" ?> SELECT age FROM user WHERE id = #{id} namaspace对应的是resources/mapper包下xml文件名

id =对应类中的方法名。mybatis通过他来与sql语句连接

resultType 的值为sql语句的返回值类型

运行结果:

上面的结果是@select注解作为java方法和sql语句的桥梁查询到了我们要的数据;

下面的结果是使用xml配置文件实现java方法和sql语句的桥梁查询到了数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值