Mybatis xml配置连接数据库,接口式编程

本文参照B站尚硅谷学习(刚刚入门,正在学习,***侵必删!***)
(先单独看xml配置)
首先创建数据库表(如下,在mybatis库下创建一个employee表)
在这里插入图片描述
这是文件目录结构:
(libs目录下导入mybatis包和mysql驱动包)
(conf目录下是需要的xml配置文件,本来conf和src是同级目录,但是这样main类无法访问到xml文件,我就放到src下了)
在这里插入图片描述

创建于数据库的表对应的Bean类。(getter和setter省略)

public class Employee {
    private Integer id;
    private String lastName;
    private  String email;
    private  String gender;}

接下来是mybatis的全局配置文件,(即文件结构里的mybatis_config.xml)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

执行结果
在这里插入图片描述

源码如下:
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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!--&-->
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="conf/employeeMapper.xml"/>
    </mappers>
</configuration>

employeeMapper.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="test.Employee">
<!--    namespace:名称空间   id :唯一标识   resultType:返回类型-->
 <select id="test.Employee.selectEmploy" resultType="test.Employee">
                    select id,last_name  lastName,email,gender from employee where id = #{id}
</select>
</mapper>

Main:

package com.company;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import test.Employee;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * @throws IOException
     * 1根据xml(全局配置文件)创建一个sqlsessionfactory对象
     */
    public static void main(String[] args) throws IOException {
        String resource = "conf/mybatis_config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 2 获取sqlSession实例,直接执行已经映射的sql语句
         * selectOne()参数含义
         *  @param statement Unique identifier matching the statement to use.
         *  @param parameter A parameter object to pass to the statement.
         */
        SqlSession openSession= null;
        try {
            openSession = sqlSessionFactory.openSession();
            Employee employee=openSession.selectOne("selectEmploy",1);
            System.out.println(employee);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            openSession.close();
        }
    }
}

问题汇总:

1、  xml配置文件要和main类在一个目录下,(还有其他解决办法),不然main类无法访问xml
2、 数据库驱动配置,似乎我目前只能写一个,首先遇到中英文“&”都不能识别。。未解决所以我暂时只写了报错的serverTimeZone,

在这里插入图片描述
(接口式编程改进)
新建一个映射接口:
在这里插入图片描述
在这里插入图片描述
在改掉查询主函数里的selectone函数的唯一标识符为配置文件里的,就可以,运行结果与上一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值