Mybatis配置全过程----整理的资料,框架笔记,一点就会,不懂私聊,毕竟是框架,要有一定的基础哟

  1. 在pom.xml中配置

<dependency>
    <
groupId>org.mybatis</groupId>
    <
artifactId>mybatis</artifactId>
    <
version>3.5.6</version>
</
dependency>
<
dependency>
    <
groupId>mysql</groupId>
    <
artifactId>mysql-connector-java</artifactId>
    <
version>8.0.22</version>
</
dependency>

2.xml中构建SqlSessionFactory


①配置xml,在src/main/resource下创建mybaties-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,代表唯一标识
<environment id="development">
//采用JDBC方式对数据库事物进行管理 commit/rollback
      <transactionManager type="JDBC"/>
//对数据源的管理,POOLED代表使用基于连接池的方式进行连接
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatisvip10?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="1234"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

3.进行测试用例


①导入

<dependency>
    <
groupId>junit</groupId>
    <
artifactId>junit</artifactId>
    <
version>4.12</version>
    <
scope>test</scope>
</
dependency>

   ②建一个junit的类进行测试用例

                   test下面建一个MybatisDemo

4.针对SqlSessionFactory进行封装处理,要保证它全局唯一,(创建工具类)

                   java.com.softeem.util下创建MybatisUtil类:

                                      public class MybatisUtil {

    private static SqlSessionFactory = null;



    static {

        try{

            //加载配置文件

            Reader reader = Resources.getResourceAsReader("mybaties-config.xml");

            //利用构造者模式根据配置文件初始化sqlSessionFactory对象

            sqlSessionFactory  = new SqlSessionFactoryBuilder().build(reader);

        }catch (IOException e){

            e.printStackTrace();

        }

    }

    //创建SqlSesion对象

    public static SqlSession createSession(){

        return sqlSessionFactory.openSession();

    }

    //关闭SqlSession会话

    public static void closeSession(SqlSession sqlSession){

        sqlSession.close();

    }

}
5.根据数据库建表,然后再com.softeem.entity下创建实体类,例如:Student, Teacher。。。
6.配置映射文件(核心)用来操作数据库(JDBC)
                   

<?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="XXX">
    <
select id="queryAll" resultType="com.softeem.entity.Student">
        select id,nickname,sex,moble,birthday
        FROM tablename1
    </
select>
   
<!--parameterType:参数的配置-->
    <!--#{}
里面写什么都可以-->
   
<select id="queryById" parameterType="Integer" resultType="com.softeem.entity.Student">
    select id,nickname,sex,moble,birthday
        FROM tablename1 WHERE id= #{id}
    </
select>
    <
insert id="insertStudent" parameterType="com.softeem.entity.Student">
        INSERT INTO tablename1(nickname,sex,moble,birthday)
        VALUES(#{nickname},#{sex},#{moble})
    </
insert>
</
mapper>

7.注册映射器

                   mybatis-config里:

<mappers>
    <
mapper resource="org/mybatis/example/BlogMapper.xml"/>
</
mappers>

                  

8.在测试类中进行测试

                   @Test

public void testSqlSessionfactory(){

    SqlSession sqlSession = null;

    try {

        //加载配置文件

        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");

        //利用构造者模式根据配置文件的信息初始化SqlSessionFactory

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder(). build(reader);

        //sqlSessionFactory创建对象sqlSession对象

        sqlSession = sqlSessionFactory.openSession();

        Connection connection = sqlSession.getConnection();

        System.out.println(connection);

    } catch (IOException e) {

        e.printStackTrace();

    }finally {

        if (sqlSession!=null){

            sqlSession.close();

        }

    }

}

@Test

public void testMybatisUtil(){

    SqlSession sqlSession = MybatisUtil.createSqlSession();

    System.out.println(sqlSession.getConnection());

    MybatisUtil.closeSession(sqlSession);

}

@Test

public void testQueryAll(){

   SqlSession sqlSession =null;

   try{

       sqlSession = MybatisUtil.createSqlSession();

       List<Student> list = sqlSession.selectList("XXX.queryAll");

        for (Student student:list){

            System.out.println(student);

        }

   }catch (Exception e){

       e.printStackTrace();

   }finally {

       MybatisUtil.closeSession(sqlSession);

   }

}

/*根据id查询单个*/

@Test

public void testQueryById(){

    SqlSession sqlSession =null;

    try{

        sqlSession = MybatisUtil.createSqlSession();

        Student student = sqlSession.selectOne("XXX.queryById",2);

        System.out.println(student);

    }catch (Exception e){

        e.printStackTrace();

    }finally {

        MybatisUtil.closeSession(sqlSession);

    }

}

/*增的操作*/

@Test

public void testInsertStudent(){

    SqlSession sqlSession= null;

    try{

        sqlSession = MybatisUtil.createSqlSession();

        Student student= new Student();

        student.setNickname("湘湘");

        student.setSex(1);

        student.setMobile("123124214");

        int insert = sqlSession.insert("XXX.insertStudent", student);

        System.out.println("num"+insert);

    }catch (Exception e){

        e.printStackTrace();

    }finally {

        MybatisUtil.closeSession(sqlSession);

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员慕慕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值