【MyBatis框架入门案例】

MyBatis框架入门案例

数据库MySQL

使用数据库MySQL5.7.27,将 MyBatis 的核心包 mybatis-3.5.7.jar、MySQL 数据库驱动程序jar包mysql-connector-java-5.x.jar 以及 MyBatis 依赖包导入到项目中

student.sql

create database if not exists xhu default charset=utf8;
use xhu;
create table student(no varchar(13) not null,
name varchar(20) not null,
sex varchar(4) not null default '男',
age int default 0,primary key(no))
default charset=utf8;

insert into student values('12345','张三','女',21);
insert into student values('12346','李四','男',22);
insert into student values('12347','王八','男',20);

持久化类

创建持久化类存放xhu.po下。

student.java

package xhu.po;

public class student {
    private String no;
    private String name;
    private String sex;
    private int age;

    public student(String no, String name, String sex, int age) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public student() {
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

sql映射文件

建立SQL映射文件存放xhu.mapper下。

studentmapper.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="xhu.mapper.studentmapper">
    <!--根据学号查询-->
    <select id="selectByNo" parameterType="String" resultType="xhu.po.student">
        select * from student where no=#{no}
    </select>
    <!--查询所有-->
    <select id="selectAll" resultType="xhu.po.student">
        select * from student
    </select>
</mapper>

MyBatis核心配置文档

创建MyBatis核心配置文档放在xhu.config下。

mybatis-config.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">
<configuration>
    <!--数据库连接-->
    <properties>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/xhu?characterEncoding=utf8"/>
    </properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="xhu/mapper/studentmapper.xml"></mapper>
    </mappers>
</configuration>

jsp测试文件

Test.jsp

<%@ page import="java.io.Reader" %>
<%@ page import="org.apache.ibatis.io.Resources" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="xhu.po.student" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: 顾北辰
  Date: 2022/8/7
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    Reader reader= Resources.getResourceAsReader("xhu/config/mybatis-config.xml");
    SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
    SqlSession sqlSession=sqlSessionFactory.openSession();
    student s=sqlSession.selectOne("xhu.mapper.studentmapper.selectByNo","12346");
    out.print(s+"<br>");
    List<student> ls=sqlSession.selectList("xhu.mapper.studentmapper.selectAll");
    for(student list:ls){
        out.print(list+"<br>");
    }
%>
</body>
</html>

测试截图

在这里插入图片描述
菜鸟创作不易,希望过路大神点个赞!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾北辰20

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值