Mybatis连接数据库

一,创建项目,导入jar

Mybatis连接数据库_第1张图片

二,编写SqlMapConfig.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>
    <!--1.声明数据库连接配置-->
    <environments default="development">
        <!--一套开发者环境-->
        <environment id="development">
            <!--默认JDBC事物管理取消单条sql的自动提交-->
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/java"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--<mapper resource="mapper/StudentMapper.xml"/>-->
        <mapper class="com.zrgm.dao.IStudentDao"/>
    </mappers>
</configuration>

三,测试数据库是否连接正常

public class test1 {

    @Test
    public void test1() throws IOException {
        // 1. 先获取会话工厂构造器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 指定连接配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");

        // 2. 根据构造器,构造会话工厂
        SqlSessionFactory factory = builder.build(is);

        // 3. 开启连接会话
        SqlSession session = factory.openSession();

        System.out.println("连接: ..\n" + session);


    }
}

四,编写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">

<mapper namespace="StudentMapper">
    <insert id="addStu" parameterType="com.zrgm.bean.Student">
        insert into tb_student(name,sex,age) values (#{name},#{sex},#{age})
    </insert>
    <delete id="delStu" parameterType="java.lang.Integer">
        delete from tb_student where id = #{id}
    </delete>
    <update id="updStu" parameterType="com.zrgm.bean.Student">
        update tb_student set name = #{name},sex = #{sex},age=#{age} where id= #{id}
    </update>
    <select id="findStuById" parameterType="Integer" resultType="com.zrgm.bean.Student">
        select * from tb_student where id=#{id}
    </select>
</mapper>

六,测试sql执行是否正常

public class test2 {
    @Test
    public void test2() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = builder.build(is);
        SqlSession sqlSession = factory.openSession();

        //删除
        Student student = sqlSession.selectOne("StudentMapper.findStuById",5);
        System.out.println("查询的对象属性为:" + student.getName() + "---" + student.getSex() + "---" + student.getAge());

        int delete = sqlSession.delete("StudentMapper.delStu", student);
        System.out.println("修改成功" + delete);
        sqlSession.commit();
        sqlSession.close();
    }

}

你可能感兴趣的:(Mybatis连接数据库)